@contentstack/cli-cm-import 1.5.11 → 1.7.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 (126) 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 +395 -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 +70 -0
  17. package/lib/import/modules/base-class.js +218 -0
  18. package/lib/import/modules/content-types.d.ts +56 -0
  19. package/lib/import/modules/content-types.js +186 -0
  20. package/lib/import/modules/custom-roles.d.ts +37 -0
  21. package/lib/import/modules/custom-roles.js +171 -0
  22. package/lib/import/modules/environments.d.ts +27 -0
  23. package/lib/import/modules/environments.js +106 -0
  24. package/lib/import/modules/extensions.d.ts +27 -0
  25. package/lib/import/modules/extensions.js +106 -0
  26. package/lib/import/modules/global-fields.d.ts +34 -0
  27. package/lib/import/modules/global-fields.js +99 -0
  28. package/lib/import/modules/index.d.ts +2 -0
  29. package/lib/import/modules/index.js +19 -0
  30. package/lib/import/modules/labels.d.ts +34 -0
  31. package/lib/import/modules/labels.js +171 -0
  32. package/lib/import/modules/locales.d.ts +31 -0
  33. package/lib/import/modules/locales.js +144 -0
  34. package/lib/import/modules/marketplace-apps.d.ts +51 -0
  35. package/lib/import/modules/marketplace-apps.js +297 -0
  36. package/lib/import/modules/webhooks.d.ts +27 -0
  37. package/lib/import/modules/webhooks.js +110 -0
  38. package/lib/import/modules-js/assets.d.ts +33 -0
  39. package/lib/import/modules-js/assets.js +415 -0
  40. package/lib/import/modules-js/content-types.d.ts +33 -0
  41. package/lib/import/modules-js/content-types.js +176 -0
  42. package/lib/import/modules-js/custom-roles.d.ts +15 -0
  43. package/lib/import/modules-js/custom-roles.js +143 -0
  44. package/lib/import/modules-js/entries.d.ts +54 -0
  45. package/lib/import/modules-js/entries.js +1260 -0
  46. package/lib/import/modules-js/environments.d.ts +13 -0
  47. package/lib/import/modules-js/environments.js +85 -0
  48. package/lib/import/modules-js/extensions.d.ts +18 -0
  49. package/lib/import/modules-js/extensions.js +86 -0
  50. package/lib/import/modules-js/global-fields.d.ts +13 -0
  51. package/lib/import/modules-js/global-fields.js +109 -0
  52. package/lib/import/modules-js/index.d.ts +1 -0
  53. package/lib/import/modules-js/index.js +33 -0
  54. package/lib/import/modules-js/labels.d.ts +20 -0
  55. package/lib/import/modules-js/labels.js +148 -0
  56. package/lib/import/modules-js/locales.d.ts +24 -0
  57. package/lib/import/modules-js/locales.js +196 -0
  58. package/lib/import/modules-js/marketplace-apps.d.ts +60 -0
  59. package/lib/import/modules-js/marketplace-apps.js +409 -0
  60. package/lib/import/modules-js/webhooks.d.ts +17 -0
  61. package/lib/import/modules-js/webhooks.js +85 -0
  62. package/lib/import/modules-js/workflows.d.ts +18 -0
  63. package/lib/import/modules-js/workflows.js +132 -0
  64. package/lib/types/default-config.d.ts +143 -0
  65. package/lib/types/default-config.js +2 -0
  66. package/lib/types/import-config.d.ts +52 -0
  67. package/lib/types/import-config.js +2 -0
  68. package/lib/types/index.d.ts +63 -0
  69. package/lib/types/index.js +4 -0
  70. package/lib/utils/asset-helper.d.ts +4 -0
  71. package/lib/utils/asset-helper.js +387 -0
  72. package/lib/utils/backup-handler.d.ts +2 -0
  73. package/lib/utils/backup-handler.js +31 -0
  74. package/lib/utils/common-helper.d.ts +20 -0
  75. package/lib/utils/common-helper.js +244 -0
  76. package/lib/utils/content-type-helper.d.ts +51 -0
  77. package/lib/utils/content-type-helper.js +145 -0
  78. package/lib/utils/entries-helper.d.ts +4 -0
  79. package/lib/utils/entries-helper.js +252 -0
  80. package/lib/utils/extension-helper.d.ts +5 -0
  81. package/lib/utils/extension-helper.js +84 -0
  82. package/lib/utils/file-helper.d.ts +14 -0
  83. package/lib/utils/file-helper.js +140 -0
  84. package/lib/utils/import-config-handler.d.ts +3 -0
  85. package/lib/utils/import-config-handler.js +73 -0
  86. package/lib/utils/index.d.ts +12 -0
  87. package/lib/utils/index.js +39 -0
  88. package/lib/utils/interactive.d.ts +7 -0
  89. package/lib/utils/interactive.js +88 -0
  90. package/lib/utils/logger.d.ts +8 -0
  91. package/lib/utils/logger.js +154 -0
  92. package/lib/utils/login-handler.d.ts +8 -0
  93. package/lib/utils/login-handler.js +53 -0
  94. package/lib/utils/marketplace-app-helper.d.ts +16 -0
  95. package/lib/utils/marketplace-app-helper.js +143 -0
  96. package/messages/index.json +1 -7
  97. package/oclif.manifest.json +2 -2
  98. package/package.json +46 -20
  99. package/src/app.js +0 -217
  100. package/src/commands/cm/stacks/import.js +0 -161
  101. package/src/config/default.js +0 -352
  102. package/src/lib/import/assets.js +0 -495
  103. package/src/lib/import/content-types.js +0 -201
  104. package/src/lib/import/custom-roles.js +0 -169
  105. package/src/lib/import/entries.js +0 -1495
  106. package/src/lib/import/environments.js +0 -106
  107. package/src/lib/import/extensions.js +0 -108
  108. package/src/lib/import/global-fields.js +0 -135
  109. package/src/lib/import/labels.js +0 -175
  110. package/src/lib/import/locales.js +0 -216
  111. package/src/lib/import/marketplace-apps.js +0 -542
  112. package/src/lib/import/webhooks.js +0 -113
  113. package/src/lib/import/workflows.js +0 -166
  114. package/src/lib/util/extensionsUidReplace.js +0 -67
  115. package/src/lib/util/fs.js +0 -124
  116. package/src/lib/util/import-flags.js +0 -187
  117. package/src/lib/util/index.js +0 -222
  118. package/src/lib/util/log.js +0 -144
  119. package/src/lib/util/login.js +0 -58
  120. package/src/lib/util/lookupReplaceAssets.js +0 -366
  121. package/src/lib/util/lookupReplaceEntries.js +0 -250
  122. package/src/lib/util/marketplace-app-helper.js +0 -31
  123. package/src/lib/util/removeReferenceFields.js +0 -59
  124. package/src/lib/util/schemaTemplate.js +0 -38
  125. package/src/lib/util/supress-mandatory-fields.js +0 -34
  126. package/src/lib/util/upload.js +0 -56
package/src/app.js DELETED
@@ -1,217 +0,0 @@
1
- /*!
2
- * Contentstack Import
3
- * Copyright (c) 2019 Contentstack LLC
4
- * MIT Licensed
5
- */
6
-
7
- const fs = require('fs');
8
- const os = require('os');
9
- const path = require('path');
10
- const chalk = require('chalk');
11
- const { copy, copySync, removeSync } = require('fs-extra');
12
- const util = require('./lib/util/index');
13
- const login = require('./lib/util/login');
14
- const { addlogs } = require('./lib/util/log');
15
- const { managementSDKClient, isAuthenticated } = require('@contentstack/cli-utilities');
16
- const { camelCase } = require('lodash')
17
-
18
- exports.initial = (configData) => {
19
- return new Promise(async (resolve, reject) => {
20
- const config = util.initialization(configData);
21
- config.oldPath = config.data;
22
- const APIClient = await managementSDKClient(config);
23
- const stackAPIClient = APIClient.stack({ api_key: config.target_stack, management_token: config.management_token });
24
-
25
- if (configData.branchName) {
26
- await validateIfBranchExist(stackAPIClient, configData, configData.branchName).catch(() => {
27
- process.exit();
28
- });
29
- }
30
-
31
- const backupAndImportData = async (APIClient, stackAPIClient) => {
32
- if (fs.existsSync(config.data)) {
33
- let migrationBackupDirPath = path.join(process.cwd(), '_backup_' + Math.floor(Math.random() * 1000));
34
- return createBackup(migrationBackupDirPath, config)
35
- .then((basePath) => {
36
- config.data = basePath;
37
- return util.sanitizeStack(config);
38
- })
39
- .then(() => {
40
- let importRes;
41
- const types = config.modules.types;
42
-
43
- if (config.moduleName) {
44
- importRes = singleImport(APIClient, stackAPIClient, config.moduleName, types, config);
45
- } else {
46
- importRes = allImport(APIClient, stackAPIClient, config, types);
47
- }
48
-
49
- importRes.then(resolve).catch(reject);
50
- })
51
- .catch((error) => {
52
- addlogs(config, `Failed to import contents. ${util.formatError(error)}`, 'error');
53
- reject(error);
54
- process.exit(1);
55
- });
56
- } else {
57
- let filename = path.basename(config.data);
58
- addlogs(config, chalk.red(`'${filename}' Folder does not exist'`), 'error');
59
- }
60
- };
61
-
62
- if (config) {
63
- if (config.management_token || config.isAuthenticated) {
64
- await backupAndImportData(APIClient, stackAPIClient);
65
- } else if ((config.email && config.password) || isAuthenticated()) {
66
- login(config).then(backupAndImportData(APIClient, stackAPIClient)).catch(reject);
67
- } else if (config.email && config.password) {
68
- login(config)
69
- .then(backupAndImportData.apply(null, [APIClient, stackAPIClient]))
70
- .catch(reject);
71
- } else {
72
- reject('Kindly login or provide management_token');
73
- }
74
- }
75
- });
76
- };
77
-
78
- let singleImport = async (APIClient, stackAPIClient, moduleName, types, config) => {
79
- try {
80
- if (types.indexOf(moduleName) > -1) {
81
- if (!config.master_locale) {
82
- try {
83
- let masterLocalResponse = await util.masterLocalDetails(stackAPIClient);
84
- let master_locale = { code: masterLocalResponse.code };
85
- config['master_locale'] = master_locale;
86
- } catch (error) {
87
- addlogs(config, `Failed to get master locale detail from the stack ${util.formatError(error)}`, 'error');
88
- }
89
- }
90
- let ImportModule = require('./lib/import/' + moduleName);
91
- const importResponse = await new ImportModule(config, stackAPIClient, APIClient).start();
92
- if (moduleName === 'content-types') {
93
- let ctPath = path.resolve(config.data, config.modules.content_types.dirName);
94
- let fieldPath = path.join(ctPath + '/field_rules_uid.json');
95
- if (fieldPath) {
96
- await util.field_rules_update(config, ctPath);
97
- }
98
- }
99
- if (!(importResponse && importResponse.empty)) {
100
- addlogs(config, `Module '${moduleName}' imported successfully!`, 'success');
101
- }
102
- addlogs(config, 'The log for this is stored at ' + path.join(config.oldPath, 'logs', 'import'), 'success');
103
- return true;
104
- } else {
105
- addlogs(config, 'Please provide valid module name.', 'error');
106
- }
107
- } catch (error) {
108
- addlogs(config, `Failed to migrate '${moduleName}'`, 'error');
109
- addlogs(config, util.formatError(error), 'error');
110
- addlogs(config, `The log for this is stored at '${path.join(config.oldPath, 'logs', 'import')}'`, 'error');
111
- }
112
- };
113
-
114
- let allImport = async (APIClient, stackAPIClient, config, types) => {
115
- try {
116
- for (let i = 0; i < types.length; i++) {
117
- let type = types[i];
118
- if (i === 0 && !config.master_locale) {
119
- let masterLocalResponse = await util.masterLocalDetails(stackAPIClient);
120
- let master_locale = { code: masterLocalResponse.code };
121
- config['master_locale'] = master_locale;
122
- }
123
- let ImportModule = require('./lib/import/' + type);
124
- await new ImportModule(config, stackAPIClient, APIClient).start(config);
125
- }
126
- if (config.target_stack && config.source_stack) {
127
- addlogs(
128
- config,
129
- chalk.green(
130
- 'The data of the ' +
131
- (config.sourceStackName || config.source_stack) +
132
- ' stack has been imported into ' +
133
- (config.destinationStackName || config.target_stack) +
134
- ' stack successfully!',
135
- ),
136
- 'success',
137
- );
138
- addlogs(config, 'The log for this is stored at ' + path.join(config.data, 'logs', 'import'), 'success');
139
- } else {
140
- addlogs(
141
- config,
142
- chalk.green(
143
- 'Data has been imported to stack ' + (config.destinationStackName || config.target_stack) + ' succesfully!',
144
- ),
145
- 'success',
146
- );
147
- addlogs(config, 'The log for this is stored at ' + path.join(config.oldPath, 'logs', 'import'), 'success');
148
- }
149
- return true;
150
- } catch (error) {
151
- addlogs(
152
- config,
153
- `Failed to migrate stack '${(config.destinationStackName || config.target_stack)}'. Please check error logs for more info`,
154
- 'error',
155
- );
156
- addlogs(config, util.formatError(error), 'error');
157
- addlogs(config, `The log for this is stored at '${path.join(config.oldPath, 'logs', 'import')}'`, 'error');
158
- }
159
- };
160
-
161
- const createBackup = (backupDirPath, config) => {
162
- return new Promise((resolve, reject) => {
163
- if (config.hasOwnProperty('useBackedupDir') && fs.existsSync(config.useBackedupDir)) {
164
- return resolve(config.useBackedupDir);
165
- }
166
-
167
- if (path.isAbsolute(config.data)) {
168
- copy(config.data, backupDirPath, (error) => {
169
- if (error) {
170
- return reject(error);
171
- }
172
- return resolve(backupDirPath);
173
- });
174
- } else {
175
- //handle mac error :- Cannot copy to a subdirectory of itself
176
- if (config.data === "." || config.data === "./") {
177
- const tempDestination = `${os.platform() === 'darwin' ? '/private/tmp' : '/tmp'}/${camelCase(backupDirPath)}`;
178
- copySync(config.data, tempDestination);
179
- copySync(tempDestination, backupDirPath);
180
- removeSync(tempDestination);
181
- return resolve(backupDirPath);
182
- } else {
183
- copy(config.data, backupDirPath,(error) => {
184
- if (error) {
185
- return reject(error);
186
- }
187
- return resolve(backupDirPath);
188
- });
189
- }
190
- }
191
- });
192
- };
193
-
194
- const validateIfBranchExist = async (stackAPIClient, config, branch) => {
195
- return new Promise(async (resolve, reject) => {
196
- try {
197
- const data = await stackAPIClient
198
- .branch(branch)
199
- .fetch()
200
- .catch((_err) => {});
201
- if (data && typeof data === 'object') {
202
- if (data.error_message) {
203
- addlogs(config, data.error_message, 'error');
204
- addlogs(config, `No branch found with the name '${branch}`, 'error');
205
- reject({ message: 'No branch found with the name ' + branch, error: error_message });
206
- } else {
207
- resolve(data);
208
- }
209
- } else {
210
- reject({ message: 'No branch found with the name ' + branch, error: {} });
211
- }
212
- } catch (error) {
213
- addlogs(config, `No branch found with the name '${branch}`, 'error');
214
- reject({ message: 'No branch found with the name ' + branch, error });
215
- }
216
- });
217
- };
@@ -1,161 +0,0 @@
1
- const _ = require('lodash');
2
- const defaultConfig = require('../../../config/default');
3
- const { Command } = require('@contentstack/cli-command');
4
- const { flags, printFlagDeprecation, isAuthenticated, cliux } = require('@contentstack/cli-utilities');
5
- const {
6
- configWithMToken,
7
- parameterWithMToken,
8
- withoutParameterMToken,
9
- configWithAuthToken,
10
- parametersWithAuthToken,
11
- withoutParametersWithAuthToken,
12
- } = require('../../../lib/util/import-flags');
13
-
14
- class ImportCommand extends Command {
15
- async run() {
16
- let self = this;
17
- const { flags: importCommandFlags } = await self.parse(ImportCommand);
18
- const extConfig = importCommandFlags.config;
19
- let targetStack = importCommandFlags['stack-uid'] || importCommandFlags['stack-api-key'];
20
- const data = importCommandFlags.data || importCommandFlags['data-dir'];
21
- const moduleName = importCommandFlags.module;
22
- const backupdir = importCommandFlags['backup-dir'];
23
- const alias = importCommandFlags['alias'] || importCommandFlags['management-token-alias'];
24
- importCommandFlags.branchName = importCommandFlags.branch;
25
- importCommandFlags.importWebhookStatus = importCommandFlags['import-webhook-status'];
26
- delete importCommandFlags.branch;
27
- delete importCommandFlags['import-webhook-status'];
28
- let host = self.cmaHost;
29
- importCommandFlags['isAuthenticated'] = isAuthenticated();
30
-
31
- return new Promise((resolve, reject) => {
32
- if (data) {
33
- defaultConfig.data = data;
34
- }
35
-
36
- defaultConfig.forceStopMarketplaceAppsPrompt = importCommandFlags.yes;
37
-
38
- if (alias) {
39
- let managementTokens = self.getToken(alias);
40
-
41
- if (managementTokens) {
42
- let result;
43
-
44
- if ((extConfig && isAuthenticated()) || alias) {
45
- result = configWithMToken(extConfig, managementTokens, moduleName, host, backupdir, importCommandFlags);
46
- } else if (data) {
47
- result = parameterWithMToken(managementTokens, data, moduleName, host, backupdir, importCommandFlags);
48
- } else {
49
- result = withoutParameterMToken(managementTokens, moduleName, host, backupdir, importCommandFlags);
50
- }
51
-
52
- result.then(resolve).catch(reject);
53
- } else {
54
- cliux.print(`error: management Token is not present please add managment token first`, {color: 'red'});
55
- }
56
- } else if (isAuthenticated()) {
57
- let result;
58
-
59
- if (extConfig) {
60
- result = configWithAuthToken(extConfig, moduleName, host, backupdir, importCommandFlags);
61
- } else if (targetStack && data) {
62
- result = parametersWithAuthToken(targetStack, data, moduleName, host, backupdir, importCommandFlags);
63
- } else {
64
- result = withoutParametersWithAuthToken(moduleName, host, backupdir, importCommandFlags);
65
- }
66
-
67
- result.then(resolve).catch(reject);
68
- } else {
69
- cliux.print(`error: Login or provide the alias for management token`, {color: 'red'});
70
- }
71
- });
72
- }
73
- }
74
-
75
- ImportCommand.description = `Import script for importing the content into the new stack
76
- ...
77
- Once you export content from the source stack, import it to your destination stack by using the cm:stacks:import command.
78
- `;
79
- ImportCommand.examples = [
80
- `csdx cm:stacks:import --stack-api-key <stack_api_key> --data-dir <path/of/export/destination/dir>`,
81
- `csdx cm:stacks:import --config <path/of/config/dir>`,
82
- `csdx cm:stacks:import --module <single module name>`,
83
- `csdx cm:stacks:import --module <single module name> --backup-dir <backup dir>`,
84
- `csdx cm:stacks:import --alias <management_token_alias>`,
85
- `csdx cm:stacks:import --alias <management_token_alias> --data-dir <path/of/export/destination/dir>`,
86
- `csdx cm:stacks:import --alias <management_token_alias> --config <path/of/config/file>`,
87
- `csdx cm:stacks:import --branch <branch name> --yes`,
88
- ];
89
- ImportCommand.flags = {
90
- config: flags.string({
91
- char: 'c',
92
- description: '[optional] path of config file',
93
- }),
94
- 'stack-uid': flags.string({
95
- char: 's',
96
- description: 'API key of the target stack',
97
- hidden: true,
98
- parse: printFlagDeprecation(['-s', '--stack-uid'], ['-k', '--stack-api-key']),
99
- }),
100
- 'stack-api-key': flags.string({
101
- char: 'k',
102
- description: 'API key of the target stack',
103
- }),
104
- data: flags.string({
105
- description: 'path and location where data is stored',
106
- hidden: true,
107
- parse: printFlagDeprecation(['--data'], ['--data-dir']),
108
- }),
109
- 'data-dir': flags.string({
110
- char: 'd',
111
- description: 'path and location where data is stored',
112
- }),
113
- alias: flags.string({
114
- char: 'a',
115
- description: 'alias of the management token',
116
- }),
117
- 'management-token-alias': flags.string({
118
- description: 'alias of the management token',
119
- hidden: true,
120
- parse: printFlagDeprecation(['--management-token-alias'], ['-a', '--alias']),
121
- }),
122
- 'auth-token': flags.boolean({
123
- char: 'A',
124
- description: 'to use auth token',
125
- hidden: true,
126
- parse: printFlagDeprecation(['-A', '--auth-token']),
127
- }),
128
- module: flags.string({
129
- char: 'm',
130
- description: '[optional] specific module name',
131
- parse: printFlagDeprecation(['-m'], ['--module']),
132
- }),
133
- 'backup-dir': flags.string({
134
- char: 'b',
135
- description: '[optional] backup directory name when using specific module',
136
- parse: printFlagDeprecation(['-b'], ['--backup-dir']),
137
- }),
138
- branch: flags.string({
139
- char: 'B',
140
- description: '[optional] branch name',
141
- parse: printFlagDeprecation(['-B'], ['--branch']),
142
- }),
143
- 'import-webhook-status': flags.string({
144
- description: '[optional] Webhook state',
145
- options: ['disable', 'current'],
146
- required: false,
147
- default: 'disable',
148
- }),
149
- yes: flags.boolean({
150
- char: 'y',
151
- required: false,
152
- description: '[optional] Override marketplace prompts',
153
- }),
154
- };
155
-
156
- ImportCommand.aliases = ['cm:import'];
157
-
158
- ImportCommand.usage =
159
- 'cm:stacks:import [-c <value>] [-k <value>] [-d <value>] [-a <value>] [--module <value>] [--backup-dir <value>] [--branch <value>] [--import-webhook-status disable|current]';
160
-
161
- module.exports = ImportCommand;