-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
executable file
·286 lines (256 loc) · 11.2 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env node
/* eslint-disable no-console */
/* eslint-env node */
const fs = require('fs-extra');
const path = require('path');
const pc = require('picocolors');
const { Command } = require('commander');
const { initializeContentModel, migrateToContentStorage, migrateToTagStorage } = require('./lib/backend');
const { createMigration, runMigrations, fetchMigration, executeMigration } = require('./lib/migration');
const { versionDelete, versionAdd } = require('./lib/version');
const { transferContent } = require('./lib/content');
const { createOfflineDocs } = require('./lib/doc');
const { createEnvironment, removeEnvironment, resetEnvironment } = require('./lib/environment');
const { getConfig, askAll, askMissing, STORAGE_CONTENT, STORAGE_TAG } = require('./lib/config');
const pkg = require('./package.json');
require('dotenv').config();
const parseArgs = (cmd) => {
const { parent = {} } = cmd || {};
const directory = cmd.path || parent.path;
return {
...cmd,
configFile: cmd.configFile,
environment: cmd.env || parent.env,
directory: directory ? path.resolve(directory) : undefined,
sourceEnvironmentId: cmd.sourceEnvironmentId || parent.sourceEnvironmentId,
destEnvironmentId: cmd.destEnvironmentId || parent.destEnvironmentId,
verbose: cmd.verbose || parent.verbose,
template: cmd.template || parent.template,
yes: cmd.yes || parent.yes,
extension: cmd.extension || parent.extension,
bail: cmd.bail || parent.bail,
};
};
const errorHandler = (error, log) => {
if (log) {
const { errors, message } = error;
console.error(pc.red('\nError:'), message);
(errors || []).forEach((err) => {
console.error(pc.red('Error:'), err.message);
});
}
process.exit(1);
};
const actionRunner = (fn, log = true) => {
return (...args) => {
const verbose = args.some((arg) => arg.verbose);
return fn(...args).catch((error) => errorHandler(error, verbose || log));
};
};
const program = new Command();
program.version(pkg.version);
program
.command('init')
.description('Initialize contentful-migrations')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(parseArgs(cmd || {}));
const verified = await askAll(config);
const { managementToken, accessToken, environmentId, spaceId, ...rest } = verified;
if (verified.storage === STORAGE_CONTENT) {
await initializeContentModel({ ...config, ...verified });
await migrateToContentStorage({ ...config, ...verified });
}
if (verified.storage === STORAGE_TAG) {
await migrateToTagStorage({ ...config, ...verified });
}
if (!process.env.CONTENTFUL_SPACE_ID) {
rest.spaceId = spaceId;
}
// try to store in package.json
const { pkgUp } = await import('pkg-up');
const localPkg = await pkgUp();
if (localPkg) {
const packageJson = await fs.readJson(localPkg);
rest.directory = path.relative(path.dirname(localPkg), rest.directory);
packageJson.migrations = rest;
await fs.outputJson(localPkg, packageJson, { spaces: 2 });
} else {
// store in .migrationsrc if no package.json is available
await fs.outputJson(path.join(process.cwd(), '.migrationsrc'), rest, { spaces: 2 });
}
})
);
program
.command('fetch')
.option('-s, --space-id <space-id>', 'Contentful space id')
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
.option('-c, --content-type <content-type...>', 'Specify content-types')
.option('-p, --path <path/to/migrations>', 'Change the path where the migrations are saved')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('-v, --verbose', 'Verbosity')
.description('Generate a new Contentful migration from content type')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(parseArgs(cmd || {}));
const verified = await askMissing(config);
await fetchMigration({ ...verified, contentType: cmd.contentType });
})
);
program
.command('generate')
.option('-s, --space-id <space-id>', 'Contentful space id')
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
.option('-p, --path <path/to/migrations>', 'Change the path where the migrations are saved')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('-v, --verbose', 'Verbosity')
.description('Generate a new Contentful migration')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(parseArgs(cmd || {}));
const verified = await askMissing(config);
await createMigration(verified);
})
);
program
.command('migrate')
.option('-s, --space-id <space-id>', 'Contentful space id')
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
.option('-p, --path <path/to/migrations>', 'Change the path where the migrations are stored')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('-v, --verbose', 'Verbosity')
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
.option('--bail', 'Abort execution after first failed migration (default: true)', true)
.option('--no-bail', 'Ignore failed migrations')
.description('Execute all unexecuted migrations available.')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(parseArgs(cmd || {}));
const verified = await askMissing(config);
const { missingStorageModel } = verified;
if (missingStorageModel) {
console.error(pc.red('\nError:'), `Missing migration content type. Run ${pc.cyan('npx migrations init')}`);
process.exit(1);
}
await runMigrations(verified);
}, false)
);
program
.command('execute <file>')
.option('-s, --space-id <space-id>', 'Contentful space id')
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('-v, --verbose', 'Verbosity')
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
.description('Execute a single migration.')
.action(
actionRunner(async (file, options) => {
const config = await getConfig(parseArgs(options || {}));
const verified = await askMissing(config);
const { missingStorageModel } = verified;
if (missingStorageModel) {
console.error(pc.red('\nError:'), `Missing migration content type. Run ${pc.cyan('npx migrations init')}`);
process.exit(1);
}
await executeMigration(path.resolve(file), verified);
}, false)
);
program
.command('version <file>')
.option('-s, --space-id <space-id>', 'Contentful space id')
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('-v, --verbose', 'Verbosity')
.option('--add', 'Mark migration as migrated')
.option('--remove', 'Delete migration entry in Contentful')
.description('Manually mark a migration as migrated or not. (Only available with the Content-model storage)')
.action(
actionRunner(async (file, options) => {
const { remove, add } = options;
const config = await getConfig(parseArgs(options || {}));
const verified = await askMissing(config);
const { missingStorageModel } = verified;
if (missingStorageModel) {
console.error(pc.red('\nError:'), `Missing migration content type. Run ${pc.cyan('npx migrations init')}`);
process.exit(1);
}
const { storage } = verified || {};
if (storage === STORAGE_TAG) {
throw new Error('The version command is not available for the "tag" storage');
}
if (remove) {
await versionDelete(file, verified);
} else if (add) {
await versionAdd(file, verified);
}
}, true)
);
program
.command('environment <environment-id>')
.option('-s, --space-id <space-id>', 'Contentful space id')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('-v, --verbose', 'Verbosity')
.option('--create', 'Create new contentful environment')
.option('--remove', 'Delete contentful environment')
.option('--reset', 'Reset contentful environment')
.option('--source-environment-id <environment-id>', 'Set the source environment to clone new environment from')
.description('Add or remove a contentful environment for migrations')
.action(
actionRunner(async (environmentId, options) => {
const { remove, create, reset } = options;
const config = await getConfig(parseArgs({ ...(options || {}), environmentId }));
const verified = await askMissing(config, ['accessToken', 'spaceId', 'environmentId']);
if (create) {
return createEnvironment(environmentId, verified);
}
if (remove) {
return removeEnvironment(environmentId, verified);
}
if (reset) {
return resetEnvironment(environmentId, verified);
}
}, true)
);
program
.command('doc')
.option('-s, --space-id <space-id>', 'Contentful space id')
.option('-e, --environment-id <environment-id>', 'Change the Contentful environment')
.option('-p, --path <path/to/docs>', 'Change the path where the docs are stored')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('-v, --verbose', 'Verbosity')
.option('-t, --template <path/to/template>', 'Use custom template for docs')
.option('--extension <file-extension>', 'Use custom file extension (default is `md`)')
.description('Generate offline docs from content-types')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(parseArgs(cmd || {}));
const verified = await askMissing(config, ['accessToken', 'spaceId', 'environmentId']);
await createOfflineDocs(verified);
}, true)
);
program
.command('content')
.requiredOption('--source-environment-id <environment-id>', 'Set the Contentful source environment (from)')
.requiredOption('--dest-environment-id <environment-id>', 'Set the Contentful destination environment (to)')
.option('-s, --space-id <space-id>', 'Contentful space id')
.option('-c, --content-type <content-type>', 'Specify content-type')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('-v, --verbose', 'Verbosity')
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
.option('--diff', 'Manually choose skip/overwrite for every conflict')
.option('--force', 'No manual diffing. Overwrites all conflicting entries/assets')
.description('Transfer content from source environment to destination environment')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(parseArgs(cmd || {}));
const verified = await askMissing({ ...config, environmentId: 'not-used' });
// run migrations on destination environment
await transferContent({
...verified,
contentType: cmd.contentType || '',
forceOverwrite: cmd.force || false,
diffConflicts: cmd.diff || false,
});
})
);
program.parse(process.argv);