@contentstack/cli-cm-import 1.2.4 → 1.4.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.
@@ -9,13 +9,12 @@ const path = require('path');
9
9
  const chalk = require('chalk');
10
10
  const mkdirp = require('mkdirp');
11
11
  const Promise = require('bluebird');
12
- const { isEmpty } = require('lodash');
12
+ const { isEmpty, merge } = require('lodash');
13
13
 
14
14
  const helper = require('../util/fs');
15
15
  let { addlogs } = require('../util/log');
16
16
  const { formatError } = require('../util');
17
17
  const config = require('../../config/default');
18
- const stack = require('../util/contentstack-management-sdk');
19
18
 
20
19
  module.exports = class ImportEnvironments {
21
20
  fails = [];
@@ -23,15 +22,15 @@ module.exports = class ImportEnvironments {
23
22
  envUidMapper = {};
24
23
  fetchConcurrency = config.modules.environments.concurrency || config.fetchConcurrency || 2;
25
24
 
26
- constructor(credentialConfig) {
27
- this.config = credentialConfig;
25
+ constructor(importConfig, stackAPIClient) {
26
+ this.config = merge(config, importConfig);
27
+ this.stackAPIClient = stackAPIClient;
28
28
  }
29
29
 
30
30
  start() {
31
31
  addlogs(this.config, 'Migrating environment', 'success');
32
32
 
33
33
  const self = this;
34
- const client = stack.Client(this.config);
35
34
  let environmentConfig = config.modules.environments;
36
35
  let environmentsFolderPath = path.resolve(this.config.data, environmentConfig.dirName);
37
36
  let envMapperPath = path.resolve(this.config.data, 'mapper', 'environments');
@@ -60,8 +59,7 @@ module.exports = class ImportEnvironments {
60
59
  if (!self.envUidMapper.hasOwnProperty(envUid)) {
61
60
  let requestOption = { environment: env };
62
61
 
63
- return client
64
- .stack({ api_key: config.target_stack, management_token: config.management_token })
62
+ return self.stackAPIClient
65
63
  .environment()
66
64
  .create(requestOption)
67
65
  .then((environment) => {
@@ -9,15 +9,13 @@ const fs = require('fs');
9
9
  const path = require('path');
10
10
  const Promise = require('bluebird');
11
11
  const chalk = require('chalk');
12
- const { isEmpty } = require('lodash');
12
+ const { isEmpty, merge } = require('lodash');
13
13
 
14
14
  const util = require('../util');
15
15
  const helper = require('../util/fs');
16
16
  const { addlogs } = require('../util/log');
17
- const { formatError } = require('../util');
18
17
 
19
18
  let config = util.getConfig();
20
- const stack = require('../util/contentstack-management-sdk');
21
19
 
22
20
  module.exports = class ImportExtensions {
23
21
  fails = [];
@@ -26,15 +24,15 @@ module.exports = class ImportExtensions {
26
24
  extensionsConfig = config.modules.extensions;
27
25
  reqConcurrency = config.concurrency || config.fetchConcurrency || 1;
28
26
 
29
- constructor(credential) {
30
- this.config = credential;
27
+ constructor(importConfig, stackAPIClient) {
28
+ this.config = merge(config, importConfig);
29
+ this.stackAPIClient = stackAPIClient;
31
30
  }
32
31
 
33
32
  start() {
34
33
  addlogs(this.config, chalk.white('Migrating extensions'), 'success');
35
34
 
36
35
  const self = this;
37
- const client = stack.Client(this.config);
38
36
  let extensionsFolderPath = path.resolve(this.config.data, this.extensionsConfig.dirName);
39
37
  let extMapperPath = path.resolve(this.config.data, 'mapper', 'extensions');
40
38
  let extUidMapperPath = path.resolve(this.config.data, 'mapper/extensions', 'uid-mapping.json');
@@ -59,8 +57,7 @@ module.exports = class ImportExtensions {
59
57
  function (extUid) {
60
58
  let ext = self.extensions[extUid];
61
59
  if (!self.extUidMapper.hasOwnProperty(extUid)) {
62
- return client
63
- .stack({ api_key: config.target_stack, management_token: config.management_token })
60
+ return self.stackAPIClient
64
61
  .extension()
65
62
  .create({ extension: ext })
66
63
  .then((response) => {
@@ -103,7 +100,7 @@ module.exports = class ImportExtensions {
103
100
  .catch(function (error) {
104
101
  // error while importing extensions
105
102
  helper.writeFileSync(extFailsPath, self.fails);
106
- addlogs(self.config, chalk.red(`Extension import failed ${formatError(error)}`), 'error');
103
+ addlogs(self.config, chalk.red(`Extension import failed ${util.formatError(error)}`), 'error');
107
104
  reject(error);
108
105
  });
109
106
  });
@@ -9,16 +9,14 @@ let path = require('path');
9
9
  let chalk = require('chalk');
10
10
  let mkdirp = require('mkdirp');
11
11
  let Promise = require('bluebird');
12
- const { isEmpty } = require('lodash');
12
+ const { isEmpty, merge } = require('lodash');
13
13
 
14
14
  let helper = require('../util/fs');
15
15
  let { addlogs } = require('../util/log');
16
16
  const { formatError } = require('../util');
17
17
  let config = require('../../config/default');
18
- const stack = require('../util/contentstack-management-sdk');
19
18
  let extension_supress = require('../util/extensionsUidReplace');
20
19
  let removeReferenceFields = require('../util/removeReferenceFields');
21
- const { getInstalledExtensions } = require('../util/marketplace-app-helper');
22
20
 
23
21
  global._globalField_pending = [];
24
22
 
@@ -29,8 +27,9 @@ module.exports = class ImportGlobalFields {
29
27
  installedExtensions = [];
30
28
  reqConcurrency = config.concurrency || config.fetchConcurrency || 1;
31
29
 
32
- constructor(credential) {
33
- this.config = credential;
30
+ constructor(importConfig, stackAPIClient) {
31
+ this.config = merge(config, importConfig);
32
+ this.stackAPIClient = stackAPIClient;
34
33
  }
35
34
 
36
35
  async start() {
@@ -39,13 +38,14 @@ module.exports = class ImportGlobalFields {
39
38
  let self = this;
40
39
  let globalfieldsConfig = config.modules.globalfields;
41
40
  let globalfieldsFolderPath = path.resolve(this.config.data, globalfieldsConfig.dirName);
42
- let appMapperFolderPath = path.join(this.config.data, 'mapper', 'marketplace_apps');
43
41
  let globalfieldsMapperPath = path.resolve(this.config.data, 'mapper', 'global_fields');
44
42
  let globalfieldsUidMapperPath = path.resolve(this.config.data, 'mapper', 'global_fields', 'uid-mapping.json');
45
43
  let globalfieldsSuccessPath = path.resolve(this.config.data, 'mapper', 'global_fields', 'success.json');
46
44
  let globalFieldsPending = path.resolve(this.config.data, 'mapper', 'global_fields', 'pending_global_fields.js');
47
45
  let globalfieldsFailsPath = path.resolve(this.config.data, 'mapper', 'global_fields', 'fails.json');
48
46
  self.globalfields = helper.readFileSync(path.resolve(globalfieldsFolderPath, globalfieldsConfig.fileName));
47
+ const appMapperPath = path.join(this.config.data, 'mapper', 'marketplace_apps', 'uid-mapping.json');
48
+ this.installedExtensions = ((await helper.readFileSync(appMapperPath)) || { extension_uid: {} }).extension_uid;
49
49
 
50
50
  if (fs.existsSync(globalfieldsUidMapperPath)) {
51
51
  self.snipUidMapper = helper.readFileSync(globalfieldsUidMapperPath);
@@ -56,15 +56,6 @@ module.exports = class ImportGlobalFields {
56
56
  mkdirp.sync(globalfieldsMapperPath);
57
57
  }
58
58
 
59
- if (fs.existsSync(path.join(appMapperFolderPath, 'marketplace-apps.json'))) {
60
- self.installedExtensions = helper.readFileSync(path.join(appMapperFolderPath, 'marketplace-apps.json')) || {};
61
- }
62
-
63
- if (isEmpty(self.installedExtensions)) {
64
- self.installedExtensions = await getInstalledExtensions(self.config);
65
- }
66
-
67
- const client = stack.Client(self.config);
68
59
  return new Promise(function (resolve, reject) {
69
60
  if (self.globalfields === undefined || isEmpty(self.globalfields)) {
70
61
  addlogs(self.config, chalk.white('No globalfields Found'), 'success');
@@ -87,8 +78,7 @@ module.exports = class ImportGlobalFields {
87
78
 
88
79
  if (!self.snipUidMapper.hasOwnProperty(snipUid)) {
89
80
  let requestOption = { global_field: snip };
90
- return client
91
- .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
81
+ return self.stackAPIClient
92
82
  .globalField()
93
83
  .create(requestOption)
94
84
  .then((globalField) => {
@@ -15,7 +15,6 @@ const helper = require('../util/fs');
15
15
  const { formatError } = require('../util');
16
16
  const { addlogs } = require('../util/log');
17
17
  const config = require('../../config/default');
18
- const stack = require('../util/contentstack-management-sdk');
19
18
 
20
19
  module.exports = class ImportLabels {
21
20
  config;
@@ -27,9 +26,9 @@ module.exports = class ImportLabels {
27
26
  labelConfig = config.modules.labels;
28
27
  reqConcurrency = config.concurrency || config.fetchConcurrency || 1;
29
28
 
30
- constructor(credentialConfig) {
31
- this.config = merge(config, credentialConfig);
32
- this.client = stack.Client(config);
29
+ constructor(importConfig, stackAPIClient) {
30
+ this.config = merge(config, importConfig);
31
+ this.stackAPIClient = stackAPIClient;
33
32
  }
34
33
 
35
34
  start() {
@@ -68,8 +67,7 @@ module.exports = class ImportLabels {
68
67
  if (!self.labelUidMapper.hasOwnProperty(labelUid)) {
69
68
  let requestOption = { label: label };
70
69
 
71
- return self.client
72
- .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
70
+ return self.stackAPIClient
73
71
  .label()
74
72
  .create(requestOption)
75
73
  .then(function (response) {
@@ -138,8 +136,7 @@ module.exports = class ImportLabels {
138
136
  label.parent[i] = self.labelUidMapper[parentUids[i]].uid;
139
137
  }
140
138
  }
141
- return self.client
142
- .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
139
+ return self.stackAPIClient
143
140
  .label(newLabelUid.uid)
144
141
  .fetch()
145
142
  .then(function (response) {
@@ -11,25 +11,25 @@ let chalk = require('chalk');
11
11
  let mkdirp = require('mkdirp');
12
12
  let Promise = require('bluebird');
13
13
  let { isEmpty, merge, cloneDeep } = require('lodash');
14
+ const { cliux } = require('@contentstack/cli-utilities');
14
15
 
15
16
  let helper = require('../util/fs');
16
17
  let { addlogs } = require('../util/log');
17
18
  const { formatError } = require('../util');
18
19
  let config = require('../../config/default');
19
- let stack = require('../util/contentstack-management-sdk');
20
20
 
21
21
  module.exports = class ImportLanguages {
22
- client;
23
22
  fails = [];
24
23
  success = [];
25
24
  langUidMapper = {};
26
25
  masterLanguage = config.master_locale;
27
26
  langConfig = config.modules.locales;
27
+ sourceMasterLangConfig = config.modules.masterLocale;
28
28
  reqConcurrency = config.concurrency || config.fetchConcurrency || 1;
29
29
 
30
- constructor(credentialConfig) {
31
- this.config = merge(config, credentialConfig);
32
- this.client = stack.Client(this.config);
30
+ constructor(importConfig, stackAPIClient) {
31
+ this.config = merge(config, importConfig);
32
+ this.stackAPIClient = stackAPIClient;
33
33
  }
34
34
 
35
35
  start() {
@@ -39,9 +39,10 @@ module.exports = class ImportLanguages {
39
39
  let langMapperPath = path.resolve(this.config.data, 'mapper', 'languages');
40
40
  let langFolderPath = path.resolve(this.config.data, this.langConfig.dirName);
41
41
  let langFailsPath = path.resolve(this.config.data, 'mapper', 'languages', 'fails.json');
42
- let langSuccessPath = path.resolve(this.config.data, 'mapper', 'languages', 'success.json');
43
42
  let langUidMapperPath = path.resolve(this.config.data, 'mapper', 'languages', 'uid-mapper.json');
43
+ self.langSuccessPath = path.resolve(this.config.data, 'mapper', 'languages', 'success.json');
44
44
  self.languages = helper.readFileSync(path.resolve(langFolderPath, this.langConfig.fileName));
45
+ self.sourceMasterLanguages = helper.readFileSync(path.resolve(langFolderPath, this.sourceMasterLangConfig.fileName));
45
46
 
46
47
  mkdirp.sync(langMapperPath);
47
48
 
@@ -50,11 +51,22 @@ module.exports = class ImportLanguages {
50
51
  self.langUidMapper = self.langUidMapper || {};
51
52
  }
52
53
 
53
- return new Promise(function (resolve, reject) {
54
+ return new Promise(async function (resolve, reject) {
54
55
  if (self.languages === undefined || isEmpty(self.languages)) {
55
56
  addlogs(self.config, chalk.white('No Languages Found'), 'success');
56
57
  return resolve({ empty: true });
57
58
  }
59
+
60
+ let sourceMasterLangDetails = self.sourceMasterLanguages && Object.values(self.sourceMasterLanguages);
61
+ if (sourceMasterLangDetails &&
62
+ sourceMasterLangDetails[0] &&
63
+ sourceMasterLangDetails[0]["code"] &&
64
+ self.masterLanguage["code"] === sourceMasterLangDetails[0]["code"]) {
65
+ await self.checkAndUpdateMasterLocaleName(sourceMasterLangDetails).catch((error) => {
66
+ addlogs(self.config, formatError(error), 'warn');
67
+ })
68
+ }
69
+
58
70
  let langUids = Object.keys(self.languages);
59
71
  return Promise.map(
60
72
  langUids,
@@ -68,8 +80,7 @@ module.exports = class ImportLanguages {
68
80
  },
69
81
  };
70
82
 
71
- return self.client
72
- .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
83
+ return self.stackAPIClient
73
84
  .locale()
74
85
  .create(requestOption)
75
86
  .then((locale) => {
@@ -101,7 +112,7 @@ module.exports = class ImportLanguages {
101
112
  self
102
113
  .updateLocales(langUids)
103
114
  .then(() => {
104
- helper.writeFileSync(langSuccessPath, self.success);
115
+ helper.writeFileSync(self.langSuccessPath, self.success);
105
116
  addlogs(self.config, chalk.green('Languages have been imported successfully!'), 'success');
106
117
  resolve();
107
118
  })
@@ -131,9 +142,7 @@ module.exports = class ImportLanguages {
131
142
  requireKeys.forEach((e) => {
132
143
  lang[e] = _lang[e];
133
144
  });
134
- let langobj = self.client
135
- .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
136
- .locale(lang.code);
145
+ let langobj = self.stackAPIClient.locale(lang.code);
137
146
  Object.assign(langobj, cloneDeep(lang));
138
147
  langobj.update().then(() => {
139
148
  // empty function
@@ -147,4 +156,58 @@ module.exports = class ImportLanguages {
147
156
  });
148
157
  });
149
158
  }
159
+
160
+ checkAndUpdateMasterLocaleName(sourceMasterLangDetails) {
161
+ let self = this;
162
+ return new Promise(async function (resolve, reject) {
163
+ let masterLangDetails = await self.stackAPIClient.locale(self.masterLanguage["code"]).fetch()
164
+ .catch((error) => {
165
+ addlogs(self.config, formatError(error), 'warn');
166
+ })
167
+ if (masterLangDetails &&
168
+ masterLangDetails["name"] &&
169
+ sourceMasterLangDetails[0]["name"] &&
170
+ masterLangDetails["name"].toString().toUpperCase() !== sourceMasterLangDetails[0]["name"].toString().toUpperCase()
171
+ ) {
172
+ cliux.print(
173
+ 'WARNING!!! The master language name for the source and destination is different.',
174
+ { color: 'yellow' },
175
+ );
176
+ cliux.print(
177
+ `Old Master language name: ${masterLangDetails["name"]}`,
178
+ { color: 'red' },
179
+ );
180
+ cliux.print(
181
+ `New Master language name: ${sourceMasterLangDetails[0]["name"]}`,
182
+ { color: 'green' },
183
+ );
184
+ let confirm = await cliux.inquire({
185
+ type: 'confirm',
186
+ message: 'Are you sure you want to update name of master language?',
187
+ name: 'confirmation',
188
+ })
189
+
190
+ if (confirm) {
191
+ let languid = sourceMasterLangDetails[0] && sourceMasterLangDetails[0]["uid"];
192
+ let lang = self.sourceMasterLanguages[languid];
193
+ if (!lang) return reject('Locale not found.!');
194
+ const langObj = self.stackAPIClient.locale(lang.code);
195
+ Object.assign(langObj, { name: lang.name });
196
+ langObj.update()
197
+ .then(() => {
198
+ helper.writeFileSync(self.langSuccessPath, self.success);
199
+ addlogs(self.config, chalk.green('Master Languages name have been updated successfully!'), 'success');
200
+ resolve();
201
+ })
202
+ .catch(function (error) {
203
+ reject(error);
204
+ });
205
+ } else {
206
+ resolve();
207
+ }
208
+ } else {
209
+ resolve();
210
+ }
211
+ });
212
+ }
150
213
  };