@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
@@ -1,21 +1,22 @@
1
- 'use strict'
1
+ 'use strict';
2
2
 
3
3
  class _TypeError {
4
- constructor() {
5
- }
4
+ constructor() {}
6
5
 
7
6
  validate(data) {
8
7
  if (data.payload.typeErrors) {
9
- return [{
10
- ...data,
11
- message: `${data.payload.typeErrors[0]} is not a valid function`,
12
- }]
8
+ return [
9
+ {
10
+ ...data,
11
+ message: `${data.payload.typeErrors[0]} is not a valid function`,
12
+ },
13
+ ];
13
14
  }
14
- return []
15
+ return [];
15
16
  }
16
17
 
17
18
  isApplicable(action) {
18
- return action.type === 'typeError'
19
+ return action.type === 'typeError';
19
20
  }
20
21
  }
21
- module.exports = _TypeError
22
+ module.exports = _TypeError;
@@ -1,271 +0,0 @@
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
-
16
- const {
17
- ApiError,
18
- SchemaValidator,
19
- MigrationError,
20
- FieldValidator,
21
- } = require("../../validators");
22
-
23
- // Utils
24
- const {
25
- map: _map,
26
- constants,
27
- safePromise,
28
- errorHelper,
29
- } = require("../../utils");
30
- const { success } = require("../../utils/logger");
31
-
32
- // Properties
33
- const { get, set, getMapInstance, resetMapInstance } = _map;
34
- const {
35
- requests: _requests,
36
- actionMapper,
37
- MANAGEMENT_SDK,
38
- MANAGEMENT_TOKEN,
39
- AUTH_TOKEN,
40
- API_KEY,
41
- BRANCH,
42
- MANAGEMENT_CLIENT,
43
- } = constants;
44
-
45
- class MigrationCommand extends Command {
46
- static examples = [
47
- "$ csdx cm:migration -A -n <migration/script/file/path> -k <api-key>",
48
- "$ csdx cm:migration -A -n <migration/script/file/path> -k <api-key> -B <target branch name>",
49
- "$ csdx cm:migration --config <key1>:<value1> <key2>:<value2> ... -n <migration/script/file/path>",
50
- "$ csdx cm:migration --config-file <path/to/json/config/file> -n <migration/script/file/path>",
51
- "$ csdx cm:migration --multi -n <migration/scripts/dir/path> ",
52
- "$ csdx cm:migration -a -n <migration/script/file/path> -k <api-key>",
53
- ];
54
-
55
- async run() {
56
- // TODO: filePath validation required.
57
- const migrationCommandFlags = this.parse(MigrationCommand).flags;
58
- const { filePath, multi, branch } = migrationCommandFlags;
59
- const authtoken = migrationCommandFlags.authtoken;
60
- const apiKey = migrationCommandFlags["api-key"];
61
- const alias = migrationCommandFlags["management-token-alias"];
62
- const config = migrationCommandFlags["config"];
63
-
64
- if (!filePath) {
65
- this.log("Please provide the migration script file path, use -n or --filePath flag");
66
- this.exit();
67
- }
68
-
69
- // Reset map instance
70
- const mapInstance = getMapInstance();
71
- resetMapInstance(mapInstance);
72
- if (migrationCommandFlags["config-file"]) {
73
- set("config-path", mapInstance, migrationCommandFlags["config-file"]);
74
- }
75
-
76
- if (Array.isArray(config) && config.length > 0) {
77
- let configObj = config.reduce((a, v) => {
78
- let objArr = v.split(":");
79
- return { ...a, [objArr[0]]: objArr[1] };
80
- }, {});
81
- set("config", mapInstance, configObj);
82
- }
83
-
84
- let stackSDKInstance;
85
- if (branch) {
86
- set(BRANCH, mapInstance, branch);
87
- }
88
-
89
- if (alias) {
90
- let managementToken = this.getToken(alias);
91
- if (managementToken) {
92
- set(MANAGEMENT_TOKEN, mapInstance, managementToken);
93
- set(API_KEY, mapInstance, managementToken.apiKey);
94
- if (branch) {
95
- stackSDKInstance = this.managementAPIClient.stack({
96
- management_token: managementToken.token,
97
- api_key: managementToken.apiKey,
98
- branch_uid: branch,
99
- });
100
- } else {
101
- stackSDKInstance = this.managementAPIClient.stack({
102
- management_token: managementToken.token,
103
- api_key: managementToken.apiKey,
104
- });
105
- }
106
- }
107
- }
108
-
109
- if (authtoken) {
110
- set(AUTH_TOKEN, mapInstance, authtoken);
111
- set(API_KEY, mapInstance, apiKey);
112
- this.managementAPIClient = { authtoken: this.authToken };
113
- if (branch) {
114
- stackSDKInstance = this.managementAPIClient.stack({
115
- api_key: apiKey,
116
- branch_uid: branch,
117
- });
118
- } else {
119
- stackSDKInstance = this.managementAPIClient.stack({ api_key: apiKey });
120
- }
121
- }
122
-
123
- set(MANAGEMENT_SDK, mapInstance, stackSDKInstance);
124
- set(MANAGEMENT_CLIENT, mapInstance, this.managementAPIClient);
125
-
126
- if (multi) {
127
- await this.execMultiFiles(filePath, mapInstance);
128
- } else {
129
- await this.execSingleFile(filePath, mapInstance);
130
- }
131
- }
132
-
133
- async execSingleFile(filePath, mapInstance) {
134
- // Resolved absolute path
135
- const resolvedMigrationPath = resolve(filePath);
136
- // User provided migration function
137
- const migrationFunc = require(resolvedMigrationPath);
138
-
139
- const parser = new Parser();
140
-
141
- try {
142
- const migrationParser = await parser.getMigrationParser(migrationFunc);
143
- if (migrationParser.hasErrors) {
144
- errorHelper(migrationParser.hasErrors);
145
- // When the process is child, send error message to parent
146
- if (process.send) process.send({ errorOccurred: true });
147
- this.exit(1);
148
- }
149
-
150
- // Make calls from here
151
- const requests = get(_requests, mapInstance);
152
- // Fetches tasks array
153
- const tasks = this.getTasks(requests);
154
-
155
- const listr = new Listr(tasks);
156
-
157
- await listr.run().catch((error) => {
158
- this.handleErrors(error);
159
- // When the process is child, send error message to parent
160
- if (process.send) process.send({ errorOccurred: true });
161
- });
162
- requests.splice(0, requests.length);
163
- } catch (error) {
164
- // errorHandler(null, null, null, 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 (let index = 0; index < files.length; index++) {
174
- const file = files[index];
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
- error(error);
183
- }
184
- }
185
-
186
- getTasks(requests) {
187
- const _tasks = [];
188
- const results = [];
189
-
190
- for (let i = 0; i < requests.length; i++) {
191
- let reqObj = requests[i];
192
- const { title, failedTitle, successTitle, tasks } = reqObj;
193
- const task = {
194
- title: title,
195
- task: async (ctx, task) => {
196
- const [err, result] = await safePromise(waterfall(tasks));
197
- if (err) {
198
- ctx.error = true;
199
- task.title = failedTitle;
200
- throw err;
201
- }
202
- result && results.push(result);
203
- task.title = successTitle;
204
- return result;
205
- },
206
- };
207
- _tasks.push(task);
208
- }
209
- return _tasks;
210
- }
211
-
212
- handleErrors() {
213
- const mapInstance = getMapInstance();
214
- const actions = get(actionMapper, mapInstance);
215
- const actionList = new ActionList(actions);
216
-
217
- actionList.addValidators(new ApiError());
218
- actionList.addValidators(new SchemaValidator());
219
- actionList.addValidators(new MigrationError());
220
- actionList.addValidators(new FieldValidator());
221
-
222
- const errors = actionList.validate();
223
- errorHelper(errors);
224
- }
225
- }
226
-
227
- MigrationCommand.description = "Contentstack migration script.";
228
-
229
- MigrationCommand.flags = {
230
- "api-key": flags.string({
231
- char: "k",
232
- description: "With this flag add the API key of your stack.",
233
- dependsOn: ["authtoken"],
234
- exclusive: ["management-token-alias"],
235
- }),
236
- authtoken: flags.boolean({
237
- char: "A",
238
- description:
239
- "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.",
240
- dependsOn: ["api-key"],
241
- exclusive: ["management-token-alias"],
242
- }),
243
- "management-token-alias": flags.string({
244
- char: "a",
245
- description: "Use this flag to add the management token alias.",
246
- exclusive: ["authtoken"],
247
- }), // Add a better description
248
- filePath: flags.string({
249
- char: "n",
250
- description:
251
- "Use this flag to provide the path of the file of the migration script provided by the user.",
252
- }),
253
- branch: flags.string({
254
- char: "B",
255
- description:
256
- "Use this flag to add the branch name where you want to perform the migration.",
257
- }),
258
- "config-file": flags.string({
259
- description: "[optional] Path of the JSON configuration file",
260
- }),
261
- config: flags.string({
262
- description: "[optional] inline configuration, <key1>:<value1>",
263
- multiple: true,
264
- }),
265
- multi: flags.boolean({
266
- description:
267
- "This flag helps you to migrate multiple content files in a single instance.",
268
- }), // Add a better description
269
- };
270
-
271
- module.exports = MigrationCommand;