@contentstack/cli-cm-export 1.4.0 → 1.5.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.
package/README.md CHANGED
@@ -38,7 +38,7 @@ $ npm install -g @contentstack/cli-cm-export
38
38
  $ csdx COMMAND
39
39
  running command...
40
40
  $ csdx (--version)
41
- @contentstack/cli-cm-export/1.4.0 linux-x64 node-v16.19.1
41
+ @contentstack/cli-cm-export/1.5.1 linux-x64 node-v16.20.0
42
42
  $ csdx --help [COMMAND]
43
43
  USAGE
44
44
  $ csdx COMMAND
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.4.0",
2
+ "version": "1.5.1",
3
3
  "commands": {
4
4
  "cm:stacks:export": {
5
5
  "id": "cm:stacks:export",
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@contentstack/cli-cm-export",
3
3
  "description": "Contentstack CLI plugin to export content from stack",
4
- "version": "1.4.0",
4
+ "version": "1.5.1",
5
5
  "author": "Contentstack",
6
6
  "bugs": "https://github.com/contentstack/cli/issues",
7
7
  "dependencies": {
8
- "@contentstack/cli-command": "^1.2.0",
9
- "@contentstack/cli-utilities": "^1.2.0",
8
+ "@contentstack/cli-command": "^1.2.2",
9
+ "@contentstack/cli-utilities": "^1.3.1",
10
10
  "@oclif/command": "^1.8.16",
11
11
  "@oclif/config": "^1.18.3",
12
12
  "async": "^3.2.4",
@@ -23,8 +23,8 @@
23
23
  "winston": "^3.7.2"
24
24
  },
25
25
  "devDependencies": {
26
- "@contentstack/cli-auth": "^1.2.0",
27
- "@contentstack/cli-config": "^1.2.0",
26
+ "@contentstack/cli-auth": "^1.3.1",
27
+ "@contentstack/cli-config": "^1.3.1",
28
28
  "@contentstack/cli-dev-dependencies": "^1.2.0",
29
29
  "@oclif/plugin-help": "^5.1.19",
30
30
  "@oclif/test": "^1.2.6",
@@ -62,17 +62,24 @@
62
62
  "test": "nyc mocha --forbid-only \"test/**/*.test.js\"",
63
63
  "version": "oclif readme && git add README.md",
64
64
  "clean": "rm -rf ./node_modules tsconfig.build.tsbuildinfo",
65
- "test:integration": "mocha --forbid-only \"test/run.test.js\" --integration-test",
65
+ "test:integration": "INTEGRATION_TEST=true mocha --config ./test/.mocharc.js --forbid-only \"test/run.test.js\"",
66
66
  "test:unit": "mocha --forbid-only \"test/unit/*.test.js\" --unit-test"
67
67
  },
68
68
  "oclif": {
69
69
  "commands": "./src/commands",
70
70
  "bin": "csdx",
71
+ "devPlugins": [
72
+ "@oclif/plugin-help"
73
+ ],
71
74
  "repositoryPrefix": "<%- repo %>/blob/main/packages/contentstack-export/<%- commandPath %>"
72
75
  },
73
76
  "csdxConfig": {
74
77
  "expiredCommands": {
75
78
  "cm:export": "csdx cm:stacks:export"
79
+ },
80
+ "shortCommandName": {
81
+ "cm:stacks:export": "EXPRT",
82
+ "cm:export": "O-EXPRT"
76
83
  }
77
84
  },
78
85
  "main": "./src/commands/cm/stacks/export.js",
package/src/app.js CHANGED
@@ -6,7 +6,7 @@ const util = require('./lib/util');
6
6
  const login = require('./lib/util/login');
7
7
  const setupBranches = require('./lib/util/setup-branches');
8
8
  const { addlogs, unlinkFileLogger } = require('./lib/util/log');
9
- const { managementSDKClient } = require('@contentstack/cli-utilities');
9
+ const { managementSDKClient, isAuthenticated } = require('@contentstack/cli-utilities');
10
10
 
11
11
  exports.initial = async (config) => {
12
12
  return new Promise(async (resolve, reject) => {
@@ -52,15 +52,21 @@ exports.initial = async (config) => {
52
52
  }
53
53
  };
54
54
 
55
- // try {
56
- if (
55
+ if (config.management_token || config.isAuthenticated) {
56
+ try {
57
+ await fetchBranchAndExport(APIClient, stackAPIClient);
58
+ } catch (error) {
59
+ addlogs(config, `${util.formatError(error)}`, 'error');
60
+ }
61
+ resolve();
62
+ } else if (
57
63
  (config.email && config.password) ||
58
64
  (!config.email && !config.password && config.source_stack && config.access_token) ||
59
- (config.auth_token && !config.management_token)
65
+ (isAuthenticated() && !config.management_token)
60
66
  ) {
61
67
  login
62
- .login(config)
63
- .then(async () => {
68
+ .login(config, APIClient, stackAPIClient)
69
+ .then(async function () {
64
70
  // setup branches
65
71
  try {
66
72
  await fetchBranchAndExport(APIClient, stackAPIClient);
@@ -79,13 +85,8 @@ exports.initial = async (config) => {
79
85
  addlogs(config, `${util.formatError(error)}`, 'error');
80
86
  }
81
87
  });
82
- } else if (config.management_token) {
83
- try {
84
- await fetchBranchAndExport(APIClient, stackAPIClient);
85
- } catch (error) {
86
- addlogs(config, util.formatError(error), 'error');
87
- }
88
- resolve();
88
+ } else {
89
+ reject('Kindly login or provide management_token');
89
90
  }
90
91
  });
91
92
  };
@@ -148,7 +149,11 @@ const allExport = async (APIClient, stackAPIClient, config, types, branchName) =
148
149
  addlogs(config, util.formatError(error), 'error');
149
150
  addlogs(
150
151
  config,
151
- chalk.red('Failed to migrate stack: ' + config.source_stack + '. Please check error logs for more info'),
152
+ chalk.red(
153
+ 'Failed to migrate stack: ' +
154
+ (config.sourceStackName || config.source_stack) +
155
+ '. Please check error logs for more info',
156
+ ),
152
157
  'error',
153
158
  );
154
159
  addlogs(config, 'The log for this is stored at ' + path.join(config.data, 'logs', 'export'), 'error');
@@ -1,5 +1,6 @@
1
1
  /* eslint-disable complexity */
2
2
  const { Command } = require('@contentstack/cli-command');
3
+ const { printFlagDeprecation, configHandler, flags, isAuthenticated } = require('@contentstack/cli-utilities');
3
4
  const {
4
5
  configWithMToken,
5
6
  parameterWithMToken,
@@ -9,35 +10,31 @@ const {
9
10
  withoutParametersWithAuthToken,
10
11
  } = require('../../../lib/util/export-flags');
11
12
  const config = require('../../../config/default');
12
- const { configHandler, printFlagDeprecation, flags } = require('@contentstack/cli-utilities');
13
13
 
14
14
  class ExportCommand extends Command {
15
15
  async run() {
16
16
  const { flags: exportCommandFlags } = await this.parse(ExportCommand);
17
17
  const extConfig = exportCommandFlags.config;
18
- let sourceStack = exportCommandFlags['stack-uid'] || exportCommandFlags['stack-api-key'];
18
+ const sourceStack = exportCommandFlags['stack-uid'] || exportCommandFlags['stack-api-key'];
19
19
  const alias = exportCommandFlags['alias'] || exportCommandFlags['management-token-alias'];
20
20
  const securedAssets = exportCommandFlags['secured-assets'];
21
21
  const data = exportCommandFlags.data || exportCommandFlags['data-dir'];
22
22
  const moduleName = exportCommandFlags.module;
23
23
  const contentTypes = exportCommandFlags['content-types'];
24
24
  const branchName = exportCommandFlags.branch;
25
- let _authToken = configHandler.get('authtoken');
26
25
  let host = this.region;
27
26
  let cmaHost = host.cma.split('//');
28
27
  let cdaHost = host.cda.split('//');
29
28
  host.cma = cmaHost[1];
30
29
  host.cda = cdaHost[1];
30
+ exportCommandFlags['isAuthenticated'] = isAuthenticated();
31
31
 
32
32
  config.forceStopMarketplaceAppsPrompt = exportCommandFlags.yes;
33
33
 
34
34
  if (alias) {
35
- let managementTokens = this.getToken(alias);
36
-
37
- if (alias) {
38
- const listOfTokens = configHandler.get('tokens');
39
- config.management_token_data = listOfTokens[alias];
40
- }
35
+ const managementTokens = this.getToken(alias);
36
+ const listOfTokens = configHandler.get('tokens');
37
+ config.management_token_data = listOfTokens[alias];
41
38
 
42
39
  if (managementTokens) {
43
40
  if (extConfig) {
@@ -49,6 +46,8 @@ class ExportCommand extends Command {
49
46
  branchName,
50
47
  securedAssets,
51
48
  moduleName,
49
+ data,
50
+ exportCommandFlags,
52
51
  );
53
52
  } else if (data) {
54
53
  await parameterWithMToken(
@@ -56,20 +55,20 @@ class ExportCommand extends Command {
56
55
  data,
57
56
  moduleName,
58
57
  host,
59
- _authToken,
60
58
  contentTypes,
61
59
  branchName,
62
60
  securedAssets,
61
+ exportCommandFlags,
63
62
  );
64
63
  } else if (data === undefined && sourceStack === undefined) {
65
64
  await withoutParameterMToken(
66
65
  managementTokens,
67
66
  moduleName,
68
67
  host,
69
- _authToken,
70
68
  contentTypes,
71
69
  branchName,
72
70
  securedAssets,
71
+ exportCommandFlags,
73
72
  );
74
73
  } else {
75
74
  this.log('Please provide a valid command. Run "csdx cm:export --help" command to view the command usage');
@@ -77,12 +76,19 @@ class ExportCommand extends Command {
77
76
  } else {
78
77
  this.log(alias + ' management token is not present, please add managment token first');
79
78
  }
80
- } else if (_authToken) {
79
+ } else if (isAuthenticated()) {
81
80
  if (extConfig) {
82
- await configWithAuthToken(extConfig, _authToken, moduleName, host, contentTypes, branchName, securedAssets);
81
+ await configWithAuthToken(
82
+ extConfig,
83
+ moduleName,
84
+ host,
85
+ contentTypes,
86
+ branchName,
87
+ securedAssets,
88
+ exportCommandFlags,
89
+ );
83
90
  } else if (sourceStack && data) {
84
91
  return await parametersWithAuthToken(
85
- _authToken,
86
92
  sourceStack,
87
93
  data,
88
94
  moduleName,
@@ -90,16 +96,23 @@ class ExportCommand extends Command {
90
96
  contentTypes,
91
97
  branchName,
92
98
  securedAssets,
99
+ exportCommandFlags,
93
100
  );
94
101
  } else if (data === undefined && sourceStack === undefined) {
95
- await withoutParametersWithAuthToken(_authToken, moduleName, host, contentTypes, branchName, securedAssets);
102
+ await withoutParametersWithAuthToken(
103
+ moduleName,
104
+ host,
105
+ contentTypes,
106
+ branchName,
107
+ securedAssets,
108
+ exportCommandFlags,
109
+ );
96
110
  } else {
97
111
  this.log('Please provide a valid command. Run "csdx cm:export --help" command to view the command usage');
98
112
  }
99
113
  } else {
100
114
  this.log('Login or provide the alias for management token');
101
115
  }
102
- // return
103
116
  }
104
117
  }
105
118
 
@@ -11,7 +11,7 @@ const Promise = require('bluebird');
11
11
  const _ = require('lodash');
12
12
  const chalk = require('chalk');
13
13
  const progress = require('progress-stream');
14
- const { HttpClient } = require('@contentstack/cli-utilities');
14
+ const { HttpClient, configHandler } = require('@contentstack/cli-utilities');
15
15
 
16
16
  const helper = require('../util/helper');
17
17
  const { addlogs } = require('../util/log');
@@ -53,11 +53,11 @@ module.exports = class ExportAssets {
53
53
  // Create asset folder
54
54
  mkdirp.sync(this.assetsFolderPath);
55
55
 
56
- return new Promise(function (resolve, reject) {
56
+ return new Promise((resolve, reject) => {
57
57
  // TBD: getting all the assets should have optimized
58
58
  return self
59
59
  .getAssetCount()
60
- .then(function (count) {
60
+ .then((count) => {
61
61
  const assetBatches = [];
62
62
 
63
63
  if (typeof count !== 'number' || count === 0) {
@@ -70,20 +70,20 @@ module.exports = class ExportAssets {
70
70
 
71
71
  return Promise.map(
72
72
  assetBatches,
73
- function (batch) {
73
+ (batch) => {
74
74
  return self
75
75
  .getAssetJSON(batch)
76
- .then(function (assetsJSON) {
76
+ .then((assetsJSON) => {
77
77
  return Promise.map(
78
78
  assetsJSON,
79
- function (assetJSON) {
79
+ (assetJSON) => {
80
80
  if (self.assetConfig.downloadVersionAssets) {
81
81
  return self
82
82
  .getVersionedAssetJSON(assetJSON.uid, assetJSON._version)
83
- .then(function () {
83
+ .then(() => {
84
84
  self.assetContents[assetJSON.uid] = assetJSON;
85
85
  })
86
- .catch(function (error) {
86
+ .catch((error) => {
87
87
  addlogs(
88
88
  self.config,
89
89
  chalk.red('The following asset failed to download\n' + JSON.stringify(assetJSON)),
@@ -93,7 +93,7 @@ module.exports = class ExportAssets {
93
93
  } else {
94
94
  return self
95
95
  .downloadAsset(assetJSON)
96
- .then(function () {
96
+ .then(() => {
97
97
  self.assetContents[assetJSON.uid] = assetJSON;
98
98
  })
99
99
  .catch((err) => {
@@ -108,38 +108,38 @@ module.exports = class ExportAssets {
108
108
  },
109
109
  { concurrency: self.vLimit },
110
110
  )
111
- .then(function () {
111
+ .then(() => {
112
112
  addlogs(self.config, 'Batch no ' + (batch + 1) + ' of assets is complete', 'success');
113
113
  // helper.writeFileSync(this.assetContentsFile, self.assetContents)
114
114
  })
115
- .catch(function (error) {
115
+ .catch((error) => {
116
116
  console.log('Error fetch/download the asset', error && error.message);
117
117
  addlogs(self.config, 'Asset batch ' + (batch + 1) + ' failed to download', 'error');
118
118
  addlogs(self.config, error, 'error');
119
119
  });
120
120
  })
121
- .catch(function (error) {
121
+ .catch((error) => {
122
122
  addlogs(self.config, error, 'error');
123
123
  reject(error);
124
124
  });
125
125
  },
126
126
  { concurrency: self.assetConfig.concurrencyLimit || 1 },
127
127
  )
128
- .then(function () {
128
+ .then(() => {
129
129
  helper.writeFileSync(self.assetContentsFile, self.assetContents);
130
130
 
131
131
  return self
132
132
  .exportFolders()
133
- .then(function () {
133
+ .then(() => {
134
134
  addlogs(self.config, chalk.green('Asset export completed successfully'), 'success');
135
135
  return resolve();
136
136
  })
137
- .catch(function (error) {
137
+ .catch((error) => {
138
138
  addlogs(self.config, error, 'success');
139
139
  reject(error);
140
140
  });
141
141
  })
142
- .catch(function (error) {
142
+ .catch((error) => {
143
143
  helper.writeFileSync(self.assetContentsFile, self.assetContents);
144
144
  addlogs(
145
145
  self.config,
@@ -149,7 +149,7 @@ module.exports = class ExportAssets {
149
149
  reject(error);
150
150
  });
151
151
  })
152
- .catch(function (error) {
152
+ .catch((error) => {
153
153
  addlogs(self.config, error, 'success');
154
154
  reject(error);
155
155
  });
@@ -158,10 +158,10 @@ module.exports = class ExportAssets {
158
158
 
159
159
  exportFolders() {
160
160
  const self = this;
161
- return new Promise(function (resolve, reject) {
161
+ return new Promise((resolve, reject) => {
162
162
  return self
163
163
  .getAssetCount(true)
164
- .then(function (fCount) {
164
+ .then((fCount) => {
165
165
  if (fCount === 0) {
166
166
  addlogs(self.config, 'No folders were found in the stack!', 'success');
167
167
  return resolve();
@@ -169,17 +169,17 @@ module.exports = class ExportAssets {
169
169
 
170
170
  return self
171
171
  .getFolderJSON(0, fCount)
172
- .then(function () {
172
+ .then(() => {
173
173
  // asset folders have been successfully exported
174
174
  addlogs(self.config, 'Asset-folders have been successfully exported!', 'success');
175
175
  return resolve();
176
176
  })
177
- .catch(function (error) {
177
+ .catch((error) => {
178
178
  addlogs(self.config, chalk.red('Error while exporting asset-folders!'), 'error');
179
179
  return reject(error);
180
180
  });
181
181
  })
182
- .catch(function (error) {
182
+ .catch((error) => {
183
183
  addlogs(self.config, error, 'error');
184
184
  // error while fetching asset folder count
185
185
  return reject(error);
@@ -189,7 +189,7 @@ module.exports = class ExportAssets {
189
189
 
190
190
  getFolderJSON(skip, fCount) {
191
191
  const self = this;
192
- return new Promise(function (resolve, reject) {
192
+ return new Promise((resolve, reject) => {
193
193
  if (typeof skip !== 'number') {
194
194
  skip = 0;
195
195
  }
@@ -213,13 +213,14 @@ module.exports = class ExportAssets {
213
213
  skip += 100;
214
214
  self.folderData.push(...response.items);
215
215
  return self.getFolderJSON(skip, fCount).then(resolve).catch(reject);
216
- });
216
+ })
217
+ .catch((error) => reject(error));
217
218
  });
218
219
  }
219
220
 
220
221
  getAssetCount(folder) {
221
222
  const self = this;
222
- return new Promise(function (resolve, reject) {
223
+ return new Promise((resolve, reject) => {
223
224
  if (folder && typeof folder === 'boolean') {
224
225
  const queryOptions = {
225
226
  skip: 99999990,
@@ -254,7 +255,7 @@ module.exports = class ExportAssets {
254
255
 
255
256
  getAssetJSON(skip) {
256
257
  const self = this;
257
- return new Promise(function (resolve, reject) {
258
+ return new Promise((resolve, reject) => {
258
259
  if (typeof skip !== 'number') {
259
260
  skip = 0;
260
261
  }
@@ -283,7 +284,7 @@ module.exports = class ExportAssets {
283
284
  const self = this;
284
285
  const assetVersionInfo = bucket || [];
285
286
 
286
- return new Promise(function (resolve, reject) {
287
+ return new Promise((resolve, reject) => {
287
288
  if (self.assetDownloadRetry[uid + version] > self.assetDownloadRetryLimit) {
288
289
  console.log('Reached max', self.assetDownloadRetry[uid + version]);
289
290
  return reject(new Error('Asset Max download retry limit exceeded! ' + uid));
@@ -308,7 +309,7 @@ module.exports = class ExportAssets {
308
309
  .then((versionedAssetJSONResponse) => {
309
310
  self
310
311
  .downloadAsset(versionedAssetJSONResponse)
311
- .then(function () {
312
+ .then(() => {
312
313
  assetVersionInfo.splice(0, 0, versionedAssetJSONResponse);
313
314
  // Remove duplicates
314
315
  assetVersionInfo = _.uniqWith(assetVersionInfo, _.isEqual);
@@ -328,7 +329,6 @@ module.exports = class ExportAssets {
328
329
  : (self.assetDownloadRetry[uid + version] = 1);
329
330
  return self.getVersionedAssetJSON(uid, version, assetVersionInfo).then(resolve).catch(reject);
330
331
  }
331
-
332
332
  reject(error);
333
333
  });
334
334
  });
@@ -336,7 +336,7 @@ module.exports = class ExportAssets {
336
336
 
337
337
  downloadAsset(asset) {
338
338
  const self = this;
339
- return new Promise(async function (resolve, reject) {
339
+ return new Promise(async (resolve, reject) => {
340
340
  const assetFolderPath = path.resolve(self.assetsFolderPath, asset.uid);
341
341
  const assetFilePath = path.resolve(assetFolderPath, asset.filename);
342
342
 
@@ -350,7 +350,7 @@ module.exports = class ExportAssets {
350
350
  }
351
351
  self.assetStream = {
352
352
  url: self.config.securedAssets
353
- ? `${asset.url}?authtoken=${self.config.authtoken || self.config.auth_token}`
353
+ ? `${asset.url}?authtoken=${configHandler.get('authtoken')}`
354
354
  : asset.url,
355
355
  };
356
356
 
@@ -366,7 +366,7 @@ module.exports = class ExportAssets {
366
366
  time: 5000,
367
367
  length: assetStreamRequest.headers['content-length'],
368
368
  });
369
- str.on('progress', function (progressData) {
369
+ str.on('progress', (progressData) => {
370
370
  console.log(`${asset.filename}: ${Math.round(progressData.percentage)}%`);
371
371
  });
372
372
  assetStreamRequest.pipe(str).pipe(assetFileStream);
@@ -378,7 +378,7 @@ module.exports = class ExportAssets {
378
378
  reject(error);
379
379
  });
380
380
  assetFileStream
381
- .on('close', function () {
381
+ .on('close', () => {
382
382
  addlogs(self.config, 'Downloaded ' + asset.filename + ': ' + asset.uid + ' successfully!', 'success');
383
383
  return resolve();
384
384
  })
@@ -391,26 +391,26 @@ module.exports = class ExportAssets {
391
391
 
392
392
  getFolders() {
393
393
  const self = this;
394
- return new Promise(function (resolve, reject) {
394
+ return new Promise((resolve, reject) => {
395
395
  return self
396
396
  .getAssetCount(true)
397
- .then(function (count) {
397
+ .then((count) => {
398
398
  if (count === 0) {
399
399
  addlogs(self.config, 'No folders were found in the stack', 'success');
400
400
  return resolve();
401
401
  }
402
402
  return self
403
403
  .getFolderDetails(0, count)
404
- .then(function () {
404
+ .then(() => {
405
405
  addlogs(self.config, chalk.green('Exported asset-folders successfully!'), 'success');
406
406
  return resolve();
407
407
  })
408
- .catch(function (error) {
408
+ .catch((error) => {
409
409
  addlogs(self.config, error, 'error');
410
410
  reject(error);
411
411
  });
412
412
  })
413
- .catch(function (error) {
413
+ .catch((error) => {
414
414
  addlogs(self.config, error, 'error');
415
415
  reject(error);
416
416
  });
@@ -419,7 +419,7 @@ module.exports = class ExportAssets {
419
419
 
420
420
  getFolderDetails(skip, tCount) {
421
421
  const self = this;
422
- return new Promise(function (resolve, reject) {
422
+ return new Promise((resolve, reject) => {
423
423
  if (typeof skip !== 'number') {
424
424
  skip = 0;
425
425
  }
@@ -4,7 +4,6 @@ const path = require('path');
4
4
  const chalk = require('chalk');
5
5
  const mkdirp = require('mkdirp');
6
6
  const { merge } = require('lodash');
7
-
8
7
  const helper = require('../util/helper');
9
8
  const { addlogs } = require('../util/log');
10
9
  const { formatError } = require('../util');
@@ -21,31 +20,27 @@ module.exports = class ExportCustomRoles {
21
20
  rolesConfig = config.modules.customRoles;
22
21
 
23
22
  constructor(exportConfig, stackAPIClient) {
24
- this.stackAPIClient = stackAPIClient;
25
23
  this.config = merge(config, exportConfig);
24
+ this.stackAPIClient = stackAPIClient;
26
25
  }
27
26
 
28
27
  async start() {
29
28
  const self = this;
29
+
30
30
  try {
31
31
  addlogs(this.config, 'Starting roles export', 'success');
32
-
33
32
  const rolesFolderPath = path.resolve(this.config.data, this.config.branchName || '', this.rolesConfig.dirName);
34
33
  mkdirp.sync(rolesFolderPath);
35
-
36
- const roles = await this.stackAPIClient.role().fetchAll({ include_rules: true, include_permissions: true });
37
-
34
+ const roles = await self.stackAPIClient.role().fetchAll({ include_rules: true, include_permissions: true });
38
35
  const customRoles = roles.items.filter((role) => !self.EXISTING_ROLES[role.name]);
39
-
40
36
  if (!customRoles.length) {
41
37
  addlogs(self.config, 'No custom roles were found in the Stack', 'success');
42
38
  return;
43
39
  }
44
-
45
40
  await self.getCustomRolesLocales(
46
41
  customRoles,
47
42
  path.join(rolesFolderPath, self.rolesConfig.customRolesLocalesFileName),
48
- this.stackAPIClient,
43
+ self.stackAPIClient,
49
44
  self.config,
50
45
  );
51
46
  self.customRoles = {};
@@ -80,7 +75,6 @@ module.exports = class ExportCustomRoles {
80
75
  });
81
76
  }
82
77
  }
83
-
84
78
  if (Object.keys(localesMap).length) {
85
79
  const locales = await stackAPIClient.locale().query({}).find();
86
80
  const sourceLocalesMap = {};
@@ -25,8 +25,8 @@ module.exports = class ExportEnvironments {
25
25
  };
26
26
 
27
27
  constructor(exportConfig, stackAPIClient) {
28
- this.stackAPIClient = stackAPIClient;
29
28
  this.config = merge(this.config, exportConfig);
29
+ this.stackAPIClient = stackAPIClient;
30
30
  }
31
31
 
32
32
  start() {
@@ -42,7 +42,7 @@ module.exports = class ExportEnvironments {
42
42
  mkdirp.sync(environmentsFolderPath);
43
43
  addlogs(self.config, 'Starting environment export', 'success');
44
44
 
45
- return new Promise(function (resolve, reject) {
45
+ return new Promise((resolve, reject) => {
46
46
  self.stackAPIClient
47
47
  .environment()
48
48
  .query(self.requestOptions.qs)
@@ -24,8 +24,8 @@ module.exports = class ExportExtensions {
24
24
  };
25
25
 
26
26
  constructor(exportConfig, stackAPIClient) {
27
- this.stackAPIClient = stackAPIClient;
28
27
  this.config = merge(config, exportConfig);
28
+ this.stackAPIClient = stackAPIClient;
29
29
  }
30
30
 
31
31
  start() {
@@ -34,8 +34,8 @@ module.exports = class ExportGlobalFields {
34
34
  include_count: true,
35
35
  },
36
36
  };
37
- this.stackAPIClient = stackAPIClient;
38
37
  this.config = merge(config, exportConfig);
38
+ this.stackAPIClient = stackAPIClient;
39
39
  this.globalfieldsFolderPath = path.resolve(
40
40
  this.config.data,
41
41
  this.config.branchName || '',
@@ -89,22 +89,17 @@ module.exports = class ExportGlobalFields {
89
89
  }
90
90
  self.global_fields.push(globalField);
91
91
  });
92
-
93
92
  skip += self.limit;
94
-
95
93
  if (skip > globalFieldResponse.count) {
96
94
  return resolve();
97
95
  }
98
-
99
96
  return self.getGlobalFields(skip, globalFieldConfig).then(resolve).catch(reject);
100
97
  } catch (error) {
101
98
  addlogs(globalFieldConfig, chalk.red(`Failed to export global-fields ${formatError(error)}`), 'error');
102
99
  reject(error);
103
100
  }
104
101
  })
105
- .catch((error) => {
106
- reject(error);
107
- });
102
+ .catch(reject);
108
103
  });
109
104
  }
110
105
 
@@ -117,7 +112,6 @@ module.exports = class ExportGlobalFields {
117
112
  self.global_fields,
118
113
  );
119
114
  addlogs(self.config, chalk.green('Global Fields export completed successfully'), 'success');
120
-
121
115
  resolve();
122
116
  } catch (error) {
123
117
  addlogs(self.config, error, 'error');
@@ -19,8 +19,8 @@ module.exports = class ExportLabels {
19
19
  labelConfig = config.modules.labels;
20
20
 
21
21
  constructor(exportConfig, stackAPIClient) {
22
- this.stackAPIClient = stackAPIClient;
23
22
  this.config = merge(config, exportConfig);
23
+ this.stackAPIClient = stackAPIClient;
24
24
  }
25
25
 
26
26
  start() {
@@ -2,6 +2,7 @@ const path = require('path');
2
2
  const chalk = require('chalk');
3
3
  const fileHelper = require('../util/helper');
4
4
  const { addlogs } = require('../util/log');
5
+ const { formatError } = require('../util');
5
6
  class LocaleExport {
6
7
  constructor(exportConfig, stackAPIClient) {
7
8
  this.stackAPIClient = stackAPIClient;
@@ -7,7 +7,15 @@ const _ = require('lodash');
7
7
  const path = require('path');
8
8
  const chalk = require('chalk');
9
9
  const mkdirp = require('mkdirp');
10
- const { cliux, HttpClient, NodeCrypto, managementSDKClient } = require('@contentstack/cli-utilities');
10
+ const {
11
+ cliux,
12
+ HttpClient,
13
+ NodeCrypto,
14
+ managementSDKClient,
15
+ HttpClientDecorator,
16
+ OauthDecorator,
17
+ isAuthenticated,
18
+ } = require('@contentstack/cli-utilities');
11
19
 
12
20
  const { formatError } = require('../util');
13
21
  const config = require('../../config/default');
@@ -29,7 +37,7 @@ module.exports = class ExportMarketplaceApps {
29
37
  }
30
38
 
31
39
  async start() {
32
- if (!this.config.auth_token) {
40
+ if (!isAuthenticated()) {
33
41
  cliux.print(
34
42
  'WARNING!!! To export Marketplace apps, you must be logged in. Please check csdx auth:login --help to log in',
35
43
  { color: 'yellow' },
@@ -41,10 +49,15 @@ module.exports = class ExportMarketplaceApps {
41
49
 
42
50
  await this.getOrgUid();
43
51
 
44
- this.httpClient = new HttpClient().headers({
45
- authtoken: this.config.auth_token,
46
- organization_uid: this.config.org_uid,
47
- });
52
+ const httpClient = new HttpClient();
53
+ if (!this.config.auth_token) {
54
+ this.httpClient = new OauthDecorator(httpClient);
55
+ const headers = await this.httpClient.preHeadersCheck(this.config);
56
+ this.httpClient = this.httpClient.headers(headers);
57
+ } else {
58
+ this.httpClient = new HttpClientDecorator(httpClient);
59
+ this.httpClient.headers(this.config);
60
+ }
48
61
 
49
62
  log(this.config, 'Starting marketplace app export', 'success');
50
63
  this.marketplaceAppPath = path.resolve(
@@ -58,18 +71,16 @@ module.exports = class ExportMarketplaceApps {
58
71
  }
59
72
 
60
73
  async getOrgUid() {
61
- if (this.config.auth_token) {
62
- const tempAPIClient = await managementSDKClient({ host: this.config.host });
63
- const tempStackData = await tempAPIClient
64
- .stack({ api_key: this.config.source_stack })
65
- .fetch()
66
- .catch((error) => {
67
- console.log(error);
68
- });
69
-
70
- if (tempStackData && tempStackData.org_uid) {
71
- this.config.org_uid = tempStackData.org_uid;
72
- }
74
+ const tempAPIClient = await managementSDKClient({ host: this.config.host });
75
+ const tempStackData = await tempAPIClient
76
+ .stack({ api_key: this.config.source_stack })
77
+ .fetch()
78
+ .catch((error) => {
79
+ console.log(error);
80
+ });
81
+
82
+ if (tempStackData && tempStackData.org_uid) {
83
+ this.config.org_uid = tempStackData.org_uid;
73
84
  }
74
85
  }
75
86
 
@@ -138,7 +149,7 @@ module.exports = class ExportMarketplaceApps {
138
149
  return listOfApps;
139
150
  })
140
151
  .catch((error) => {
141
- log(self.config, `Failed to export marketplace-apps ${formatError(error)}`, 'error');
152
+ log(this.config, `Failed to export marketplace-apps ${formatError(error)}`, 'error');
142
153
  });
143
154
  }
144
155
 
@@ -10,7 +10,7 @@ const { merge } = require('lodash');
10
10
  const helper = require('../util/helper');
11
11
  const { addlogs } = require('../util/log');
12
12
  const config = require('../../config/default');
13
- const { managementSDKClient } = require('@contentstack/cli-utilities');
13
+ const { managementSDKClient, isAuthenticated } = require('@contentstack/cli-utilities');
14
14
  module.exports = class ExportStack {
15
15
  stackConfig = config.modules.stack;
16
16
 
@@ -27,7 +27,7 @@ module.exports = class ExportStack {
27
27
 
28
28
  async start() {
29
29
  const self = this;
30
- if (self.config.auth_token) {
30
+ if (isAuthenticated()) {
31
31
  const tempAPIClient = await managementSDKClient({ host: config.host });
32
32
  const tempStackData = await tempAPIClient
33
33
  .stack({ api_key: self.config.source_stack })
@@ -14,6 +14,7 @@ const { formatError } = require('../util');
14
14
  const { addlogs } = require('../util/log');
15
15
  const config = require('../../config/default');
16
16
 
17
+ // Create folder for environments
17
18
  module.exports = class ExportWebhooks {
18
19
  config;
19
20
  master = {};
@@ -25,8 +26,8 @@ module.exports = class ExportWebhooks {
25
26
  webhooksConfig = config.modules.webhooks;
26
27
 
27
28
  constructor(exportConfig, stackAPIClient) {
28
- this.stackAPIClient = stackAPIClient;
29
29
  this.config = merge(config, exportConfig);
30
+ this.stackAPIClient = stackAPIClient;
30
31
  }
31
32
 
32
33
  start() {
@@ -20,8 +20,8 @@ module.exports = class ExportWorkFlows {
20
20
  workFlowConfig = config.modules.workflows;
21
21
 
22
22
  constructor(exportConfig, stackAPIClient) {
23
- this.stackAPIClient = stackAPIClient;
24
23
  this.config = merge(config, exportConfig);
24
+ this.stackAPIClient = stackAPIClient;
25
25
  }
26
26
 
27
27
  start() {
@@ -33,7 +33,6 @@ module.exports = class ExportWorkFlows {
33
33
  this.config.branchName || '',
34
34
  this.workFlowConfig.dirName,
35
35
  );
36
-
37
36
  mkdirp.sync(workflowsFolderPath);
38
37
 
39
38
  return new Promise(function (resolve, reject) {
@@ -103,6 +102,8 @@ module.exports = class ExportWorkFlows {
103
102
  deleteItems.forEach((e) => delete workflow[e]);
104
103
  }
105
104
  } catch (error) {
105
+ console.log('Error getting workflow data', error && error.message);
106
+ addlogs(self.config, 'Error fetching workflow data in export workflows task.', 'error');
106
107
  throw error;
107
108
  }
108
109
  }
@@ -11,14 +11,27 @@ const helper = require('../util/helper');
11
11
  let _ = require('lodash');
12
12
  const { cliux } = require('@contentstack/cli-utilities');
13
13
 
14
- exports.configWithMToken = async function (config, managementTokens, host, contentTypes, branchName, securedAssets, moduleName) {
14
+ exports.configWithMToken = async (
15
+ config,
16
+ managementTokens,
17
+ host,
18
+ contentTypes,
19
+ branchName,
20
+ securedAssets,
21
+ moduleName,
22
+ data,
23
+ exportCommandFlags,
24
+ ) => {
15
25
  let externalConfig = require(config);
26
+ const modules = externalConfig.filteredModules;
27
+
16
28
  defaultConfig.securedAssets = securedAssets;
17
29
  defaultConfig.management_token = managementTokens.token;
18
30
  defaultConfig.host = host.cma;
19
31
  defaultConfig.cdn = host.cda;
20
32
  defaultConfig.branchName = branchName;
21
33
  defaultConfig.source_stack = managementTokens.apiKey;
34
+ defaultConfig.isAuthenticated = exportCommandFlags.isAuthenticated;
22
35
  if (moduleName) {
23
36
  defaultConfig.moduleName = moduleName;
24
37
  // Specfic content type setting is only for entries module
@@ -27,25 +40,34 @@ exports.configWithMToken = async function (config, managementTokens, host, conte
27
40
  }
28
41
  }
29
42
  defaultConfig = _.merge(defaultConfig, externalConfig);
43
+
44
+ if(!defaultConfig.data) {
45
+ defaultConfig.data = data
46
+ }
47
+
48
+ if (_.isArray(modules)) {
49
+ defaultConfig.modules.types = _.filter(defaultConfig.modules.types, (module) => _.includes(modules, module));
50
+ }
51
+
30
52
  await initial(defaultConfig);
31
53
  };
32
54
 
33
- exports.parameterWithMToken = async function (
55
+ exports.parameterWithMToken = async (
34
56
  managementTokens,
35
57
  data,
36
58
  moduleName,
37
59
  host,
38
- _authToken,
39
60
  contentTypes,
40
61
  branchName,
41
62
  securedAssets,
42
- ) {
63
+ exportCommandFlags,
64
+ ) => {
43
65
  defaultConfig.management_token = managementTokens.token;
44
- defaultConfig.auth_token = _authToken;
45
66
  defaultConfig.host = host.cma;
46
67
  defaultConfig.cdn = host.cda;
47
68
  defaultConfig.branchName = branchName;
48
69
  defaultConfig.securedAssets = securedAssets;
70
+ defaultConfig.isAuthenticated = exportCommandFlags.isAuthenticated;
49
71
  if (!moduleName) {
50
72
  defaultConfig.contentTypes = contentTypes;
51
73
  } else {
@@ -65,10 +87,10 @@ exports.withoutParameterMToken = async (
65
87
  managementTokens,
66
88
  moduleName,
67
89
  host,
68
- _authToken,
69
90
  contentTypes,
70
91
  branchName,
71
92
  securedAssets,
93
+ exportCommandFlags,
72
94
  ) => {
73
95
  const stackUid = managementTokens.apiKey;
74
96
  const pathOfExport = await cliux.prompt(message.promptMessageList.promptPathStoredData);
@@ -76,8 +98,8 @@ exports.withoutParameterMToken = async (
76
98
  defaultConfig.host = host.cma;
77
99
  defaultConfig.cdn = host.cda;
78
100
  defaultConfig.branchName = branchName;
79
- defaultConfig.auth_token = _authToken;
80
101
  defaultConfig.securedAssets = securedAssets;
102
+ defaultConfig.isAuthenticated = exportCommandFlags.isAuthenticated;
81
103
  if (moduleName) {
82
104
  defaultConfig.moduleName = moduleName;
83
105
  // Specfic content type setting is only for entries module
@@ -90,13 +112,21 @@ exports.withoutParameterMToken = async (
90
112
  await initial(defaultConfig);
91
113
  };
92
114
 
93
- exports.configWithAuthToken = async function (config, _authToken, moduleName, host, contentTypes, branchName, securedAssets) {
94
- let externalConfig = helper.readFile(path.resolve(config));
95
- defaultConfig.auth_token = _authToken;
115
+ exports.configWithAuthToken = async function (
116
+ config,
117
+ moduleName,
118
+ host,
119
+ contentTypes,
120
+ branchName,
121
+ securedAssets,
122
+ exportCommandFlags,
123
+ ) {
124
+ let externalConfig = helper.readFileSync(path.resolve(config));
96
125
  defaultConfig.host = host.cma;
97
126
  defaultConfig.cdn = host.cda;
98
127
  defaultConfig.branchName = branchName;
99
128
  defaultConfig.securedAssets = securedAssets;
129
+ defaultConfig.isAuthenticated = exportCommandFlags.isAuthenticated;
100
130
  if (moduleName) {
101
131
  defaultConfig.moduleName = moduleName;
102
132
  // Specfic content type setting is only for entries module
@@ -108,8 +138,7 @@ exports.configWithAuthToken = async function (config, _authToken, moduleName, ho
108
138
  await initial(defaultConfig);
109
139
  };
110
140
 
111
- exports.parametersWithAuthToken = function (
112
- _authToken,
141
+ exports.parametersWithAuthToken = async (
113
142
  sourceStack,
114
143
  data,
115
144
  moduleName,
@@ -117,45 +146,38 @@ exports.parametersWithAuthToken = function (
117
146
  contentTypes,
118
147
  branchName,
119
148
  securedAssets,
120
- ) {
121
- return new Promise(async (resolve, reject) => {
122
- defaultConfig.auth_token = _authToken;
123
- defaultConfig.source_stack = sourceStack;
124
- if (moduleName) {
125
- defaultConfig.moduleName = moduleName;
126
- // Specfic content type setting is only for entries module
127
- if (moduleName === 'entries' && Array.isArray(contentTypes) && contentTypes.length > 0) {
128
- defaultConfig.contentTypes = contentTypes;
129
- }
149
+ exportCommandFlags,
150
+ ) => {
151
+ defaultConfig.source_stack = sourceStack;
152
+ defaultConfig.isAuthenticated = exportCommandFlags.isAuthenticated;
153
+ if (moduleName) {
154
+ defaultConfig.moduleName = moduleName;
155
+ // Specfic content type setting is only for entries module
156
+ if (moduleName === 'entries' && Array.isArray(contentTypes) && contentTypes.length > 0) {
157
+ defaultConfig.contentTypes = contentTypes;
130
158
  }
131
- defaultConfig.branchName = branchName;
132
- defaultConfig.host = host.cma;
133
- defaultConfig.cdn = host.cda;
134
- defaultConfig.data = data;
135
- defaultConfig.securedAssets = securedAssets;
136
- await initial(defaultConfig)
137
- .then(() => {
138
- return resolve();
139
- })
140
- .catch((error) => {
141
- return reject(error);
142
- });
143
- });
159
+ }
160
+ defaultConfig.branchName = branchName;
161
+ defaultConfig.host = host.cma;
162
+ defaultConfig.cdn = host.cda;
163
+ defaultConfig.data = data;
164
+ defaultConfig.securedAssets = securedAssets;
165
+ await initial(defaultConfig);
144
166
  };
145
167
 
146
168
  exports.withoutParametersWithAuthToken = async (
147
- _authToken,
148
169
  moduleName,
149
170
  host,
150
171
  contentTypes,
151
172
  branchName,
152
173
  securedAssets,
174
+ exportCommandFlags,
153
175
  ) => {
154
176
  const stackUid = await cliux.prompt(message.promptMessageList.promptSourceStack);
155
177
  const pathOfExport = await cliux.prompt(message.promptMessageList.promptPathStoredData);
156
- defaultConfig.auth_token = _authToken;
157
178
  defaultConfig.source_stack = stackUid;
158
179
  defaultConfig.securedAssets = securedAssets;
180
+ defaultConfig.isAuthenticated = exportCommandFlags.isAuthenticated;
159
181
  if (moduleName) {
160
182
  defaultConfig.moduleName = moduleName;
161
183
  // Specfic content type setting is only for entries module
@@ -4,12 +4,12 @@
4
4
  * MIT Licensed
5
5
  */
6
6
 
7
- var _ = require('lodash');
8
- var defaultConfig = require('../../config/default');
9
- const chalk = require('chalk');
7
+ const _ = require('lodash');
8
+ const defaultConfig = require('../../config/default');
10
9
  const promiseLimit = require('promise-limit');
10
+ const { isAuthenticated } = require('@contentstack/cli-utilities');
11
11
 
12
- exports.validateConfig = function (config) {
12
+ exports.validateConfig = (config) => {
13
13
  if (!config.host || !config.cdn) {
14
14
  throw new Error('Host/CDN end point is missing from config');
15
15
  }
@@ -22,7 +22,7 @@ exports.validateConfig = function (config) {
22
22
  !config.management_token &&
23
23
  config.source_stack &&
24
24
  !config.access_token &&
25
- !config.auth_token
25
+ !isAuthenticated()
26
26
  ) {
27
27
  throw new Error('Kindly provide management_token or email and password');
28
28
  } else if (
@@ -31,7 +31,7 @@ exports.validateConfig = function (config) {
31
31
  !config.access_token &&
32
32
  config.source_stack &&
33
33
  !config.management_token &&
34
- !config.auth_token
34
+ !isAuthenticated()
35
35
  ) {
36
36
  throw new Error('Kindly provide access_token or management_token');
37
37
  } else if (!config.email && !config.password && config.preserveStackVersion) {
@@ -9,7 +9,7 @@
9
9
 
10
10
  const chalk = require('chalk');
11
11
  const { addlogs } = require('../util/log');
12
- const { managementSDKClient } = require('@contentstack/cli-utilities');
12
+ const { managementSDKClient, isAuthenticated } = require('@contentstack/cli-utilities');
13
13
 
14
14
  module.exports.login = (config) => {
15
15
  return new Promise((resolve, reject) => {
@@ -22,11 +22,10 @@ module.exports.login = (config) => {
22
22
  .then((response) => {
23
23
  // eslint-disable-next-line no-console
24
24
  console.log(chalk.green('Contentstack account authenticated successfully!'));
25
- config.authtoken = response.user.authtoken;
26
25
  config.headers = {
27
26
  api_key: config.source_stack,
28
27
  access_token: config.access_token,
29
- authtoken: config.authtoken,
28
+ authtoken: response.user.authtoken,
30
29
  'X-User-Agent': 'contentstack-export/v',
31
30
  };
32
31
  resolve(config);
@@ -34,7 +33,7 @@ module.exports.login = (config) => {
34
33
  .catch((error) => {
35
34
  reject(error);
36
35
  });
37
- } else if (config.auth_token && !config.management_token) {
36
+ } else if (isAuthenticated() && !config.management_token) {
38
37
  const stackAPIClient = APIClient.stack({
39
38
  api_key: config.source_stack,
40
39
  management_token: config.management_token,
@@ -1,16 +1,19 @@
1
1
  const mkdirp = require('mkdirp');
2
2
  const path = require('path');
3
3
  const helper = require('./helper');
4
+ const {isAuthenticated, configHandler} = require('@contentstack/cli-utilities')
4
5
 
5
6
  const setupBranches = async (config, branch, stackAPIClient) => {
6
7
  if (typeof config !== 'object') {
7
8
  throw new Error('Invalid config to setup the branch');
8
9
  }
10
+
9
11
  let branches = [];
12
+
10
13
  const headers = { api_key: config.source_stack };
11
14
 
12
- if (config.auth_token) {
13
- headers['authtoken'] = config.auth_token;
15
+ if (isAuthenticated()) {
16
+ headers['authtoken'] = configHandler.get('authtoken');
14
17
  } else if (config.management_token) {
15
18
  headers['authorization'] = config.management_token;
16
19
  }