@contentstack/cli-migration 0.1.1-beta.3 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +22 -27
  3. package/oclif.manifest.json +1 -1
  4. package/package.json +16 -11
  5. package/src/actions/action-list.js +11 -11
  6. package/src/actions/index.js +33 -34
  7. package/src/commands/cm/stacks/migration.js +305 -0
  8. package/src/config/api-config.js +8 -9
  9. package/src/config/default-options.js +2 -2
  10. package/src/config/index.js +2 -2
  11. package/src/config/master-locale.js +2 -2
  12. package/src/modules/base.js +33 -33
  13. package/src/modules/content-types.js +76 -76
  14. package/src/modules/fields.js +73 -73
  15. package/src/modules/index.js +2 -2
  16. package/src/modules/locale.js +13 -13
  17. package/src/modules/migration.js +55 -49
  18. package/src/modules/parser.js +65 -52
  19. package/src/services/content-types.js +160 -166
  20. package/src/services/index.js +2 -2
  21. package/src/services/locales.js +32 -35
  22. package/src/utils/auto-retry.js +14 -12
  23. package/src/utils/callsite.js +14 -14
  24. package/src/utils/constants.js +108 -115
  25. package/src/utils/contentstack-sdk.js +42 -43
  26. package/src/utils/error-handler.js +8 -8
  27. package/src/utils/error-helper.js +41 -40
  28. package/src/utils/fs-helper.js +12 -12
  29. package/src/utils/get-batches.js +4 -4
  30. package/src/utils/get-config.js +6 -6
  31. package/src/utils/group-by.js +17 -17
  32. package/src/utils/index.js +2 -2
  33. package/src/utils/logger.js +42 -52
  34. package/src/utils/map.js +3 -3
  35. package/src/utils/object-helper.js +7 -7
  36. package/src/utils/safe-promise.js +2 -2
  37. package/src/utils/schema-helper.js +12 -12
  38. package/src/utils/success-handler.js +5 -5
  39. package/src/validators/api-error.js +10 -8
  40. package/src/validators/base-validator.js +14 -14
  41. package/src/validators/create-content-type-validator.js +21 -25
  42. package/src/validators/edit-content-type-validator.js +21 -24
  43. package/src/validators/field-validator.js +10 -8
  44. package/src/validators/index.js +2 -2
  45. package/src/validators/migration-error.js +9 -7
  46. package/src/validators/schema-validator.js +11 -9
  47. package/src/validators/type-error.js +11 -10
  48. package/src/commands/cm/migration.js +0 -271
@@ -0,0 +1,305 @@
1
+ /* eslint-disable no-unused-expressions */
2
+ /* eslint-disable no-warning-comments */
3
+ /* eslint-disable camelcase */
4
+ 'use strict';
5
+
6
+ // Dependencies
7
+ const Listr = require('listr');
8
+ const { resolve, extname } = require('path');
9
+ const { Command, flags } = require('@contentstack/cli-command');
10
+ const { waterfall } = require('async');
11
+ const { Parser } = require('../../../modules');
12
+ const { ActionList } = require('../../../actions');
13
+ const fs = require('fs');
14
+ const chalk = require('chalk');
15
+ const { configHandler } = require('@contentstack/cli-utilities');
16
+ const { printFlagDeprecation } = require('@contentstack/cli-utilities');
17
+
18
+ const { ApiError, SchemaValidator, MigrationError, FieldValidator } = require('../../../validators');
19
+
20
+ // Utils
21
+ const { map: _map, constants, safePromise, errorHelper } = require('../../../utils');
22
+ const { success } = require('../../../utils/logger');
23
+
24
+ // Properties
25
+ const { get, set, getMapInstance, resetMapInstance } = _map;
26
+ const {
27
+ requests: _requests,
28
+ actionMapper,
29
+ MANAGEMENT_SDK,
30
+ MANAGEMENT_TOKEN,
31
+ AUTH_TOKEN,
32
+ API_KEY,
33
+ BRANCH,
34
+ MANAGEMENT_CLIENT,
35
+ } = constants;
36
+
37
+ class MigrationCommand extends Command {
38
+ static examples = [
39
+ '$ csdx cm:migration --file-path <migration/script/file/path> -k <api-key>',
40
+ '$ csdx cm:migration --file-path <migration/script/file/path> -k <api-key> --branch <target branch name>',
41
+ '$ csdx cm:migration --config <key1>:<value1> <key2>:<value2> ... --file-path <migration/script/file/path>',
42
+ '$ csdx cm:migration --config-file <path/to/json/config/file> --file-path <migration/script/file/path>',
43
+ '$ csdx cm:migration --multiple --file-path <migration/scripts/dir/path> ',
44
+ '$ csdx cm:migration --alias --file-path <migration/script/file/path> -k <api-key>',
45
+ ];
46
+
47
+ async run() {
48
+ // TODO: filePath validation required.
49
+ const migrationCommandFlags = this.parse(MigrationCommand).flags;
50
+ const { branch } = migrationCommandFlags;
51
+ const filePath = migrationCommandFlags['file-path'] || migrationCommandFlags.filePath;
52
+ const multi = migrationCommandFlags.multiple || migrationCommandFlags.multi;
53
+ const authtoken = configHandler.get('authtoken');
54
+ const apiKey = migrationCommandFlags['api-key'] || migrationCommandFlags['stack-api-key'];
55
+ const alias = migrationCommandFlags['alias'] || migrationCommandFlags['management-token-alias'];
56
+ const config = migrationCommandFlags['config'];
57
+
58
+ if (!authtoken && !alias) {
59
+ this.log(
60
+ "AuthToken is not present in local drive, Hence use 'csdx auth:login' command for login or provide management token alias",
61
+ );
62
+ this.exit();
63
+ }
64
+
65
+ if (!filePath) {
66
+ this.log('Please provide the migration script file path, use --file-path flag');
67
+ this.exit();
68
+ }
69
+
70
+ // Reset map instance
71
+ const mapInstance = getMapInstance();
72
+ resetMapInstance(mapInstance);
73
+ if (migrationCommandFlags['config-file']) {
74
+ set('config-path', mapInstance, migrationCommandFlags['config-file']);
75
+ }
76
+
77
+ if (Array.isArray(config) && config.length > 0) {
78
+ let configObj = config.reduce((a, v) => {
79
+ let objArr = v.split(':');
80
+ return { ...a, [objArr[0]]: objArr[1] };
81
+ }, {});
82
+ set('config', mapInstance, configObj);
83
+ }
84
+
85
+ let stackSDKInstance;
86
+ if (branch) {
87
+ set(BRANCH, mapInstance, branch);
88
+ }
89
+
90
+ if (alias) {
91
+ let managementToken = this.getToken(alias);
92
+ if (managementToken) {
93
+ set(MANAGEMENT_TOKEN, mapInstance, managementToken);
94
+ set(API_KEY, mapInstance, managementToken.apiKey);
95
+ if (branch) {
96
+ stackSDKInstance = this.managementAPIClient.stack({
97
+ management_token: managementToken.token,
98
+ api_key: managementToken.apiKey,
99
+ branch_uid: branch,
100
+ });
101
+ } else {
102
+ stackSDKInstance = this.managementAPIClient.stack({
103
+ management_token: managementToken.token,
104
+ api_key: managementToken.apiKey,
105
+ });
106
+ }
107
+ }
108
+ } else if (authtoken) {
109
+ set(AUTH_TOKEN, mapInstance, authtoken);
110
+ set(API_KEY, mapInstance, apiKey);
111
+ this.managementAPIClient = { authtoken: this.authToken };
112
+ if (branch) {
113
+ stackSDKInstance = this.managementAPIClient.stack({
114
+ api_key: apiKey,
115
+ branch_uid: branch,
116
+ });
117
+ } else {
118
+ stackSDKInstance = this.managementAPIClient.stack({ api_key: apiKey });
119
+ }
120
+ }
121
+
122
+ set(MANAGEMENT_SDK, mapInstance, stackSDKInstance);
123
+ set(MANAGEMENT_CLIENT, mapInstance, this.managementAPIClient);
124
+
125
+ if (multi) {
126
+ await this.execMultiFiles(filePath, mapInstance);
127
+ } else {
128
+ await this.execSingleFile(filePath, mapInstance);
129
+ }
130
+ }
131
+
132
+ async execSingleFile(filePath, mapInstance) {
133
+ // Resolved absolute path
134
+ const resolvedMigrationPath = resolve(filePath);
135
+ // User provided migration function
136
+ const migrationFunc = require(resolvedMigrationPath);
137
+
138
+ const parser = new Parser();
139
+
140
+ try {
141
+ const migrationParser = await parser.getMigrationParser(migrationFunc);
142
+ if (migrationParser.hasErrors) {
143
+ errorHelper(migrationParser.hasErrors);
144
+ // When the process is child, send error message to parent
145
+ if (process.send) process.send({ errorOccurred: true });
146
+ this.exit(1);
147
+ }
148
+
149
+ // Make calls from here
150
+ const requests = get(_requests, mapInstance);
151
+ // Fetches tasks array
152
+ const tasks = this.getTasks(requests);
153
+
154
+ const listr = new Listr(tasks);
155
+
156
+ await listr.run().catch((error) => {
157
+ this.handleErrors(error);
158
+ // When the process is child, send error message to parent
159
+ if (process.send) process.send({ errorOccurred: true });
160
+ });
161
+ requests.splice(0, requests.length);
162
+ } catch (error) {
163
+ // errorHandler(null, null, null, error)
164
+ this.log(error);
165
+ }
166
+ }
167
+
168
+ async execMultiFiles(filePath, mapInstance) {
169
+ // Resolved absolute path
170
+ const resolvedMigrationPath = resolve(filePath);
171
+ try {
172
+ const files = fs.readdirSync(resolvedMigrationPath);
173
+ for (const element of files) {
174
+ const file = element;
175
+ if (extname(file) === '.js') {
176
+ success(chalk`{white Executing file:} {grey {bold ${file}}}`);
177
+ // eslint-disable-next-line no-await-in-loop
178
+ await this.execSingleFile(resolve(filePath, file), mapInstance);
179
+ }
180
+ }
181
+ } catch (error) {
182
+ errorHelper(error);
183
+ }
184
+ }
185
+
186
+ getTasks(requests) {
187
+ const _tasks = [];
188
+ const results = [];
189
+
190
+ const taskFn = (reqObj) => {
191
+ const { failedTitle, successTitle, tasks } = reqObj;
192
+
193
+ return async (ctx, task) => {
194
+ const [err, result] = await safePromise(waterfall(tasks));
195
+ if (err) {
196
+ ctx.error = true;
197
+ task.title = failedTitle;
198
+ throw err;
199
+ }
200
+ result && results.push(result);
201
+ task.title = successTitle;
202
+ return result;
203
+ }
204
+ }
205
+
206
+ for (const element of requests) {
207
+ let reqObj = element;
208
+ const { title } = reqObj;
209
+ const taskObj = {
210
+ title: title,
211
+ task: taskFn(reqObj)
212
+ };
213
+ _tasks.push(taskObj);
214
+ }
215
+ return _tasks;
216
+ }
217
+
218
+ handleErrors() {
219
+ const mapInstance = getMapInstance();
220
+ const actions = get(actionMapper, mapInstance);
221
+ const actionList = new ActionList(actions);
222
+
223
+ actionList.addValidators(new ApiError());
224
+ actionList.addValidators(new SchemaValidator());
225
+ actionList.addValidators(new MigrationError());
226
+ actionList.addValidators(new FieldValidator());
227
+
228
+ const errors = actionList.validate();
229
+ errorHelper(errors);
230
+ }
231
+ }
232
+
233
+ MigrationCommand.description = 'Contentstack migration script.';
234
+
235
+ MigrationCommand.flags = {
236
+ 'api-key': flags.string({
237
+ char: 'k',
238
+ description: 'With this flag add the API key of your stack.',
239
+ dependsOn: ['authtoken'],
240
+ exclusive: ['alias'],
241
+ parse: printFlagDeprecation(['--api-key'], ['-k', '--stack-api-key']),
242
+ hidden: true,
243
+ }),
244
+ 'stack-api-key': flags.string({
245
+ char: 'k',
246
+ description: 'With this flag add the API key of your stack.',
247
+ dependsOn: ['authtoken'],
248
+ exclusive: ['alias'],
249
+ }),
250
+ authtoken: flags.boolean({
251
+ char: 'A',
252
+ description:
253
+ 'Use this flag to use the auth token of the current session. After logging in CLI, an auth token is generated for each new session.',
254
+ dependsOn: ['api-key'],
255
+ exclusive: ['alias'],
256
+ parse: printFlagDeprecation(['-A', '--authtoken']),
257
+ hidden: true,
258
+ }),
259
+ alias: flags.string({
260
+ char: 'a',
261
+ description: 'Use this flag to add the management token alias.',
262
+ exclusive: ['authtoken'],
263
+ }),
264
+ 'management-token-alias': flags.string({
265
+ description: 'alias of the management token',
266
+ exclusive: ['authtoken'],
267
+ hidden: true,
268
+ parse: printFlagDeprecation(['--management-token-alias'], ['-a', '--alias']),
269
+ }),
270
+ filePath: flags.string({
271
+ char: 'n',
272
+ description: 'Use this flag to provide the path of the file of the migration script provided by the user.',
273
+ parse: printFlagDeprecation(['-n', '--filePath'], ['--file-path']),
274
+ hidden: true,
275
+ }),
276
+ 'file-path': flags.string({
277
+ description: 'Use this flag to provide the path of the file of the migration script provided by the user.',
278
+ }),
279
+ branch: flags.string({
280
+ char: 'B',
281
+ description: 'Use this flag to add the branch name where you want to perform the migration.',
282
+ parse: printFlagDeprecation(['-B'], ['--branch']),
283
+ }),
284
+ 'config-file': flags.string({
285
+ description: '[optional] Path of the JSON configuration file',
286
+ }),
287
+ config: flags.string({
288
+ description: '[optional] inline configuration, <key1>:<value1>',
289
+ multiple: true,
290
+ }),
291
+ multi: flags.boolean({
292
+ description: 'This flag helps you to migrate multiple content files in a single instance.',
293
+ parse: printFlagDeprecation(['--multi'], ['--multiple']),
294
+ hidden: true,
295
+ }),
296
+ multiple: flags.boolean({
297
+ description: 'This flag helps you to migrate multiple content files in a single instance.',
298
+ }),
299
+ };
300
+
301
+ MigrationCommand.aliases = ['cm:migration'];
302
+
303
+ MigrationCommand.usage = 'cm:stacks:migration [-k <value>] [-a <value>] [--file-path <value>] [--branch <value>] [--config-file <value>] [--config <value>] [--multiple]';
304
+
305
+ module.exports = MigrationCommand;
@@ -1,17 +1,16 @@
1
1
  /* eslint-disable camelcase */
2
- "use strict";
2
+ 'use strict';
3
3
 
4
4
  const { CONTENTSTACK_API_KEY, CONTENTSTACK_AUTHTOKEN } = process.env;
5
- const { version } = require("../../package.json");
5
+ const { version } = require('../../package.json');
6
6
  module.exports = {
7
- hostname: "dev9-app.contentstack.com",
8
- // hostname: 'stag-app.contentstack.com',
9
- version: "/v3",
10
- method: "GET", // Default Http method
7
+ hostname: 'api.contentstack.io',
8
+ version: '/v3',
9
+ method: 'GET', // Default Http method
11
10
  headers: {
12
- "Content-Type": "application/json",
13
- "Content-Length": null,
14
- "X-User-Agent": `@contentstack-migration/v${version}`,
11
+ 'Content-Type': 'application/json',
12
+ 'Content-Length': null,
13
+ 'X-User-Agent': `@contentstack-migration/v${version}`,
15
14
  authtoken: CONTENTSTACK_AUTHTOKEN,
16
15
  api_key: CONTENTSTACK_API_KEY,
17
16
  // management_token: CONTENTSTACK_MANAGEMENT_TOKEN
@@ -1,7 +1,7 @@
1
1
  /* eslint-disable camelcase */
2
- 'use strict'
2
+ 'use strict';
3
3
 
4
4
  module.exports = {
5
5
  is_page: true,
6
6
  singleton: false,
7
- }
7
+ };
@@ -1,7 +1,7 @@
1
- 'use strict'
1
+ 'use strict';
2
2
 
3
3
  module.exports = {
4
4
  apiConfig: require('./api-config'),
5
5
  defaultOptions: require('./default-options'),
6
6
  masterLocale: require('./master-locale'),
7
- }
7
+ };
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable camelcase */
2
- 'use strict'
2
+ 'use strict';
3
3
 
4
4
  module.exports = {
5
5
  master_locale: {
@@ -7,4 +7,4 @@ module.exports = {
7
7
  name: 'English - United States',
8
8
  code: 'en-us',
9
9
  },
10
- }
10
+ };
@@ -1,12 +1,12 @@
1
- 'use strict'
1
+ 'use strict';
2
2
 
3
3
  // Utils
4
- const {map: _map, constants} = require('../utils')
4
+ const { map: _map, constants } = require('../utils');
5
5
  // Actions
6
- const {actionCreators} = require('../actions')
6
+ const { actionCreators } = require('../actions');
7
7
  // Utils properties
8
- const {getMapInstance, get} = _map
9
- const {actionMapper} = constants
8
+ const { getMapInstance, get } = _map;
9
+ const { actionMapper } = constants;
10
10
 
11
11
  /**
12
12
  * Base class for module classes
@@ -15,9 +15,9 @@ const {actionMapper} = constants
15
15
  */
16
16
  class Base {
17
17
  constructor(id, action) {
18
- this.id = id
19
- this.action = action
20
- this.actions = []
18
+ this.id = id;
19
+ this.action = action;
20
+ this.actions = [];
21
21
  }
22
22
 
23
23
  /**
@@ -26,14 +26,14 @@ class Base {
26
26
  * @returns {Base} current instance of inherited class
27
27
  */
28
28
  title(value) {
29
- const mapInstance = getMapInstance()
30
- const {id, action} = this
29
+ const mapInstance = getMapInstance();
30
+ const { id, action } = this;
31
31
 
32
- const contentType = get(id, mapInstance)
32
+ const contentType = get(id, mapInstance);
33
33
 
34
- contentType[action].content_type.title = value
34
+ contentType[action].content_type.title = value;
35
35
 
36
- return this
36
+ return this;
37
37
  }
38
38
 
39
39
  /**
@@ -42,11 +42,11 @@ class Base {
42
42
  * @returns {Base} current instance of inherited class
43
43
  */
44
44
  description(value) {
45
- const mapInstance = getMapInstance()
46
- const {id, action} = this
47
- const contentType = get(id, mapInstance)
48
- contentType[action].content_type.description = value
49
- return this
45
+ const mapInstance = getMapInstance();
46
+ const { id, action } = this;
47
+ const contentType = get(id, mapInstance);
48
+ contentType[action].content_type.description = value;
49
+ return this;
50
50
  }
51
51
 
52
52
  /**
@@ -55,14 +55,14 @@ class Base {
55
55
  * @returns {Base} current instance of inherited class
56
56
  */
57
57
  force(value) {
58
- const mapInstance = getMapInstance()
59
- const {id, action} = this
58
+ const mapInstance = getMapInstance();
59
+ const { id, action } = this;
60
60
 
61
- const contentType = get(id, mapInstance)
61
+ const contentType = get(id, mapInstance);
62
62
 
63
- contentType[action].content_type.force = value
63
+ contentType[action].content_type.force = value;
64
64
 
65
- return this
65
+ return this;
66
66
  }
67
67
 
68
68
  /**
@@ -75,21 +75,21 @@ class Base {
75
75
  */
76
76
  dispatch(callsite, id, opts, method) {
77
77
  if (!id && !opts) {
78
- let mapInstance = getMapInstance()
79
- let actions = get(actionMapper, mapInstance) // Returns an array if empty
80
- let action = actionCreators.customTasks(callsite, opts)
81
- actions.push(action)
78
+ let mapInstance = getMapInstance();
79
+ let actions = get(actionMapper, mapInstance); // Returns an array if empty
80
+ let action = actionCreators.customTasks(callsite, opts);
81
+ actions.push(action);
82
82
  } else {
83
- let mapInstance = getMapInstance()
84
- let actions = get(actionMapper, mapInstance) // Returns an array if empty
85
- let action = actionCreators.contentType[method](callsite, id, {...opts, id})
86
- actions.push(action)
83
+ let mapInstance = getMapInstance();
84
+ let actions = get(actionMapper, mapInstance); // Returns an array if empty
85
+ let action = actionCreators.contentType[method](callsite, id, { ...opts, id });
86
+ actions.push(action);
87
87
  }
88
88
  }
89
89
 
90
90
  getActions() {
91
- return this.actions
91
+ return this.actions;
92
92
  }
93
93
  }
94
94
 
95
- module.exports = Base
95
+ module.exports = Base;