@contentstack/cli-cm-import 1.2.3 → 1.3.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) {
@@ -16,10 +16,8 @@ let helper = require('../util/fs');
16
16
  let { addlogs } = require('../util/log');
17
17
  const { formatError } = require('../util');
18
18
  let config = require('../../config/default');
19
- let stack = require('../util/contentstack-management-sdk');
20
19
 
21
20
  module.exports = class ImportLanguages {
22
- client;
23
21
  fails = [];
24
22
  success = [];
25
23
  langUidMapper = {};
@@ -27,9 +25,9 @@ module.exports = class ImportLanguages {
27
25
  langConfig = config.modules.locales;
28
26
  reqConcurrency = config.concurrency || config.fetchConcurrency || 1;
29
27
 
30
- constructor(credentialConfig) {
31
- this.config = merge(config, credentialConfig);
32
- this.client = stack.Client(this.config);
28
+ constructor(importConfig, stackAPIClient) {
29
+ this.config = merge(config, importConfig);
30
+ this.stackAPIClient = stackAPIClient;
33
31
  }
34
32
 
35
33
  start() {
@@ -68,8 +66,7 @@ module.exports = class ImportLanguages {
68
66
  },
69
67
  };
70
68
 
71
- return self.client
72
- .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
69
+ return self.stackAPIClient
73
70
  .locale()
74
71
  .create(requestOption)
75
72
  .then((locale) => {
@@ -131,9 +128,7 @@ module.exports = class ImportLanguages {
131
128
  requireKeys.forEach((e) => {
132
129
  lang[e] = _lang[e];
133
130
  });
134
- let langobj = self.client
135
- .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
136
- .locale(lang.code);
131
+ let langobj = self.stackAPIClient.locale(lang.code);
137
132
  Object.assign(langobj, cloneDeep(lang));
138
133
  langobj.update().then(() => {
139
134
  // empty function