-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·362 lines (331 loc) · 13.6 KB
/
Copy pathcli.js
File metadata and controls
executable file
·362 lines (331 loc) · 13.6 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/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, confirm, STORAGE_CONTENT, STORAGE_TAG } = require('./lib/config');
const pkg = require('./package.json');
const parseArgs = (cmd) => {
const { parent = {} } = cmd || {};
const directory = cmd.path || parent.path;
return {
...cmd,
configFile: cmd.config,
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')
.option('-s, --space-id <space-id>', 'Contentful space id')
.option('--host <host>', 'Management API host')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('--cwd <directory>', 'Working directory. Defaults to process.cwd()')
.description('Initialize contentful-migrations')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(
parseArgs(cmd || {}),
[],
[
'managementToken',
'host',
'organizationId',
'spaceId',
'environmentId',
'storage',
'fieldId',
'migrationContentTypeId',
'directory',
]
);
if (config.storage === STORAGE_CONTENT) {
await initializeContentModel(config);
await migrateToContentStorage(config);
}
if (config.storage === STORAGE_TAG) {
await migrateToTagStorage(config);
}
const storeConfig = await confirm({ message: 'Do you want to store the configuration?' });
if (storeConfig) {
// only store relevant config values without management token
// management token should not be stored inside the project
const { host, spaceId, environmentId, storage, fieldId, migrationContentTypeId, directory } = config;
const data = {
host,
spaceId,
environmentId,
directory,
storage,
...(storage === STORAGE_CONTENT ? { migrationContentTypeId } : { fieldId }),
};
// try to store in package.json
const { pkgUp } = await import('pkg-up');
const localPkg = await pkgUp();
if (localPkg) {
const packageJson = await fs.readJson(localPkg);
data.directory = path.relative(path.dirname(localPkg), data.directory);
packageJson.migrations = data;
await fs.outputJson(localPkg, packageJson, { spaces: 2 });
} else {
// store in .migrationsrc if no package.json is available
const cwd = cmd.cwd ? path.resolve(cmd.cwd) : process.cwd();
data.directory = path.relative(cwd, data.directory);
await fs.outputJson(path.join(cwd, '.migrationsrc.json'), data, { 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('-v, --verbose', 'Verbosity')
.option('--host <host>', 'Management API host')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('--cwd <directory>', 'Working directory. Defaults to process.cwd()')
.description('Generate a new Contentful migration from content type')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(parseArgs(cmd || {}), [
'managementToken',
'spaceId',
'environmentId',
'directory',
]);
await fetchMigration({ ...config, 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('-v, --verbose', 'Verbosity')
.option('--host <host>', 'Management API host')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('--cwd <directory>', 'Working directory. Defaults to process.cwd()')
.option('-n, --name <name>', 'Set a name for the migration')
.description('Generate a new Contentful migration')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(parseArgs(cmd || {}), [
'managementToken',
'spaceId',
'environmentId',
'directory',
]);
await createMigration({ ...config, name: cmd.name });
})
);
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('-v, --verbose', 'Verbosity')
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
.option('--host <host>', 'Management API host')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('--cwd <directory>', 'Working directory. Defaults to process.cwd()')
.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 || {}), [
'managementToken',
'spaceId',
'environmentId',
'storage',
'fieldId',
'migrationContentTypeId',
'directory',
]);
const { missingStorageModel } = config;
if (missingStorageModel) {
console.error(pc.red('\nError:'), `Missing migration content type. Run ${pc.cyan('npx migrations init')}`);
process.exit(1);
}
await runMigrations(config);
}, 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('-v, --verbose', 'Verbosity')
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
.option('--host <host>', 'Management API host')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('--cwd <directory>', 'Working directory. Defaults to process.cwd()')
.description('Execute a single migration.')
.action(
actionRunner(async (file, options) => {
const config = await getConfig(parseArgs(options || {}), [
'managementToken',
'spaceId',
'environmentId',
'storage',
'fieldId',
'migrationContentTypeId',
'directory',
]);
const { missingStorageModel } = config;
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), config);
}, 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('-v, --verbose', 'Verbosity')
.option('--host <host>', 'Management API host')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('--add', 'Mark migration as migrated')
.option('--remove', 'Delete migration entry in Contentful')
.option('--cwd <directory>', 'Working directory. Defaults to process.cwd()')
.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 || {}), [
'managementToken',
'spaceId',
'environmentId',
'storage',
'fieldId',
'migrationContentTypeId',
'directory',
]);
const { missingStorageModel } = config;
if (missingStorageModel) {
console.error(pc.red('\nError:'), `Missing migration content type. Run ${pc.cyan('npx migrations init')}`);
process.exit(1);
}
const { storage } = config || {};
if (storage === STORAGE_TAG) {
throw new Error('The version command is not available for the "tag" storage');
}
if (remove) {
await versionDelete(file, config);
} else if (add) {
await versionAdd(file, config);
}
}, true)
);
program
.command('environment <environment-id>')
.option('-s, --space-id <space-id>', 'Contentful space id')
.option('-v, --verbose', 'Verbosity')
.option('--host <host>', 'Management API host')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.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')
.option('--cwd <directory>', 'Working directory. Defaults to process.cwd()')
.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 }), [
'managementToken',
'spaceId',
'environmentId',
]);
if (create) {
return createEnvironment(environmentId, config);
}
if (remove) {
return removeEnvironment(environmentId, config);
}
if (reset) {
return resetEnvironment(environmentId, config);
}
}, 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('-v, --verbose', 'Verbosity')
.option('-t, --template <path/to/template>', 'Use custom template for docs')
.option('--host <host>', 'Management API host')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.option('--extension <file-extension>', 'Use custom file extension (default is `md`)')
.option('--cwd <directory>', 'Working directory. Defaults to process.cwd()')
.description('Generate offline docs from content-types')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(parseArgs(cmd || {}), ['managementToken', 'spaceId', 'environmentId']);
await createOfflineDocs(config);
}, 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('-v, --verbose', 'Verbosity')
.option('-y, --yes', 'Assume "yes" as answer to all prompts and run non-interactively.')
.option('--host <host>', 'Management API host')
.option('--config <path/to/config>', 'Config file path (disables auto detect)')
.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')
.option('--cwd <directory>', 'Working directory. Defaults to process.cwd()')
.action(
actionRunner(async (cmd) => {
const config = await getConfig(parseArgs(cmd || {}), ['managementToken', 'spaceId', 'storage']);
// run migrations on destination environment
await transferContent({
...config,
contentType: cmd.contentType || '',
forceOverwrite: cmd.force || false,
diffConflicts: cmd.diff || false,
});
})
);
program.parse(process.argv);