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

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 (46) 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/{migration.js → stacks/migration.js} +102 -74
  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 +45 -46
  18. package/src/modules/parser.js +65 -52
  19. package/src/services/content-types.js +160 -163
  20. package/src/services/index.js +2 -2
  21. package/src/services/locales.js +33 -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/object-helper.js +7 -7
  35. package/src/utils/safe-promise.js +2 -2
  36. package/src/utils/schema-helper.js +12 -12
  37. package/src/utils/success-handler.js +5 -5
  38. package/src/validators/api-error.js +10 -8
  39. package/src/validators/base-validator.js +14 -14
  40. package/src/validators/create-content-type-validator.js +21 -25
  41. package/src/validators/edit-content-type-validator.js +21 -24
  42. package/src/validators/field-validator.js +10 -8
  43. package/src/validators/index.js +2 -2
  44. package/src/validators/migration-error.js +9 -7
  45. package/src/validators/schema-validator.js +11 -9
  46. package/src/validators/type-error.js +11 -10
@@ -1,17 +1,17 @@
1
- 'use strict'
1
+ 'use strict';
2
2
 
3
- const {map: _map, getCallsite, constants, safePromise} = require('../utils')
4
- const Listr = require('listr')
5
- const {waterfall} = require('async')
6
- const {requests} = constants
3
+ const { map: _map, getCallsite, constants, safePromise } = require('../utils');
4
+ const Listr = require('listr');
5
+ const { waterfall } = require('async');
6
+ const { requests } = constants;
7
7
 
8
8
  // Properties
9
- const {getMapInstance, set, get} = _map
9
+ const { getMapInstance, set, get } = _map;
10
10
 
11
- const ContentType = require('./content-types')
11
+ const ContentType = require('./content-types');
12
12
 
13
13
  // Merge all classes containing migration methods into a single class
14
- const _Migration = _Class => class extends _Class { }
14
+ const _Migration = (_Class) => class extends _Class {};
15
15
 
16
16
  /**
17
17
  * Migration class
@@ -41,66 +41,65 @@ class Migration extends _Migration(ContentType) {
41
41
  * migration.addTask(task)
42
42
  */
43
43
  addTask(taskDescription) {
44
- const {title, failMessage, successMessage} = taskDescription
45
- let {tasks, task} = taskDescription
46
- const callsite = getCallsite()
47
- const mapInstance = getMapInstance()
44
+ const { title, failMessage, successMessage } = taskDescription;
45
+ let { tasks, task } = taskDescription;
46
+ const callsite = getCallsite();
47
+ const mapInstance = getMapInstance();
48
48
  // eslint-disable-next-line no-warning-comments
49
49
  // TODO: Make it better to accept only single task
50
- if (tasks && !Array.isArray(tasks))
51
- tasks = [tasks]
50
+ if (tasks && !Array.isArray(tasks)) tasks = [tasks];
52
51
  if (task && !Array.isArray(task)) {
53
- tasks = [task]
52
+ tasks = [task];
54
53
  }
55
- this.contentTypeService.base.dispatch(callsite, null, null, tasks)
56
- let _requests = get(requests, mapInstance)
54
+ this.contentTypeService.base.dispatch(callsite, null, null, tasks);
55
+ let _requests = get(requests, mapInstance);
57
56
  const req = {
58
57
  title: title,
59
58
  failedTitle: failMessage || `Failed to execute task: ${title}`,
60
59
  successTitle: successMessage || `Successfully executed task: ${title}`,
61
60
  tasks,
62
- }
63
- _requests.push(req)
64
- set(requests, mapInstance, _requests)
61
+ };
62
+ _requests.push(req);
63
+ set(requests, mapInstance, _requests);
65
64
  }
66
65
 
67
66
  async run() {
68
- const mapInstance = getMapInstance()
69
- let _requests = get(requests, mapInstance)
67
+ const mapInstance = getMapInstance();
68
+ let _requests = get(requests, mapInstance);
70
69
  // Make calls from here
71
- const tasks = await this.getTasks(_requests)
72
- const listr = new Listr(tasks)
73
- await listr.run().catch(error => {
74
- this.handleErrors(error)
70
+ const tasks = await this.getTasks(_requests);
71
+ const listr = new Listr(tasks);
72
+ await listr.run().catch((error) => {
73
+ this.handleErrors(error);
75
74
  // When the process is child, send error message to parent
76
- if (process.send) process.send({errorOccurred: true})
77
- })
75
+ if (process.send) process.send({ errorOccurred: true });
76
+ });
78
77
  }
79
78
 
80
- async getTasks(requests) {
81
- const _tasks = []
82
- const results = []
83
- for (let i = 0; i < requests.length; i++) {
84
- let reqObj = requests[i]
85
- const {title, failedTitle, successTitle, tasks} = reqObj
79
+ async getTasks(_requests) {
80
+ const _tasks = [];
81
+ const results = [];
82
+ for (let i = 0; i < _requests.length; i++) {
83
+ let reqObj = _requests[i];
84
+ const { title, failedTitle, successTitle, tasks } = reqObj;
86
85
  const task = {
87
86
  title: title,
88
- task: async (ctx, task) => {
89
- const [err, result] = await safePromise(waterfall(tasks))
87
+ task: async (ctx, _task) => {
88
+ const [err, result] = await safePromise(waterfall(tasks));
90
89
  if (err) {
91
- ctx.error = true
92
- task.title = failedTitle
93
- throw err
90
+ ctx.error = true;
91
+ _task.title = failedTitle;
92
+ throw err;
94
93
  }
95
- result && results.push(result)
96
- task.title = successTitle
97
- return result
94
+ result && results.push(result);
95
+ _task.title = successTitle;
96
+ return result;
98
97
  },
99
- }
100
- _tasks.push(task)
98
+ };
99
+ _tasks.push(task);
101
100
  }
102
- return _tasks
101
+ return _tasks;
103
102
  }
104
103
  }
105
104
 
106
- module.exports = Migration
105
+ module.exports = Migration;
@@ -1,92 +1,105 @@
1
- 'use strict'
1
+ 'use strict';
2
2
 
3
- const Migration = require('./migration')
3
+ const Migration = require('./migration');
4
4
 
5
- const {
6
- CreateContentTypeValidator,
7
- EditContentTypeValidator,
8
- _TypeError,
9
- FieldValidator,
10
- } = require('../validators')
5
+ const { CreateContentTypeValidator, EditContentTypeValidator, _TypeError, FieldValidator } = require('../validators');
11
6
  // eslint-disable-next-line no-warning-comments
12
7
  // TODO: Need a better way to combine classes
13
- const Base = require('./base')
8
+ const Base = require('./base');
14
9
 
15
- const {ActionList} = require('../actions')
10
+ const { ActionList } = require('../actions');
16
11
  // Utils
17
- const {map: _map, constants, fsHelper} = require('../utils')
12
+ const { map: _map, constants, fsHelper } = require('../utils');
18
13
  // map properties
19
- const {getMapInstance, get} = _map
14
+ const { getMapInstance, get } = _map;
20
15
  // Constants
21
- const {actionMapper, MANAGEMENT_SDK, MANAGEMENT_TOKEN, AUTH_TOKEN, API_KEY, BRANCH, MANAGEMENT_CLIENT, SOURCE_BRANCH} = constants
16
+ const {
17
+ actionMapper,
18
+ MANAGEMENT_SDK,
19
+ MANAGEMENT_TOKEN,
20
+ AUTH_TOKEN,
21
+ API_KEY,
22
+ BRANCH,
23
+ MANAGEMENT_CLIENT,
24
+ SOURCE_BRANCH,
25
+ } = constants;
22
26
 
23
27
  class Parser {
24
28
  async getMigrationParser(migrationFunc) {
25
- const migration = new Migration()
26
- const mapInstance = getMapInstance()
27
- const parseResult = {}
28
- const typeErrors = []
29
+ const migration = new Migration();
30
+ const mapInstance = getMapInstance();
31
+ const parseResult = {};
32
+ let typeErrors = [];
29
33
  // migrations
30
34
  try {
31
- const stackSDKInstance = get(MANAGEMENT_SDK, mapInstance)
32
- const managementToken = get(MANAGEMENT_TOKEN, mapInstance)
33
- const authToken = get(AUTH_TOKEN, mapInstance)
34
- const apiKey = get(API_KEY, mapInstance)
35
- const branch = get(BRANCH, mapInstance)
36
- const managementAPIClient = get(MANAGEMENT_CLIENT, mapInstance)
37
- const externalConfigPath = get("config-path", mapInstance)
38
- const externalConfig = get("config", mapInstance)
39
- let externalFileConfig
40
- if (typeof externalConfigPath == "string") {
35
+ const stackSDKInstance = get(MANAGEMENT_SDK, mapInstance);
36
+ const managementToken = get(MANAGEMENT_TOKEN, mapInstance);
37
+ const authToken = get(AUTH_TOKEN, mapInstance);
38
+ const apiKey = get(API_KEY, mapInstance);
39
+ const branch = get(BRANCH, mapInstance);
40
+ const managementAPIClient = get(MANAGEMENT_CLIENT, mapInstance);
41
+ const externalConfigPath = get('config-path', mapInstance);
42
+ const externalConfig = get('config', mapInstance);
43
+ let externalFileConfig;
44
+ if (typeof externalConfigPath == 'string') {
41
45
  externalFileConfig = await fsHelper.readJSONFile(externalConfigPath);
42
46
  }
43
47
  const config = Object.assign({}, externalFileConfig, externalConfig);
44
- await migrationFunc({migration, stackSDKInstance, managementAPIClient, managementToken, authToken, apiKey, branch, config})
48
+ await migrationFunc({
49
+ migration,
50
+ stackSDKInstance,
51
+ managementAPIClient,
52
+ managementToken,
53
+ authToken,
54
+ apiKey,
55
+ branch,
56
+ config,
57
+ });
45
58
  } catch (error) {
46
59
  if (error instanceof TypeError) {
47
60
  if (error.message.includes('is not a function')) {
48
- const base = new Base()
61
+ const base = new Base();
49
62
  // eslint-disable-next-line
50
63
  const [, filename, line] = error.stack.match(/\/([\/\w-_\.]+\.js):(\d*):(\d*)/);
51
64
  const callsite = {
52
65
  getFileName: () => `/${filename}`,
53
66
  getLineNumber: () => line,
54
- }
55
- const errMsgString = error.message.split(' ')
56
- const typeErrorFirstStr = errMsgString[0].split('.')
57
- const typeErrorFunction = typeErrorFirstStr[typeErrorFirstStr.length - 1]
58
- typeErrors.push(typeErrorFunction)
59
- base.dispatch(callsite, null, {typeErrors}, 'typeError')
67
+ };
68
+ const errMsgString = error.message.split(' ');
69
+ const typeErrorFirstStr = errMsgString[0].split('.');
70
+ const typeErrorFunction = typeErrorFirstStr[typeErrorFirstStr.length - 1];
71
+ typeErrors.push(typeErrorFunction);
72
+ base.dispatch(callsite, null, { typeErrors }, 'typeError');
60
73
  }
61
74
  } else {
62
- console.log(error)
75
+ console.log(error);
63
76
  // eslint-disable-next-line
64
- const [, filename, line] = error.stack.match(/\/([\/\w-_\.]+\.js):(\d*):(\d*)/)
77
+ const [, filename, line] = error.stack.match(/\/([\/\w-_\.]+\.js):(\d*):(\d*)/);
65
78
  const callsite = {
66
79
  getFileName: () => `/${filename}`,
67
80
  getLineNumber: () => line,
68
- }
69
- const base = new Base()
70
- let typeErrors = [error]
71
- base.dispatch(callsite, null, {typeErrors}, 'typeError')
81
+ };
82
+ const base = new Base();
83
+ typeErrors = [error];
84
+ base.dispatch(callsite, null, { typeErrors }, 'typeError');
72
85
  }
73
86
  }
74
- const actions = get(actionMapper, mapInstance)
75
- const actionList = new ActionList(actions)
87
+ const actions = get(actionMapper, mapInstance);
88
+ const actionList = new ActionList(actions);
76
89
 
77
- actionList.addValidators(new CreateContentTypeValidator())
78
- actionList.addValidators(new FieldValidator())
79
- actionList.addValidators(new _TypeError())
80
- actionList.addValidators(new EditContentTypeValidator())
90
+ actionList.addValidators(new CreateContentTypeValidator());
91
+ actionList.addValidators(new FieldValidator());
92
+ actionList.addValidators(new _TypeError());
93
+ actionList.addValidators(new EditContentTypeValidator());
81
94
 
82
- const hasErrors = actionList.validate()
95
+ const hasErrors = actionList.validate();
83
96
 
84
97
  if (hasErrors.length > 0) {
85
- parseResult.hasErrors = hasErrors
86
- return parseResult
98
+ parseResult.hasErrors = hasErrors;
99
+ return parseResult;
87
100
  }
88
- return parseResult
101
+ return parseResult;
89
102
  }
90
103
  }
91
104
 
92
- module.exports = Parser
105
+ module.exports = Parser;