@contentstack/cli-cm-import 1.5.11 → 1.6.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 (110) hide show
  1. package/README.md +6 -14
  2. package/bin/dev +17 -0
  3. package/bin/dev.cmd +3 -0
  4. package/bin/run +6 -0
  5. package/bin/run.cmd +3 -0
  6. package/lib/commands/cm/stacks/import.d.ts +10 -0
  7. package/lib/commands/cm/stacks/import.js +111 -0
  8. package/lib/config/index.d.ts +3 -0
  9. package/lib/config/index.js +372 -0
  10. package/lib/import/index.d.ts +1 -0
  11. package/lib/import/index.js +8 -0
  12. package/lib/import/module-importer.d.ts +13 -0
  13. package/lib/import/module-importer.js +70 -0
  14. package/lib/import/modules/assets.d.ts +63 -0
  15. package/lib/import/modules/assets.js +265 -0
  16. package/lib/import/modules/base-class.d.ts +69 -0
  17. package/lib/import/modules/base-class.js +165 -0
  18. package/lib/import/modules/index.d.ts +2 -0
  19. package/lib/import/modules/index.js +19 -0
  20. package/lib/import/modules/locales.d.ts +31 -0
  21. package/lib/import/modules/locales.js +152 -0
  22. package/lib/import/modules-js/assets.d.ts +33 -0
  23. package/lib/import/modules-js/assets.js +415 -0
  24. package/lib/import/modules-js/content-types.d.ts +33 -0
  25. package/lib/import/modules-js/content-types.js +176 -0
  26. package/lib/import/modules-js/custom-roles.d.ts +15 -0
  27. package/lib/import/modules-js/custom-roles.js +141 -0
  28. package/lib/import/modules-js/entries.d.ts +54 -0
  29. package/lib/import/modules-js/entries.js +1260 -0
  30. package/lib/import/modules-js/environments.d.ts +13 -0
  31. package/lib/import/modules-js/environments.js +85 -0
  32. package/lib/import/modules-js/extensions.d.ts +17 -0
  33. package/lib/import/modules-js/extensions.js +86 -0
  34. package/lib/import/modules-js/global-fields.d.ts +13 -0
  35. package/lib/import/modules-js/global-fields.js +109 -0
  36. package/lib/import/modules-js/index.d.ts +1 -0
  37. package/lib/import/modules-js/index.js +33 -0
  38. package/lib/import/modules-js/labels.d.ts +20 -0
  39. package/lib/import/modules-js/labels.js +148 -0
  40. package/lib/import/modules-js/locales.d.ts +24 -0
  41. package/lib/import/modules-js/locales.js +196 -0
  42. package/lib/import/modules-js/marketplace-apps.d.ts +60 -0
  43. package/lib/import/modules-js/marketplace-apps.js +409 -0
  44. package/lib/import/modules-js/webhooks.d.ts +17 -0
  45. package/lib/import/modules-js/webhooks.js +85 -0
  46. package/lib/import/modules-js/workflows.d.ts +18 -0
  47. package/lib/import/modules-js/workflows.js +132 -0
  48. package/lib/types/default-config.d.ts +130 -0
  49. package/lib/types/default-config.js +2 -0
  50. package/lib/types/import-config.d.ts +51 -0
  51. package/lib/types/import-config.js +2 -0
  52. package/lib/types/index.d.ts +30 -0
  53. package/lib/types/index.js +2 -0
  54. package/lib/utils/asset-helper.d.ts +4 -0
  55. package/lib/utils/asset-helper.js +387 -0
  56. package/lib/utils/backup-handler.d.ts +2 -0
  57. package/lib/utils/backup-handler.js +31 -0
  58. package/lib/utils/common-helper.d.ts +20 -0
  59. package/lib/utils/common-helper.js +244 -0
  60. package/lib/utils/content-type-helper.d.ts +49 -0
  61. package/lib/utils/content-type-helper.js +143 -0
  62. package/lib/utils/entries-helper.d.ts +4 -0
  63. package/lib/utils/entries-helper.js +252 -0
  64. package/lib/utils/extension-helper.d.ts +10 -0
  65. package/lib/utils/extension-helper.js +72 -0
  66. package/lib/utils/file-helper.d.ts +14 -0
  67. package/lib/utils/file-helper.js +140 -0
  68. package/lib/utils/import-config-handler.d.ts +3 -0
  69. package/lib/utils/import-config-handler.js +73 -0
  70. package/lib/utils/index.d.ts +12 -0
  71. package/lib/utils/index.js +29 -0
  72. package/lib/utils/interactive.d.ts +2 -0
  73. package/lib/utils/interactive.js +23 -0
  74. package/lib/utils/logger.d.ts +8 -0
  75. package/lib/utils/logger.js +154 -0
  76. package/lib/utils/login-handler.d.ts +8 -0
  77. package/lib/utils/login-handler.js +53 -0
  78. package/lib/utils/marketplace-app-helper.d.ts +4 -0
  79. package/lib/utils/marketplace-app-helper.js +31 -0
  80. package/messages/index.json +1 -7
  81. package/oclif.manifest.json +2 -2
  82. package/package.json +47 -21
  83. package/src/app.js +0 -217
  84. package/src/commands/cm/stacks/import.js +0 -161
  85. package/src/config/default.js +0 -352
  86. package/src/lib/import/assets.js +0 -495
  87. package/src/lib/import/content-types.js +0 -201
  88. package/src/lib/import/custom-roles.js +0 -169
  89. package/src/lib/import/entries.js +0 -1495
  90. package/src/lib/import/environments.js +0 -106
  91. package/src/lib/import/extensions.js +0 -108
  92. package/src/lib/import/global-fields.js +0 -135
  93. package/src/lib/import/labels.js +0 -175
  94. package/src/lib/import/locales.js +0 -216
  95. package/src/lib/import/marketplace-apps.js +0 -542
  96. package/src/lib/import/webhooks.js +0 -113
  97. package/src/lib/import/workflows.js +0 -166
  98. package/src/lib/util/extensionsUidReplace.js +0 -67
  99. package/src/lib/util/fs.js +0 -124
  100. package/src/lib/util/import-flags.js +0 -187
  101. package/src/lib/util/index.js +0 -222
  102. package/src/lib/util/log.js +0 -144
  103. package/src/lib/util/login.js +0 -58
  104. package/src/lib/util/lookupReplaceAssets.js +0 -366
  105. package/src/lib/util/lookupReplaceEntries.js +0 -250
  106. package/src/lib/util/marketplace-app-helper.js +0 -31
  107. package/src/lib/util/removeReferenceFields.js +0 -59
  108. package/src/lib/util/schemaTemplate.js +0 -38
  109. package/src/lib/util/supress-mandatory-fields.js +0 -34
  110. package/src/lib/util/upload.js +0 -56
@@ -1,222 +0,0 @@
1
- /* eslint-disable no-console */
2
- /*!
3
- * Contentstack Import
4
- * Copyright (c) 2019 Contentstack LLC
5
- * MIT Licensed
6
- */
7
-
8
- const _ = require('lodash');
9
- const { HttpClient, managementSDKClient, isAuthenticated } = require('@contentstack/cli-utilities');
10
- const fs = require('./fs');
11
- const path = require('path');
12
- const chalk = require('chalk');
13
- const { addlogs } = require('./log');
14
- const defaultConfig = require('../../config/default');
15
- const promiseLimit = require('promise-limit');
16
- let config;
17
-
18
- exports.initialization = (configData) => {
19
- config = this.buildAppConfig(configData);
20
- const res = this.validateConfig(config);
21
-
22
- if ((res && res !== 'error') || res === undefined) {
23
- return config;
24
- }
25
- };
26
-
27
- exports.validateConfig = (importConfig) => {
28
- if (importConfig.email && importConfig.password && !importConfig.target_stack) {
29
- addlogs(importConfig, chalk.red('Kindly provide api_token'), 'error');
30
- return 'error';
31
- } else if (
32
- !importConfig.email &&
33
- !importConfig.password &&
34
- !importConfig.management_token &&
35
- importConfig.target_stack &&
36
- !isAuthenticated()
37
- ) {
38
- addlogs(importConfig, chalk.red('Kindly provide management_token or email and password'), 'error');
39
- return 'error';
40
- } else if (!importConfig.email && !importConfig.password && importConfig.preserveStackVersion) {
41
- addlogs(importConfig, chalk.red('Kindly provide Email and password for old version stack'), 'error');
42
- return 'error';
43
- } else if ((importConfig.email && !importConfig.password) || (!importConfig.email && importConfig.password)) {
44
- addlogs(importConfig, chalk.red('Kindly provide Email and password'), 'error');
45
- return 'error';
46
- }
47
- };
48
-
49
- exports.buildAppConfig = (importConfig) => {
50
- importConfig = _.merge(defaultConfig, importConfig);
51
- return importConfig;
52
- };
53
-
54
- exports.sanitizeStack = (importConfig) => {
55
- if (typeof importConfig.preserveStackVersion !== 'boolean' || !importConfig.preserveStackVersion) {
56
- return Promise.resolve();
57
- }
58
- addlogs(importConfig, 'Running script to maintain stack version.', 'success');
59
- try {
60
- const httpClient = HttpClient.create();
61
- httpClient.headers(importConfig.headers);
62
- return httpClient.get(`https://${importConfig.host}/v3${importConfig.apis.stacks}`).then((stackDetails) => {
63
- if (stackDetails.data && stackDetails.data.stack && stackDetails.data.stack.settings) {
64
- const newStackVersion = stackDetails.data.stack.settings.version;
65
- const newStackDate = new Date(newStackVersion).toString();
66
- const stackFilePath = path.join(
67
- importConfig.data,
68
- importConfig.modules.stack.dirName,
69
- importConfig.modules.stack.fileName,
70
- );
71
-
72
- const oldStackDetails = fs.readFileSync(stackFilePath);
73
- if (!oldStackDetails || !oldStackDetails.settings || !oldStackDetails.settings.hasOwnProperty('version')) {
74
- throw new Error(`${JSON.stringify(oldStackDetails)} is invalid!`);
75
- }
76
- const oldStackDate = new Date(oldStackDetails.settings.version).toString();
77
-
78
- if (oldStackDate > newStackDate) {
79
- throw new Error(
80
- 'Migration Error. You cannot migrate data from new stack onto old. Kindly contact support@contentstack.com for more details.',
81
- );
82
- } else if (oldStackDate === newStackDate) {
83
- addlogs(importConfig, 'The version of both the stacks are same.', 'success');
84
- return Promise.resolve();
85
- }
86
- addlogs(importConfig, 'Updating stack version.', 'success');
87
-
88
- return httpClient
89
- .put(`https://${importConfig.host}/v3${importConfig.apis.stacks}settings/set-version`, {
90
- stack_settings: {
91
- version: '2017-10-14', // This can be used as a variable
92
- },
93
- })
94
- .then((response) => {
95
- addlogs(importConfig, `Stack version preserved successfully!\n${JSON.stringify(response.data)}`, 'success');
96
- });
97
- }
98
- throw new Error(`Unexpected stack details ${stackDetails && JSON.stringify(stackDetails.data)}`);
99
- });
100
- } catch (error) {
101
- console.log(error);
102
- }
103
- };
104
-
105
- exports.masterLocalDetails = (stackAPIClient) => {
106
- return new Promise((resolve, reject) => {
107
- const result = stackAPIClient.locale().query();
108
- result
109
- .find()
110
- .then((response) => {
111
- const masterLocalObj = response.items.filter((obj) => {
112
- if (obj.fallback_locale === null) {
113
- return obj;
114
- }
115
- });
116
- return resolve(masterLocalObj[0]);
117
- })
118
- .catch((error) => {
119
- return reject(error);
120
- });
121
- });
122
- };
123
-
124
- exports.field_rules_update = (importConfig, ctPath) => {
125
- return new Promise((resolve, reject) => {
126
- managementSDKClient(config)
127
- .then((APIClient) => {
128
- fs.readFileSync(path.join(ctPath + '/field_rules_uid.json'), async (err, data) => {
129
- if (err) {
130
- throw err;
131
- }
132
- const ct_field_visibility_uid = JSON.parse(data);
133
- let ct_files = fs.readdirSync(ctPath);
134
- if (ct_field_visibility_uid && ct_field_visibility_uid != 'undefined') {
135
- for (const ele of ct_field_visibility_uid) {
136
- if (ct_files.indexOf(ele + '.json') > -1) {
137
- let schema = require(path.resolve(ctPath, ele));
138
- // await field_rules_update(schema)
139
- let fieldRuleLength = schema.field_rules.length;
140
- for (let k = 0; k < fieldRuleLength; k++) {
141
- let fieldRuleConditionLength = schema.field_rules[k].conditions.length;
142
- for (let i = 0; i < fieldRuleConditionLength; i++) {
143
- if (schema.field_rules[k].conditions[i].operand_field === 'reference') {
144
- let entryMapperPath = path.resolve(importConfig.data, 'mapper', 'entries');
145
- let entryUidMapperPath = path.join(entryMapperPath, 'uid-mapping.json');
146
- let fieldRulesValue = schema.field_rules[k].conditions[i].value;
147
- let fieldRulesArray = fieldRulesValue.split('.');
148
- let updatedValue = [];
149
- for (const element of fieldRulesArray) {
150
- let splitedFieldRulesValue = element;
151
- let oldUid = helper.readFileSync(path.join(entryUidMapperPath));
152
- if (oldUid.hasOwnProperty(splitedFieldRulesValue)) {
153
- updatedValue.push(oldUid[splitedFieldRulesValue]);
154
- } else {
155
- updatedValue.push(element);
156
- }
157
- }
158
- schema.field_rules[k].conditions[i].value = updatedValue.join('.');
159
- }
160
- }
161
- }
162
- const stackAPIClient = APIClient.stack({
163
- api_key: importConfig.target_stack,
164
- management_token: importConfig.management_token,
165
- });
166
- let ctObj = stackAPIClient.contentType(schema.uid);
167
- Object.assign(ctObj, _.cloneDeep(schema));
168
- ctObj
169
- .update()
170
- .then(() => {
171
- return resolve();
172
- })
173
- .catch((error) => {
174
- return reject(error);
175
- });
176
- }
177
- }
178
- }
179
- });
180
- })
181
- .catch(reject);
182
- });
183
- };
184
-
185
- exports.getConfig = () => {
186
- return config;
187
- };
188
-
189
- exports.formatError = (error) => {
190
- try {
191
- if (typeof error === 'string') {
192
- error = JSON.parse(error);
193
- } else {
194
- error = JSON.parse(error.message);
195
- }
196
- } catch (e) {}
197
- let message = error.errorMessage || error.error_message || error.message || error;
198
- if (error.errors && Object.keys(error.errors).length > 0) {
199
- Object.keys(error.errors).forEach((e) => {
200
- let entity = e;
201
- if (e === 'authorization') entity = 'Management Token';
202
- if (e === 'api_key') entity = 'Stack API key';
203
- if (e === 'uid') entity = 'Content Type';
204
- if (e === 'access_token') entity = 'Delivery Token';
205
- message += ' ' + [entity, error.errors[e]].join(' ');
206
- });
207
- }
208
- return message;
209
- };
210
-
211
- exports.executeTask = (handler, options, tasks = []) => {
212
- if (typeof handler !== 'function') {
213
- throw new Error('Invalid handler');
214
- }
215
- const { concurrency = 1 } = options;
216
- const limit = promiseLimit(concurrency);
217
- return Promise.all(
218
- tasks.map((task) => {
219
- return limit(() => handler(task));
220
- }),
221
- );
222
- };
@@ -1,144 +0,0 @@
1
- /*!
2
- * Contentstack Import
3
- * Copyright (c) 2019 Contentstack LLC
4
- * MIT Licensed
5
- */
6
-
7
- var winston = require('winston');
8
- var path = require('path');
9
- var mkdirp = require('mkdirp');
10
- var slice = Array.prototype.slice;
11
-
12
- const ansiRegexPattern = [
13
- '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
14
- '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))',
15
- ].join('|');
16
-
17
- function returnString(args) {
18
- var returnStr = '';
19
- if (args && args.length) {
20
- returnStr = args
21
- .map(function (item) {
22
- if (item && typeof item === 'object') {
23
- try {
24
- return JSON.stringify(item).replace(/authtoken\":\"blt................/g, 'authtoken":"blt....');
25
- } catch (error) {
26
- return item.message;
27
- }
28
- }
29
- return item;
30
- })
31
- .join(' ')
32
- .trim();
33
- }
34
- returnStr = returnStr.replace(new RegExp(ansiRegexPattern, 'g'), '').trim();
35
- return returnStr;
36
- }
37
-
38
- var myCustomLevels = {
39
- levels: {
40
- warn: 1,
41
- info: 2,
42
- debug: 3,
43
- },
44
- colors: {
45
- info: 'blue',
46
- debug: 'green',
47
- warn: 'yellow',
48
- error: 'red',
49
- },
50
- };
51
-
52
- let logger;
53
- let errorLogger;
54
-
55
- let successTransport;
56
- let errorTransport;
57
-
58
- // removed logfileName from arguments
59
- function init(_logPath) {
60
- if (!logger || !errorLogger) {
61
- var logsDir = path.resolve(_logPath, 'logs', 'import');
62
- // Create dir if doesn't already exist
63
- mkdirp.sync(logsDir);
64
-
65
- successTransport = {
66
- filename: path.join(logsDir, 'success.log'),
67
- maxFiles: 20,
68
- maxsize: 1000000,
69
- tailable: true,
70
- json: true,
71
- level: 'info',
72
- };
73
-
74
- errorTransport = {
75
- filename: path.join(logsDir, 'error.log'),
76
- maxFiles: 20,
77
- maxsize: 1000000,
78
- tailable: true,
79
- json: true,
80
- level: 'error',
81
- };
82
-
83
- logger = winston.createLogger({
84
- transports: [
85
- new winston.transports.File(successTransport),
86
- new winston.transports.Console({ format: winston.format.simple() }),
87
- ],
88
- levels: myCustomLevels.levels,
89
- });
90
-
91
- errorLogger = winston.createLogger({
92
- transports: [
93
- new winston.transports.File(errorTransport),
94
- new winston.transports.Console({
95
- level: 'error',
96
- format: winston.format.combine(winston.format.colorize({ all: true, colors:{ error: 'red'} }), winston.format.simple()),
97
- }),
98
- ],
99
- levels: { error: 0 },
100
- });
101
- }
102
-
103
- return {
104
- log: function () {
105
- var args = slice.call(arguments);
106
- var logString = returnString(args);
107
- if (logString) {
108
- logger.log('info', logString);
109
- }
110
- },
111
- warn: function () {
112
- var args = slice.call(arguments);
113
- var logString = returnString(args);
114
- if (logString) {
115
- logger.log('warn', logString);
116
- }
117
- },
118
- error: function () {
119
- var args = slice.call(arguments);
120
- var logString = returnString(args);
121
- if (logString) {
122
- errorLogger.log('error', logString);
123
- }
124
- },
125
- debug: function () {
126
- var args = slice.call(arguments);
127
- var logString = returnString(args);
128
- if (logString) {
129
- logger.log('debug', logString);
130
- }
131
- },
132
- };
133
- }
134
-
135
- exports.addlogs = async (config, message, type) => {
136
- var configLogPath = config.source_stack && config.target_stack ? config.data : config.oldPath;
137
- // ignoring the type argument, as we are not using it to create a logfile anymore
138
- if (type !== 'error') {
139
- // removed type argument from init method
140
- init(configLogPath).log(message);
141
- } else {
142
- init(configLogPath).error(message);
143
- }
144
- };
@@ -1,58 +0,0 @@
1
- /* eslint-disable no-console */
2
- /*!
3
- * Contentstack Import
4
- * Copyright (c) 2019 Contentstack LLC
5
- * MIT Licensed
6
- */
7
-
8
- const chalk = require('chalk');
9
-
10
- const { addlogs } = require('../util/log');
11
- const { managementSDKClient, isAuthenticated } = require('@contentstack/cli-utilities');
12
-
13
- module.exports = (config) => {
14
- return new Promise((resolve, reject) => {
15
- managementSDKClient(config)
16
- .then((APIClient) => {
17
- // eslint-disable-next-line no-console
18
- if (config.email && config.password) {
19
- console.log('Logging into Contentstack');
20
- return APIClient.login({ email: config.email, password: config.password })
21
- .then((response) => {
22
- // eslint-disable-next-line no-console
23
- console.log(chalk.green('Contentstack account authenticated successfully!'));
24
- config.headers = {
25
- api_key: config.target_stack,
26
- authtoken: response.user.authtoken,
27
- 'X-User-Agent': 'contentstack-import/v',
28
- };
29
- return resolve(config);
30
- })
31
- .catch(reject);
32
- } else if (config.management_token) {
33
- return resolve();
34
- } else if (isAuthenticated()) {
35
- const stackAPIClient = APIClient.stack({
36
- api_key: config.target_stack,
37
- management_token: config.management_token,
38
- });
39
- stackAPIClient
40
- .fetch()
41
- .then((stack) => {
42
- config.destinationStackName = stack.name;
43
- return resolve();
44
- })
45
- .catch((error) => {
46
- let errorstack_key = error.errors.api_key;
47
- if (error.errors.api_key) {
48
- addlogs(config, chalk.red('Stack Api key ' + errorstack_key[0], 'Please enter valid Key'), 'error');
49
- return reject(error);
50
- }
51
- addlogs(config, error.errorMessage, 'error');
52
- return reject(error);
53
- });
54
- }
55
- })
56
- .catch((error) => reject(error));
57
- });
58
- };