@contentstack/cli-cm-import 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
@@ -37,7 +37,7 @@ $ npm install -g @contentstack/cli-cm-import
37
37
  $ csdx COMMAND
38
38
  running command...
39
39
  $ csdx (--version)
40
- @contentstack/cli-cm-import/1.4.0 linux-x64 node-v16.19.1
40
+ @contentstack/cli-cm-import/1.5.1 linux-x64 node-v16.20.0
41
41
  $ csdx --help [COMMAND]
42
42
  USAGE
43
43
  $ 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:import": {
5
5
  "id": "cm:stacks:import",
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@contentstack/cli-cm-import",
3
3
  "description": "Contentstack CLI plugin to import content into 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/config": "^1.18.3",
11
11
  "big-json": "^3.2.0",
12
12
  "bluebird": "^3.7.2",
@@ -50,13 +50,9 @@
50
50
  "prepack": "oclif manifest && oclif readme",
51
51
  "test": "nyc mocha --forbid-only \"test/**/*.test.js\"",
52
52
  "version": "oclif readme && git add README.md",
53
+ "test:integration": "mocha --forbid-only \"test/run.test.js\" --integration-test --timeout 60000",
53
54
  "clean": "rm -rf ./node_modules tsconfig.build.tsbuildinfo"
54
55
  },
55
- "csdxConfig": {
56
- "expiredCommands": {
57
- "cm:import": "csdx cm:stacks:import"
58
- }
59
- },
60
56
  "main": "./src/commands/cm/stacks/import.js",
61
57
  "license": "MIT",
62
58
  "oclif": {
@@ -64,5 +60,14 @@
64
60
  "bin": "csdx",
65
61
  "repositoryPrefix": "<%- repo %>/blob/main/packages/contentstack-import/<%- commandPath %>"
66
62
  },
63
+ "csdxConfig": {
64
+ "expiredCommands": {
65
+ "cm:import": "csdx cm:stacks:import"
66
+ },
67
+ "shortCommandName": {
68
+ "cm:stacks:import": "IMPRT",
69
+ "cm:import": "O-IMPRT"
70
+ }
71
+ },
67
72
  "repository": "https://github.com/contentstack/cli"
68
73
  }
package/src/app.js CHANGED
@@ -11,7 +11,7 @@ const chalk = require('chalk');
11
11
  const util = require('./lib/util/index');
12
12
  const login = require('./lib/util/login');
13
13
  const { addlogs } = require('./lib/util/log');
14
- const { managementSDKClient } = require('@contentstack/cli-utilities');
14
+ const { managementSDKClient, isAuthenticated } = require('@contentstack/cli-utilities');
15
15
 
16
16
  exports.initial = (configData) => {
17
17
  return new Promise(async (resolve, reject) => {
@@ -58,10 +58,16 @@ exports.initial = (configData) => {
58
58
  };
59
59
 
60
60
  if (config) {
61
- if ((config.email && config.password) || config.auth_token) {
62
- login(config).then(backupAndImportData(APIClient, stackAPIClient)).catch(reject);
63
- } else if (config.management_token) {
61
+ if (config.management_token || config.isAuthenticated) {
64
62
  await backupAndImportData(APIClient, stackAPIClient);
63
+ } else if ((config.email && config.password) || isAuthenticated()) {
64
+ login(config).then(backupAndImportData(APIClient, stackAPIClient)).catch(reject);
65
+ } else if (config.email && config.password) {
66
+ login(config)
67
+ .then(backupAndImportData.apply(null, [APIClient, stackAPIClient]))
68
+ .catch(reject);
69
+ } else {
70
+ reject('Kindly login or provide management_token');
65
71
  }
66
72
  }
67
73
  });
@@ -1,7 +1,7 @@
1
1
  const _ = require('lodash');
2
2
  const defaultConfig = require('../../../config/default');
3
3
  const { Command } = require('@contentstack/cli-command');
4
- const { configHandler, flags, printFlagDeprecation } = require('@contentstack/cli-utilities');
4
+ const { flags, printFlagDeprecation, isAuthenticated } = require('@contentstack/cli-utilities');
5
5
  const {
6
6
  configWithMToken,
7
7
  parameterWithMToken,
@@ -21,12 +21,12 @@ class ImportCommand extends Command {
21
21
  const moduleName = importCommandFlags.module;
22
22
  const backupdir = importCommandFlags['backup-dir'];
23
23
  const alias = importCommandFlags['alias'] || importCommandFlags['management-token-alias'];
24
- let _authToken = configHandler.get('authtoken');
25
24
  importCommandFlags.branchName = importCommandFlags.branch;
26
25
  importCommandFlags.importWebhookStatus = importCommandFlags['import-webhook-status'];
27
26
  delete importCommandFlags.branch;
28
27
  delete importCommandFlags['import-webhook-status'];
29
28
  let host = self.cmaHost;
29
+ importCommandFlags['isAuthenticated'] = isAuthenticated();
30
30
 
31
31
  return new Promise((resolve, reject) => {
32
32
  if (data) {
@@ -41,58 +41,27 @@ class ImportCommand extends Command {
41
41
  if (managementTokens) {
42
42
  let result;
43
43
 
44
- if ((extConfig && _authToken) || alias) {
45
- result = configWithMToken(
46
- extConfig,
47
- managementTokens,
48
- moduleName,
49
- host,
50
- _authToken,
51
- backupdir,
52
- importCommandFlags,
53
- );
44
+ if ((extConfig && isAuthenticated()) || alias) {
45
+ result = configWithMToken(extConfig, managementTokens, moduleName, host, backupdir, importCommandFlags);
54
46
  } else if (data) {
55
- result = parameterWithMToken(
56
- managementTokens,
57
- data,
58
- moduleName,
59
- host,
60
- _authToken,
61
- backupdir,
62
- importCommandFlags,
63
- );
47
+ result = parameterWithMToken(managementTokens, data, moduleName, host, backupdir, importCommandFlags);
64
48
  } else {
65
- result = withoutParameterMToken(
66
- managementTokens,
67
- moduleName,
68
- host,
69
- _authToken,
70
- backupdir,
71
- importCommandFlags,
72
- );
49
+ result = withoutParameterMToken(managementTokens, moduleName, host, backupdir, importCommandFlags);
73
50
  }
74
51
 
75
52
  result.then(resolve).catch(reject);
76
53
  } else {
77
54
  console.log('management Token is not present please add managment token first');
78
55
  }
79
- } else if (_authToken) {
56
+ } else if (isAuthenticated()) {
80
57
  let result;
81
58
 
82
59
  if (extConfig) {
83
- result = configWithAuthToken(extConfig, _authToken, moduleName, host, backupdir, importCommandFlags);
60
+ result = configWithAuthToken(extConfig, moduleName, host, backupdir, importCommandFlags);
84
61
  } else if (targetStack && data) {
85
- result = parametersWithAuthToken(
86
- _authToken,
87
- targetStack,
88
- data,
89
- moduleName,
90
- host,
91
- backupdir,
92
- importCommandFlags,
93
- );
62
+ result = parametersWithAuthToken(targetStack, data, moduleName, host, backupdir, importCommandFlags);
94
63
  } else {
95
- result = withoutParametersWithAuthToken(_authToken, moduleName, host, backupdir, importCommandFlags);
64
+ result = withoutParametersWithAuthToken(moduleName, host, backupdir, importCommandFlags);
96
65
  }
97
66
 
98
67
  result.then(resolve).catch(reject);
@@ -127,7 +127,7 @@ class ContentTypesImport {
127
127
  try {
128
128
  await this.stackAPIClient.contentType().create(requestObject.json);
129
129
  } catch (error) {
130
- if (error.error_code === 115 && (error.errors.uid || error.errors.title)) {
130
+ if (error.errorCode === 115 && (error.errors.uid || error.errors.title)) {
131
131
  // content type uid already exists
132
132
  // _.remove(self.contentTypes, { uid: uid });
133
133
  return true;
@@ -1007,7 +1007,7 @@ module.exports = class ImportEntries {
1007
1007
  addlogs(this.config, 'field_rules is not available', 'error');
1008
1008
  }
1009
1009
 
1010
- self.stackAPIClient
1010
+ this.stackAPIClient
1011
1011
  .contentType(schema.uid)
1012
1012
  .fetch()
1013
1013
  .then((contentTypeResponse) => {
@@ -70,7 +70,7 @@ module.exports = class ImportLanguages {
70
70
  let langUids = Object.keys(self.languages);
71
71
  return Promise.map(
72
72
  langUids,
73
- function (langUid) {
73
+ (langUid) => {
74
74
  let lang = self.languages[langUid];
75
75
  if (!self.langUidMapper.hasOwnProperty(langUid) && lang.code !== self.masterLanguage) {
76
76
  let requestOption = {
@@ -8,8 +8,15 @@ const _ = require('lodash');
8
8
  const path = require('path');
9
9
  const chalk = require('chalk');
10
10
  const mkdirp = require('mkdirp');
11
- const contentstack = require('@contentstack/management');
12
- const { cliux, HttpClient, NodeCrypto, managementSDKClient } = require('@contentstack/cli-utilities');
11
+ const {
12
+ cliux,
13
+ HttpClient,
14
+ NodeCrypto,
15
+ managementSDKClient,
16
+ isAuthenticated,
17
+ HttpClientDecorator,
18
+ OauthDecorator,
19
+ } = require('@contentstack/cli-utilities');
13
20
 
14
21
  const { formatError } = require('../util');
15
22
  let config = require('../../config/default');
@@ -44,7 +51,7 @@ module.exports = class ImportMarketplaceApps {
44
51
 
45
52
  if (_.isEmpty(this.marketplaceApps)) {
46
53
  return Promise.resolve();
47
- } else if (!this.config.auth_token) {
54
+ } else if (!isAuthenticated()) {
48
55
  cliux.print(
49
56
  '\nWARNING!!! To import Marketplace apps, you must be logged in. Please check csdx auth:login --help to log in\n',
50
57
  { color: 'yellow' },
@@ -53,16 +60,19 @@ module.exports = class ImportMarketplaceApps {
53
60
  }
54
61
 
55
62
  this.developerHubBaseUrl = this.config.developerHubBaseUrl || (await getDeveloperHubUrl(this.config));
56
- this.client = contentstack.client({ authtoken: this.config.auth_token, endpoint: this.developerHubBaseUrl });
63
+ this.client = await managementSDKClient({ endpoint: this.developerHubBaseUrl });
57
64
 
58
65
  await this.getOrgUid();
59
66
 
60
- this.httpClient = new HttpClient({
61
- headers: {
62
- authtoken: this.config.auth_token,
63
- organization_uid: this.config.org_uid,
64
- },
65
- });
67
+ const httpClient = new HttpClient();
68
+ if (!this.config.auth_token) {
69
+ this.httpClient = new OauthDecorator(httpClient);
70
+ const headers = await this.httpClient.preHeadersCheck(this.config);
71
+ this.httpClient = this.httpClient.headers(headers);
72
+ } else {
73
+ this.httpClient = new HttpClientDecorator(httpClient);
74
+ this.httpClient.headers(this.config);
75
+ }
66
76
 
67
77
  if (!fs.existsSync(this.mapperDirPath)) {
68
78
  mkdirp.sync(this.mapperDirPath);
@@ -72,18 +82,16 @@ module.exports = class ImportMarketplaceApps {
72
82
  }
73
83
 
74
84
  async getOrgUid() {
75
- if (this.config.auth_token) {
76
- const tempAPIClient = await managementSDKClient({ host: this.config.host });
77
- const tempStackData = await tempAPIClient
78
- .stack({ api_key: this.config.target_stack })
79
- .fetch()
80
- .catch((error) => {
81
- console.log(error);
82
- });
85
+ const tempAPIClient = await managementSDKClient({ host: this.config.host });
86
+ const tempStackData = await tempAPIClient
87
+ .stack({ api_key: this.config.target_stack })
88
+ .fetch()
89
+ .catch((error) => {
90
+ console.log(error);
91
+ });
83
92
 
84
- if (tempStackData && tempStackData.org_uid) {
85
- this.config.org_uid = tempStackData.org_uid;
86
- }
93
+ if (tempStackData && tempStackData.org_uid) {
94
+ this.config.org_uid = tempStackData.org_uid;
87
95
  }
88
96
  }
89
97
 
@@ -288,7 +296,7 @@ module.exports = class ImportMarketplaceApps {
288
296
  } else {
289
297
  log(this.config, formatError(message), 'error');
290
298
 
291
- if (this.config.forceStopMarketplaceAppsPrompt) return resolve();
299
+ if (this.config.forceStopMarketplaceAppsPrompt) return Promise.resolve();
292
300
 
293
301
  if (
294
302
  await cliux.confirm(
@@ -297,7 +305,7 @@ module.exports = class ImportMarketplaceApps {
297
305
  ),
298
306
  )
299
307
  ) {
300
- resolve();
308
+ Promise.resolve();
301
309
  } else {
302
310
  process.exit();
303
311
  }
@@ -386,9 +394,9 @@ module.exports = class ImportMarketplaceApps {
386
394
  .catch((error) => error);
387
395
 
388
396
  if (installation.installation_uid) {
389
- let appName = (this.appNameMapping[app.manifest.name]) ?
390
- this.appNameMapping[app.manifest.name] :
391
- app.manifest.name ;
397
+ let appName = this.appNameMapping[app.manifest.name]
398
+ ? this.appNameMapping[app.manifest.name]
399
+ : app.manifest.name;
392
400
  log(this.config, `${appName} app installed successfully.!`, 'success');
393
401
  await this.makeRedirectUrlCall(installation, app.manifest.name);
394
402
  this.installationUidMapping[app.uid] = installation.installation_uid;
@@ -14,7 +14,6 @@ exports.configWithMToken = function (
14
14
  managementTokens,
15
15
  moduleName,
16
16
  host,
17
- _authToken,
18
17
  backupdir,
19
18
  importCommandFlags,
20
19
  ) {
@@ -31,6 +30,7 @@ exports.configWithMToken = function (
31
30
  defaultConfig.target_stack = managementTokens.apiKey;
32
31
  defaultConfig.management_token = managementTokens.token;
33
32
  defaultConfig.importWebhookStatus = importCommandFlags.importWebhookStatus;
33
+ defaultConfig.isAuthenticated = importCommandFlags.isAuthenticated;
34
34
 
35
35
  if (moduleName && moduleName !== undefined) {
36
36
  defaultConfig.moduleName = moduleName;
@@ -40,12 +40,11 @@ exports.configWithMToken = function (
40
40
  defaultConfig.useBackedupDir = backupdir;
41
41
  }
42
42
 
43
- defaultConfig.auth_token = _authToken;
44
43
  defaultConfig = _.merge(defaultConfig, externalConfig);
45
44
 
46
- if(!defaultConfig.data) {
45
+ if (!defaultConfig.data) {
47
46
  const exporteddata = await cliux.prompt(message.promptMessageList.promptPathStoredData);
48
- defaultConfig.data = exporteddata
47
+ defaultConfig.data = exporteddata;
49
48
  }
50
49
 
51
50
  if (_.isArray(modules)) {
@@ -61,16 +60,15 @@ exports.parameterWithMToken = function (
61
60
  data,
62
61
  moduleName,
63
62
  host,
64
- _authToken,
65
63
  backupdir,
66
64
  importCommandFlags,
67
65
  ) {
68
66
  return new Promise(async function (resolve, reject) {
69
67
  defaultConfig.management_token = managementTokens.token;
70
68
  defaultConfig.target_stack = managementTokens.apiKey;
71
- defaultConfig.auth_token = _authToken;
72
69
  defaultConfig.branchName = importCommandFlags.branchName;
73
70
  defaultConfig.importWebhookStatus = importCommandFlags.importWebhookStatus;
71
+ defaultConfig.isAuthenticated = importCommandFlags.isAuthenticated;
74
72
  if (moduleName && moduleName !== undefined) {
75
73
  defaultConfig.moduleName = moduleName;
76
74
  }
@@ -88,7 +86,6 @@ exports.withoutParameterMToken = async (
88
86
  managementTokens,
89
87
  moduleName,
90
88
  host,
91
- _authToken,
92
89
  backupdir,
93
90
  importCommandFlags,
94
91
  ) => {
@@ -96,9 +93,9 @@ exports.withoutParameterMToken = async (
96
93
  const exporteddata = await cliux.prompt(message.promptMessageList.promptPathStoredData);
97
94
  defaultConfig.management_token = managementTokens.token;
98
95
  defaultConfig.target_stack = managementTokens.apiKey;
99
- defaultConfig.auth_token = _authToken;
100
96
  defaultConfig.branchName = importCommandFlags.branchName;
101
97
  defaultConfig.importWebhookStatus = importCommandFlags.importWebhookStatus;
98
+ defaultConfig.isAuthenticated = importCommandFlags.isAuthenticated;
102
99
  if (moduleName && moduleName !== undefined) {
103
100
  defaultConfig.moduleName = moduleName;
104
101
  }
@@ -111,12 +108,12 @@ exports.withoutParameterMToken = async (
111
108
  });
112
109
  };
113
110
 
114
- exports.configWithAuthToken = function (config, _authToken, moduleName, host, backupdir, importCommandFlags) {
111
+ exports.configWithAuthToken = function (config, moduleName, host, backupdir, importCommandFlags) {
115
112
  return new Promise(async function (resolve, reject) {
116
113
  let externalConfig = require(config);
117
- defaultConfig.auth_token = _authToken;
118
114
  defaultConfig.branchName = importCommandFlags.branchName;
119
115
  defaultConfig.importWebhookStatus = importCommandFlags.importWebhookStatus;
116
+ defaultConfig.isAuthenticated = importCommandFlags.isAuthenticated;
120
117
  if (moduleName && moduleName !== undefined) {
121
118
  defaultConfig.moduleName = moduleName;
122
119
  }
@@ -133,14 +130,13 @@ exports.configWithAuthToken = function (config, _authToken, moduleName, host, ba
133
130
  defaultConfig = _.merge(defaultConfig, externalConfig);
134
131
  if (!defaultConfig.data) {
135
132
  const exporteddata = await cliux.prompt(message.promptMessageList.promptPathStoredData);
136
- defaultConfig.data = exporteddata
133
+ defaultConfig.data = exporteddata;
137
134
  }
138
135
  initial(defaultConfig).then(resolve).catch(reject);
139
136
  });
140
137
  };
141
138
 
142
139
  exports.parametersWithAuthToken = function (
143
- _authToken,
144
140
  targetStack,
145
141
  data,
146
142
  moduleName,
@@ -149,10 +145,10 @@ exports.parametersWithAuthToken = function (
149
145
  importCommandFlags,
150
146
  ) {
151
147
  return new Promise(async function (resolve, reject) {
152
- defaultConfig.auth_token = _authToken;
153
148
  defaultConfig.target_stack = targetStack;
154
149
  defaultConfig.branchName = importCommandFlags.branchName;
155
150
  defaultConfig.importWebhookStatus = importCommandFlags.importWebhookStatus;
151
+ defaultConfig.isAuthenticated = importCommandFlags.isAuthenticated;
156
152
  if (moduleName && moduleName !== undefined && backupdir === undefined) {
157
153
  defaultConfig.moduleName = moduleName;
158
154
  } else if (moduleName && moduleName !== undefined && backupdir !== undefined) {
@@ -168,15 +164,15 @@ exports.parametersWithAuthToken = function (
168
164
  });
169
165
  };
170
166
 
171
- exports.withoutParametersWithAuthToken = async (_authToken, moduleName, host, backupdir, importCommandFlags) => {
167
+ exports.withoutParametersWithAuthToken = async (moduleName, host, backupdir, importCommandFlags) => {
172
168
  return new Promise(async function (resolve, reject) {
173
169
  const stackUid = await cliux.prompt(message.promptMessageList.promptTargetStack);
174
170
  const exporteddata = await cliux.prompt(message.promptMessageList.promptPathStoredData);
175
- defaultConfig.auth_token = _authToken;
176
171
  defaultConfig.target_stack = stackUid;
177
172
  defaultConfig.data = exporteddata;
178
173
  defaultConfig.branchName = importCommandFlags.branchName;
179
174
  defaultConfig.importWebhookStatus = importCommandFlags.importWebhookStatus;
175
+ defaultConfig.isAuthenticated = importCommandFlags.isAuthenticated;
180
176
  if (moduleName && moduleName !== undefined && backupdir === undefined) {
181
177
  defaultConfig.moduleName = moduleName;
182
178
  } else if (moduleName && moduleName !== undefined && backupdir !== undefined) {
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  const _ = require('lodash');
9
- const { HttpClient, managementSDKClient } = require('@contentstack/cli-utilities');
9
+ const { HttpClient, managementSDKClient, isAuthenticated } = require('@contentstack/cli-utilities');
10
10
  const fs = require('./fs');
11
11
  const path = require('path');
12
12
  const chalk = require('chalk');
@@ -33,7 +33,7 @@ exports.validateConfig = (importConfig) => {
33
33
  !importConfig.password &&
34
34
  !importConfig.management_token &&
35
35
  importConfig.target_stack &&
36
- !importConfig.auth_token
36
+ !isAuthenticated()
37
37
  ) {
38
38
  addlogs(importConfig, chalk.red('Kindly provide management_token or email and password'), 'error');
39
39
  return 'error';
@@ -8,7 +8,7 @@
8
8
  const chalk = require('chalk');
9
9
 
10
10
  const { addlogs } = require('../util/log');
11
- const { managementSDKClient } = require('@contentstack/cli-utilities');
11
+ const { managementSDKClient, isAuthenticated } = require('@contentstack/cli-utilities');
12
12
 
13
13
  module.exports = (config) => {
14
14
  return new Promise((resolve, reject) => {
@@ -21,10 +21,9 @@ module.exports = (config) => {
21
21
  .then((response) => {
22
22
  // eslint-disable-next-line no-console
23
23
  console.log(chalk.green('Contentstack account authenticated successfully!'));
24
- config.authtoken = response.user.authtoken;
25
24
  config.headers = {
26
25
  api_key: config.target_stack,
27
- authtoken: config.authtoken,
26
+ authtoken: response.user.authtoken,
28
27
  'X-User-Agent': 'contentstack-import/v',
29
28
  };
30
29
  return resolve(config);
@@ -32,7 +31,7 @@ module.exports = (config) => {
32
31
  .catch(reject);
33
32
  } else if (config.management_token) {
34
33
  return resolve();
35
- } else if (config.auth_token) {
34
+ } else if (isAuthenticated()) {
36
35
  const stackAPIClient = APIClient.stack({
37
36
  api_key: config.target_stack,
38
37
  management_token: config.management_token,