@contentstack/cli-cm-import 1.1.0 → 1.2.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,113 +9,107 @@ 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 helper = require('../util/fs');
15
+ const { formatError } = require('../util');
15
16
  const { addlogs } = require('../util/log');
16
- let config = require('../../config/default');
17
- let stack = require('../util/contentstack-management-sdk');
18
- let reqConcurrency = config.concurrency;
19
- let webhooksConfig = config.modules.webhooks;
17
+ const config = require('../../config/default');
18
+ const stack = require('../util/contentstack-management-sdk');
20
19
 
21
- let webhooksFolderPath;
22
- let webMapperPath;
23
- let webUidMapperPath;
24
- let webSuccessPath;
25
- let webFailsPath;
26
- let client;
20
+ module.exports = class ImportWebhooks {
21
+ config;
22
+ fails = [];
23
+ success = [];
24
+ webUidMapper = {};
25
+ webhooksConfig = config.modules.webhooks;
26
+ reqConcurrency = config.concurrency || config.fetchConcurrency;
27
27
 
28
- function importWebhooks() {
29
- this.fails = [];
30
- this.success = [];
31
- this.webUidMapper = {};
32
- }
28
+ constructor(credentialConfig) {
29
+ this.config = merge(config, credentialConfig);
30
+ }
31
+
32
+ start() {
33
+ addlogs(this.config, chalk.white('Migrating webhooks'), 'success');
34
+
35
+ const self = this;
36
+ const client = stack.Client(this.config);
37
+
38
+ let webMapperPath = path.resolve(this.config.data, 'mapper', 'webhooks');
39
+ let webFailsPath = path.resolve(this.config.data, 'mapper', 'webhooks', 'fails.json');
40
+ let webSuccessPath = path.resolve(this.config.data, 'mapper', 'webhooks', 'success.json');
41
+ let webUidMapperPath = path.resolve(this.config.data, 'mapper', 'webhooks', 'uid-mapping.json');
42
+
43
+ let webhooksFolderPath = path.resolve(this.config.data, this.webhooksConfig.dirName);
44
+ this.webhooks = helper.readFileSync(path.resolve(webhooksFolderPath, this.webhooksConfig.fileName));
33
45
 
34
- importWebhooks.prototype = {
35
- start: function (credentialConfig) {
36
- let self = this;
37
- config = credentialConfig;
38
- addlogs(config, chalk.white('Migrating webhooks'), 'success');
39
- client = stack.Client(config);
40
- webhooksFolderPath = path.resolve(config.data, webhooksConfig.dirName);
41
- webMapperPath = path.resolve(config.data, 'mapper', 'webhooks');
42
- webUidMapperPath = path.resolve(config.data, 'mapper', 'webhooks', 'uid-mapping.json');
43
- webSuccessPath = path.resolve(config.data, 'mapper', 'webhooks', 'success.json');
44
- webFailsPath = path.resolve(config.data, 'mapper', 'webhooks', 'fails.json');
45
- self.webhooks = helper.readFile(path.resolve(webhooksFolderPath, webhooksConfig.fileName));
46
46
  if (fs.existsSync(webUidMapperPath)) {
47
- self.webUidMapper = helper.readFile(webUidMapperPath);
47
+ self.webUidMapper = helper.readFileSync(webUidMapperPath);
48
48
  self.webUidMapper = self.webUidMapper || {};
49
49
  }
50
+
50
51
  mkdirp.sync(webMapperPath);
51
52
 
52
53
  return new Promise(function (resolve, reject) {
53
54
  if (self.webhooks == undefined || isEmpty(self.webhooks)) {
54
- addlogs(config, chalk.white('No Webhooks Found'), 'success');
55
+ addlogs(self.config, chalk.white('No Webhooks Found'), 'success');
55
56
  return resolve({ empty: true });
56
57
  }
58
+
57
59
  let webUids = Object.keys(self.webhooks);
58
60
  return Promise.map(
59
61
  webUids,
60
62
  function (webUid) {
61
63
  let web = self.webhooks[webUid];
62
- if (config.importWebhookStatus !== 'current' || config.importWebhookStatus === 'disable') {
64
+ if (self.config.importWebhookStatus !== 'current' || self.config.importWebhookStatus === 'disable') {
63
65
  web.disabled = true;
64
66
  }
65
67
 
66
68
  if (!self.webUidMapper.hasOwnProperty(webUid)) {
67
- let requestOption = {
68
- json: {
69
- webhook: web,
70
- },
71
- };
69
+ let requestOption = { json: { webhook: web } };
72
70
 
73
71
  return client
74
- .stack({ api_key: config.target_stack, management_token: config.management_token })
72
+ .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
75
73
  .webhook()
76
74
  .create(requestOption.json)
77
75
  .then(function (response) {
78
76
  self.success.push(response);
79
77
  self.webUidMapper[webUid] = response.uid;
80
- helper.writeFile(webUidMapperPath, self.webUidMapper);
78
+ helper.writeFileSync(webUidMapperPath, self.webUidMapper);
81
79
  })
82
80
  .catch(function (err) {
83
81
  let error = JSON.parse(err.message);
84
82
  self.fails.push(web);
85
83
  addlogs(
86
- config,
87
- chalk.red("Webhooks: '" + web.name + "' failed to be import\n" + JSON.stringify(error)),
84
+ self.config,
85
+ chalk.red("Webhooks: '" + web.name + "' failed to be import\n" + formatError(error)),
88
86
  'error',
89
87
  );
90
88
  });
91
89
  } else {
92
90
  // the webhooks has already been created
93
91
  addlogs(
94
- config,
92
+ self.config,
95
93
  chalk.white("The Webhooks: '" + web.name + "' already exists. Skipping it to avoid duplicates!"),
96
94
  'success',
97
95
  );
98
96
  }
99
97
  // import 2 webhooks at a time
100
98
  },
101
- {
102
- concurrency: reqConcurrency,
103
- },
99
+ { concurrency: self.reqConcurrency },
104
100
  )
105
101
  .then(function () {
106
102
  // webhooks have imported successfully
107
- helper.writeFile(webSuccessPath, self.success);
108
- addlogs(config, chalk.green('Webhooks have been imported successfully!'), 'success');
103
+ helper.writeFileSync(webSuccessPath, self.success);
104
+ addlogs(self.config, chalk.green('Webhooks have been imported successfully!'), 'success');
109
105
  return resolve();
110
106
  })
111
107
  .catch(function (error) {
112
108
  // error while importing environments
113
- helper.writeFile(webFailsPath, self.fails);
114
- addlogs(config, chalk.red('Webhooks import failed'), 'error');
109
+ helper.writeFileSync(webFailsPath, self.fails);
110
+ addlogs(self.config, chalk.red('Webhooks import failed'), 'error');
115
111
  return reject(error);
116
112
  });
117
113
  });
118
- },
114
+ }
119
115
  };
120
-
121
- module.exports = new importWebhooks();
@@ -9,49 +9,49 @@ 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 helper = require('../util/fs');
15
+ const { formatError } = require('../util');
15
16
  const { addlogs } = require('../util/log');
16
17
  let config = require('../../config/default');
17
18
  let stack = require('../util/contentstack-management-sdk');
18
19
 
19
- let reqConcurrency = config.concurrency;
20
- let workflowConfig = config.modules.workflows;
21
- let workflowFolderPath;
22
- let workflowMapperPath;
23
- let workflowUidMapperPath;
24
- let workflowSuccessPath;
25
- let workflowFailsPath;
26
- let client;
27
-
28
- function importWorkflows() {
29
- this.fails = [];
30
- this.success = [];
31
- this.workflowUidMapper = {};
32
- this.labelUids = [];
33
- if (fs.existsSync(workflowUidMapperPath)) {
34
- this.workflowUidMapper = helper.readFile(workflowUidMapperPath);
35
- this.workflowUidMapper = this.workflowUidMapper || {};
20
+ module.exports = class importWorkflows {
21
+ client;
22
+ fails = [];
23
+ success = [];
24
+ workflowUidMapper = {};
25
+ workflowConfig = config.modules.workflows;
26
+ reqConcurrency = config.concurrency || config.fetchConcurrency || 1;
27
+
28
+ constructor(credentialConfig) {
29
+ this.config = merge(config, credentialConfig);
30
+ this.client = stack.Client(this.config);
36
31
  }
37
- }
38
32
 
39
- importWorkflows.prototype = {
40
- start: function (credentialConfig) {
33
+ start() {
34
+ addlogs(this.config, chalk.white('Migrating workflows'), 'success');
35
+
41
36
  let self = this;
42
- config = credentialConfig;
43
- client = stack.Client(config);
44
- addlogs(config, chalk.white('Migrating workflows'), 'success');
45
- workflowFolderPath = path.resolve(config.data, workflowConfig.dirName);
46
- self.workflows = helper.readFile(path.resolve(workflowFolderPath, workflowConfig.fileName));
47
- workflowMapperPath = path.resolve(config.data, 'mapper', 'workflows');
48
- workflowUidMapperPath = path.resolve(config.data, 'mapper', 'workflows', 'uid-mapping.json');
49
- workflowSuccessPath = path.resolve(config.data, 'workflows', 'success.json');
50
- workflowFailsPath = path.resolve(config.data, 'workflows', 'fails.json');
37
+ let workflowMapperPath = path.resolve(this.config.data, 'mapper', 'workflows');
38
+ let workflowFailsPath = path.resolve(this.config.data, 'workflows', 'fails.json');
39
+ let workflowSuccessPath = path.resolve(this.config.data, 'workflows', 'success.json');
40
+ let workflowUidMapperPath = path.resolve(this.config.data, 'mapper', 'workflows', 'uid-mapping.json');
41
+ let workflowFolderPath = path.resolve(this.config.data, this.workflowConfig.dirName);
42
+
43
+ self.workflows = helper.readFileSync(path.resolve(workflowFolderPath, this.workflowConfig.fileName));
44
+
45
+ if (fs.existsSync(workflowUidMapperPath)) {
46
+ this.workflowUidMapper = helper.readFileSync(workflowUidMapperPath);
47
+ this.workflowUidMapper = this.workflowUidMapper || {};
48
+ }
49
+
51
50
  mkdirp.sync(workflowMapperPath);
51
+
52
52
  return new Promise(function (resolve, reject) {
53
53
  if (self.workflows == undefined || isEmpty(self.workflows)) {
54
- addlogs(config, chalk.white('No workflow Found'), 'success');
54
+ addlogs(self.config, chalk.white('No workflow Found'), 'success');
55
55
  return resolve({ empty: true });
56
56
  }
57
57
  self.workflowsUids = Object.keys(self.workflows);
@@ -61,18 +61,19 @@ importWorkflows.prototype = {
61
61
  let workflow = self.workflows[workflowUid];
62
62
 
63
63
  if (!self.workflowUidMapper.hasOwnProperty(workflowUid)) {
64
- const workflowStages = workflow.workflow_stages;
65
64
  const roleNameMap = {};
66
- const roles = await client.stack({ api_key: config.target_stack, management_token: config.management_token }).role().fetchAll();
65
+ const workflowStages = workflow.workflow_stages;
66
+ const roles = await self.client
67
+ .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
68
+ .role()
69
+ .fetchAll();
70
+
67
71
  for (const role of roles.items) {
68
72
  roleNameMap[role.name] = role.uid;
69
73
  }
70
- for (let i = 0; i < workflowStages.length; i++) {
71
- const stage = workflowStages[i];
72
- if (
73
- stage.SYS_ACL.users.uids.length &&
74
- stage.SYS_ACL.users.uids[0] !== '$all'
75
- ) {
74
+
75
+ for (const stage of workflowStages) {
76
+ if (stage.SYS_ACL.users.uids.length && stage.SYS_ACL.users.uids[0] !== '$all') {
76
77
  stage.SYS_ACL.users.uids = ['$all'];
77
78
  }
78
79
 
@@ -82,16 +83,19 @@ importWorkflows.prototype = {
82
83
  const roleData = stage.SYS_ACL.roles.uids[i];
83
84
  if (!roleNameMap[roleData.name]) {
84
85
  // rules.branch is required to create custom roles.
85
- const branchRuleExists = roleData.rules.find(rule => rule.module === 'branch');
86
+ const branchRuleExists = roleData.rules.find((rule) => rule.module === 'branch');
86
87
  if (!branchRuleExists) {
87
88
  roleData.rules.push({
88
89
  module: 'branch',
89
90
  branches: ['main'],
90
- acl: { read: true }
91
+ acl: { read: true },
91
92
  });
92
93
  }
93
94
 
94
- const role = await client.stack({ api_key: config.target_stack, management_token: config.management_token }).role().create({ role: roleData });
95
+ const role = await self.client
96
+ .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
97
+ .role()
98
+ .create({ role: roleData });
95
99
  stage.SYS_ACL.roles.uids[i] = role.uid;
96
100
  roleNameMap[roleData.name] = role.uid;
97
101
  } else {
@@ -99,14 +103,22 @@ importWorkflows.prototype = {
99
103
  }
100
104
  }
101
105
  } catch (error) {
102
- addlogs(config, chalk.red('Error while importing workflows roles. ' + error && error.message), 'error');
103
- return reject({ message: 'Error while importing workflows roles' });
106
+ addlogs(
107
+ self.config,
108
+ chalk.red('Error while importing workflows roles. ' + formatError(error)),
109
+ 'error',
110
+ );
111
+ reject({ message: 'Error while importing workflows roles' });
104
112
  }
105
113
  }
106
114
  }
107
115
 
108
116
  if (workflow.admin_users !== undefined) {
109
- addlogs(config, chalk.yellow('We are skipping import of `Workflow superuser(s)` from workflow'), 'info');
117
+ addlogs(
118
+ self.config,
119
+ chalk.yellow('We are skipping import of `Workflow superuser(s)` from workflow'),
120
+ 'info',
121
+ );
110
122
  delete workflow.admin_users;
111
123
  }
112
124
  // One branch is required to create workflow.
@@ -114,55 +126,53 @@ importWorkflows.prototype = {
114
126
  workflow.branches = ['main'];
115
127
  }
116
128
 
117
- return client
118
- .stack({ api_key: config.target_stack, management_token: config.management_token })
129
+ return self.client
130
+ .stack({ api_key: self.config.target_stack, management_token: self.config.management_token })
119
131
  .workflow()
120
132
  .create({ workflow })
121
133
  .then(function (response) {
122
134
  self.workflowUidMapper[workflowUid] = response;
123
- helper.writeFile(workflowUidMapperPath, self.workflowUidMapper);
135
+ helper.writeFileSync(workflowUidMapperPath, self.workflowUidMapper);
124
136
  })
125
137
  .catch(function (error) {
126
138
  self.fails.push(workflow);
127
139
  if (error.errors.name) {
128
- addlogs(config, chalk.red("workflow: '" + workflow.name + "' already exist"), 'error');
140
+ addlogs(self.config, chalk.red("workflow: '" + workflow.name + "' already exist"), 'error');
129
141
  } else if (error.errors['workflow_stages.0.users']) {
130
142
  addlogs(
131
- config,
143
+ self.config,
132
144
  chalk.red(
133
145
  "Failed to import Workflows as you've specified certain roles in the Stage transition and access rules section. We currently don't import roles to the stack.",
134
146
  ),
135
147
  'error',
136
148
  );
137
149
  } else {
138
- addlogs(config, chalk.red("workflow: '" + workflow.name + "' failed"), 'error');
150
+ addlogs(self.config, chalk.red("workflow: '" + workflow.name + "' failed"), 'error');
139
151
  }
140
152
  });
141
153
  } else {
142
154
  // the workflow has already been created
143
155
  addlogs(
144
- config,
156
+ self.config,
145
157
  chalk.white("The Workflows: '" + workflow.name + "' already exists. Skipping it to avoid duplicates!"),
146
158
  'success',
147
159
  );
148
160
  }
149
161
  // import 1 workflows at a time
150
162
  },
151
- {
152
- concurrency: reqConcurrency,
153
- },
163
+ { concurrency: self.reqConcurrency },
154
164
  )
155
165
  .then(function () {
156
- helper.writeFile(workflowSuccessPath, self.success);
157
- addlogs(config, chalk.green('Workflows have been imported successfully!'), 'success');
158
- return resolve();
166
+ helper.writeFileSync(workflowSuccessPath, self.success);
167
+ addlogs(self.config, chalk.green('Workflows have been imported successfully!'), 'success');
168
+ resolve();
159
169
  })
160
170
  .catch(function (error) {
161
- helper.writeFile(workflowFailsPath, self.fails);
162
- addlogs(config, chalk.red('Workflows import failed'), 'error');
171
+ helper.writeFileSync(workflowFailsPath, self.fails);
172
+ addlogs(self.config, chalk.red('Workflows import failed'), 'error');
173
+ addlogs(self.config, formatError(error), 'error');
163
174
  return reject(error);
164
175
  });
165
176
  });
166
- },
177
+ }
167
178
  };
168
- module.exports = new importWorkflows();
@@ -6,7 +6,7 @@
6
6
 
7
7
  // eslint-disable-next-line unicorn/filename-case
8
8
  let path = require('path');
9
- const _ = require('lodash')
9
+ const _ = require('lodash');
10
10
  let helper = require('./fs');
11
11
  let util = require('../util');
12
12
  let config = util.getConfig();
@@ -39,33 +39,33 @@ let extension_uid_Replace = (module.exports = function (schema, preserveStackVer
39
39
  }
40
40
  } else if (schema[i].data_type === 'global_field') {
41
41
  let global_fields_key_value = schema[i].reference_to;
42
- let global_fields_data = helper.readFile(path.join(globalfieldsPath));
42
+ let global_fields_data = helper.readFileSync(path.join(globalfieldsPath));
43
43
  if (global_fields_data && global_fields_data.hasOwnProperty(global_fields_key_value)) {
44
44
  schema[i].reference_to = global_fields_data[global_fields_key_value];
45
45
  }
46
46
  } else if (schema[i].hasOwnProperty('extension_uid')) {
47
47
  const extension_key_value = schema[i].extension_uid;
48
- const data = helper.readFile(path.join(extensionPath));
48
+ const data = helper.readFileSync(path.join(extensionPath));
49
49
  if (data && data.hasOwnProperty(extension_key_value)) {
50
50
  // eslint-disable-next-line camelcase
51
51
  schema[i].extension_uid = data[extension_key_value];
52
52
  } else if (schema[i].field_metadata && schema[i].field_metadata.extension) {
53
53
  if (installedExtensions) {
54
- const marketplaceApps = helper.readFile(marketplaceAppPath);
55
- const oldExt = _.find(marketplaceApps, { uid: schema[i].extension_uid })
54
+ const marketplaceApps = helper.readFileSync(marketplaceAppPath);
55
+ const oldExt = _.find(marketplaceApps, { uid: schema[i].extension_uid });
56
56
 
57
57
  if (oldExt) {
58
- const ext = _.find(installedExtensions, { type: 'field', title: oldExt.title, app_uid: oldExt.app_uid })
58
+ const ext = _.find(installedExtensions, { type: 'field', title: oldExt.title, app_uid: oldExt.app_uid });
59
59
 
60
60
  if (ext) {
61
- schema[i].extension_uid = ext.uid
61
+ schema[i].extension_uid = ext.uid;
62
62
  }
63
63
  }
64
64
  }
65
65
  }
66
66
  } else if (schema[i].data_type === 'json' && schema[i].hasOwnProperty('plugins') && schema[i].plugins.length > 0) {
67
67
  const newPluginUidsArray = [];
68
- const data = helper.readFile(path.join(extensionPath));
68
+ const data = helper.readFileSync(path.join(extensionPath));
69
69
  schema[i].plugins.forEach((extension_key_value) => {
70
70
  if (data && data.hasOwnProperty(extension_key_value)) {
71
71
  newPluginUidsArray.push(data[extension_key_value]);
@@ -74,4 +74,4 @@ let extension_uid_Replace = (module.exports = function (schema, preserveStackVer
74
74
  schema[i].plugins = newPluginUidsArray;
75
75
  }
76
76
  }
77
- })
77
+ });
@@ -7,33 +7,112 @@
7
7
  var fs = require('fs');
8
8
  var path = require('path');
9
9
  var mkdirp = require('mkdirp');
10
+ const bigJSON = require('big-json');
10
11
 
11
- exports.readFile = function (filePath, parse) {
12
- var data;
12
+ exports.readFileSync = function (filePath, parse) {
13
+ let data;
13
14
  parse = typeof parse === 'undefined' ? true : parse;
14
15
  filePath = path.resolve(filePath);
15
16
  if (fs.existsSync(filePath)) {
16
- data = parse ? JSON.parse(fs.readFileSync(filePath, 'utf-8')) : data;
17
+ try {
18
+ data = parse ? JSON.parse(fs.readFileSync(filePath, 'utf-8')) : data;
19
+ } catch (error) {
20
+ return data;
21
+ }
17
22
  }
18
23
  return data;
19
24
  };
20
25
 
21
- exports.writeFile = function (filePath, data) {
26
+ // by default file type is json
27
+ exports.readFile = async (filePath, options = { type: 'json' }) => {
28
+ return new Promise((resolve, reject) => {
29
+ filePath = path.resolve(filePath);
30
+ fs.readFile(filePath, 'utf-8', (error, data) => {
31
+ if (error) {
32
+ if (error.code === 'ENOENT') {
33
+ return resolve();
34
+ }
35
+ reject(error);
36
+ } else {
37
+ if (options.type !== 'json') {
38
+ return resolve(data);
39
+ }
40
+ resolve(JSON.parse(data));
41
+ }
42
+ });
43
+ });
44
+ };
45
+
46
+ exports.writeFileSync = function (filePath, data) {
22
47
  data = typeof data === 'object' ? JSON.stringify(data) : data || '{}';
23
48
  fs.writeFileSync(filePath, data);
24
49
  };
25
50
 
26
- exports.makeDirectory = function () {
27
- for (var key in arguments) {
28
- var dirname = path.resolve(arguments[key]);
29
- if (!fs.existsSync(dirname)) {
30
- mkdirp.sync(dirname);
31
- }
51
+ exports.writeFile = function (filePath, data) {
52
+ return new Promise((resolve, reject) => {
53
+ data = typeof data === 'object' ? JSON.stringify(data) : data || '{}';
54
+ fs.writeFile(filePath, data, (error) => {
55
+ if (error) {
56
+ return reject(error);
57
+ }
58
+ resolve('done');
59
+ });
60
+ });
61
+ };
62
+
63
+ exports.makeDirectory = async function (path) {
64
+ if (!path) {
65
+ throw new Error('Invalid path to create directory');
66
+ }
67
+ return mkdirp(path);
68
+ };
69
+
70
+ exports.readLargeFile = function (filePath, opts = {}) {
71
+ if (typeof filePath !== 'string') {
72
+ return;
73
+ }
74
+ filePath = path.resolve(filePath);
75
+ if (fs.existsSync(filePath)) {
76
+ return new Promise((resolve, reject) => {
77
+ const readStream = fs.createReadStream(filePath, { encoding: 'utf-8' });
78
+ const parseStream = bigJSON.createParseStream();
79
+ parseStream.on('data', function (data) {
80
+ if (opts.type === 'array') {
81
+ return resolve(Object.values(data));
82
+ }
83
+ resolve(data);
84
+ });
85
+ parseStream.on('error', function (error) {
86
+ console.log('error', error);
87
+ reject(error);
88
+ });
89
+ readStream.pipe(parseStream);
90
+ });
32
91
  }
33
92
  };
34
93
 
35
- exports.readdir = function (dirPath) {
36
- if (fs.existsSync(path)) {
94
+ exports.writeLargeFile = function (filePath, data) {
95
+ if (typeof filePath !== 'string' || typeof data !== 'object') {
96
+ return;
97
+ }
98
+ filePath = path.resolve(filePath);
99
+ return new Promise((resolve, reject) => {
100
+ const stringifyStream = bigJSON.createStringifyStream({
101
+ body: data,
102
+ });
103
+ var writeStream = fs.createWriteStream(filePath, 'utf-8');
104
+ stringifyStream.pipe(writeStream);
105
+ writeStream.on('finish', () => {
106
+ resolve();
107
+ });
108
+ writeStream.on('error', (error) => {
109
+ reject(error);
110
+ });
111
+ });
112
+ };
113
+
114
+ exports.readdirSync = function (dirPath) {
115
+ if (fs.existsSync(dirPath)) {
37
116
  return fs.readdirSync(dirPath);
38
117
  } else {
39
118
  return [];
@@ -13,6 +13,7 @@ var chalk = require('chalk');
13
13
  var { addlogs } = require('./log');
14
14
  var defaultConfig = require('../../config/default');
15
15
  const stack = require('./contentstack-management-sdk');
16
+ const promiseLimit = require('promise-limit');
16
17
  var config;
17
18
 
18
19
  exports.initialization = function (configData) {
@@ -69,7 +70,7 @@ exports.sanitizeStack = function (importConfig) {
69
70
  importConfig.modules.stack.fileName,
70
71
  );
71
72
 
72
- const oldStackDetails = fs.readFile(stackFilePath);
73
+ const oldStackDetails = fs.readFileSync(stackFilePath);
73
74
  if (!oldStackDetails || !oldStackDetails.settings || !oldStackDetails.settings.hasOwnProperty('version')) {
74
75
  throw new Error(`${JSON.stringify(oldStackDetails)} is invalid!`);
75
76
  }
@@ -129,7 +130,7 @@ exports.field_rules_update = function (importConfig, ctPath) {
129
130
  return new Promise(function (resolve, reject) {
130
131
  let client = stack.Client(importConfig);
131
132
 
132
- fs.readFile(path.join(ctPath + '/field_rules_uid.json'), async (err, data) => {
133
+ fs.readFileSync(path.join(ctPath + '/field_rules_uid.json'), async (err, data) => {
133
134
  if (err) {
134
135
  throw err;
135
136
  }
@@ -152,7 +153,7 @@ exports.field_rules_update = function (importConfig, ctPath) {
152
153
  let updatedValue = [];
153
154
  for (let j = 0; j < fieldRulesArray.length; j++) {
154
155
  let splitedFieldRulesValue = fieldRulesArray[j];
155
- let oldUid = helper.readFile(path.join(entryUidMapperPath));
156
+ let oldUid = helper.readFileSync(path.join(entryUidMapperPath));
156
157
  if (oldUid.hasOwnProperty(splitedFieldRulesValue)) {
157
158
  updatedValue.push(oldUid[splitedFieldRulesValue]);
158
159
  } else {
@@ -185,3 +186,38 @@ exports.field_rules_update = function (importConfig, ctPath) {
185
186
  exports.getConfig = function () {
186
187
  return config;
187
188
  };
189
+
190
+ exports.formatError = function (error) {
191
+ try {
192
+ if (typeof error === 'string') {
193
+ error = JSON.parse(error);
194
+ } else {
195
+ error = JSON.parse(error.message);
196
+ }
197
+ } catch (e) {}
198
+ let message = error.errorMessage || error.error_message || error.message || error;
199
+ if (error.errors && Object.keys(error.errors).length > 0) {
200
+ Object.keys(error.errors).forEach((e) => {
201
+ let entity = e;
202
+ if (e === 'authorization') entity = 'Management Token';
203
+ if (e === 'api_key') entity = 'Stack API key';
204
+ if (e === 'uid') entity = 'Content Type';
205
+ if (e === 'access_token') entity = 'Delivery Token';
206
+ message += ' ' + [entity, error.errors[e]].join(' ');
207
+ });
208
+ }
209
+ return message;
210
+ };
211
+
212
+ exports.executeTask = function (tasks = [], handler, options) {
213
+ if (typeof handler !== 'function') {
214
+ throw new Error('Invalid handler');
215
+ }
216
+ const { concurrency = 1 } = options;
217
+ const limit = promiseLimit(concurrency);
218
+ return Promise.all(
219
+ tasks.map((task) => {
220
+ return limit(() => handler(task));
221
+ }),
222
+ );
223
+ };