@contentstack/cli-cm-export 0.1.1-beta.9 → 1.0.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.
@@ -4,64 +4,89 @@
4
4
  * MIT Licensed
5
5
  */
6
6
 
7
- var chalk = require('chalk')
8
- var mkdirp = require('mkdirp')
9
- var path = require('path')
7
+ var chalk = require('chalk');
8
+ var mkdirp = require('mkdirp');
9
+ var path = require('path');
10
10
 
11
- var app = require('../../app')
12
- var helper = require('../util/helper')
13
- var {addlogs} = require('../util/log')
14
- const stack = require('../util/contentstack-management-sdk')
11
+ var app = require('../../app');
12
+ var helper = require('../util/helper');
13
+ var { addlogs } = require('../util/log');
14
+ const stack = require('../util/contentstack-management-sdk');
15
15
 
16
+ let config = require('../../config/default');
16
17
 
17
- let config = require('../../config/default')
18
+ var stackConfig = config.modules.stack;
19
+ let client;
18
20
 
19
- var stackConfig = config.modules.stack
20
- let client
21
-
22
- function ExportStack () {
21
+ function ExportStack() {
23
22
  this.requestOption = {
24
23
  uri: config.host + config.apis.stacks,
25
24
  headers: config.headers,
26
- json: true
27
- }
25
+ json: true,
26
+ };
28
27
  }
29
28
 
30
29
  ExportStack.prototype.start = function (credentialConfig) {
31
- config = credentialConfig
32
- client = stack.Client(config)
33
- if (!config.preserveStackVersion && !config.hasOwnProperty("master_locale")) {
34
- return new Promise((resolve, reject) => {
35
- var result = client.stack({ api_key: credentialConfig.source_stack, management_token: credentialConfig.management_token }).locale().query()
36
- result.find()
37
- .then(response => {
38
- var masterLocalObj = response.items.filter(obj => {
39
- if (obj.fallback_locale === null) {
40
- return obj
41
- }
42
- });
43
- return resolve(masterLocalObj[0])
44
- }).catch(error => {
45
- return reject(error)
46
- })
47
- })
48
- } else if(config.preserveStackVersion) {
49
- addlogs(config, 'Exporting stack details', 'success')
50
- let stackFolderPath = path.resolve(config.data, stackConfig.dirName)
51
- let stackContentsFile = path.resolve(stackFolderPath, stackConfig.fileName)
52
-
53
- mkdirp.sync(stackFolderPath)
54
-
30
+ config = credentialConfig;
31
+ client = stack.Client(config);
32
+ const self = this
33
+ if (!config.preserveStackVersion && !config.hasOwnProperty('master_locale')) {
34
+ const apiDetails = {
35
+ limit: 100,
36
+ skip: 0,
37
+ include_count: true,
38
+ }
39
+ return self.getLocales(apiDetails)
40
+ } else if (config.preserveStackVersion) {
41
+ addlogs(config, 'Exporting stack details', 'success');
42
+ let stackFolderPath = path.resolve(config.data, stackConfig.dirName);
43
+ let stackContentsFile = path.resolve(stackFolderPath, stackConfig.fileName);
44
+
45
+ mkdirp.sync(stackFolderPath);
46
+
55
47
  return new Promise((resolve, reject) => {
56
- return client.stack({api_key: config.source_stack}).fetch()
57
- .then(response => {
58
- helper.writeFile(stackContentsFile, response)
59
- addlogs(config, 'Exported stack details successfully!', 'success')
60
- return resolve(response)
61
- })
62
- .catch(reject)
63
- })
48
+ return client
49
+ .stack({ api_key: config.source_stack })
50
+ .fetch()
51
+ .then((response) => {
52
+ helper.writeFile(stackContentsFile, response);
53
+ addlogs(config, 'Exported stack details successfully!', 'success');
54
+ return resolve(response);
55
+ })
56
+ .catch(reject);
57
+ });
64
58
  }
59
+ };
60
+
61
+ ExportStack.prototype.getLocales = function (apiDetails) {
62
+ let self = this
63
+ return new Promise((resolve, reject) => {
64
+ const result = client
65
+ .stack({ api_key: config.source_stack, management_token: config.management_token })
66
+ .locale()
67
+ .query(apiDetails)
68
+
69
+ result
70
+ .find()
71
+ .then((response) => {
72
+ const masterLocalObj = response.items.find((obj) => {
73
+ if (obj.fallback_locale === null) {
74
+ return obj;
75
+ }
76
+ });
77
+ apiDetails.skip += apiDetails.limit;
78
+ if (masterLocalObj) { return resolve(masterLocalObj); }
79
+ else if (apiDetails.skip <= response.count) {
80
+ return resolve(self.getLocales(apiDetails))
81
+ }
82
+ else {
83
+ return reject('Master locale not found');
84
+ }
85
+ })
86
+ .catch((error) => {
87
+ return reject(error);
88
+ });
89
+ });
65
90
  }
66
91
 
67
- module.exports = new ExportStack()
92
+ module.exports = new ExportStack();
@@ -1,20 +1,20 @@
1
1
  /*!
2
- * Contentstack Export
3
- * Copyright (c) 2019 Contentstack LLC
4
- * MIT Licensed
5
- */
2
+ * Contentstack Export
3
+ * Copyright (c) 2019 Contentstack LLC
4
+ * MIT Licensed
5
+ */
6
6
 
7
- const mkdirp = require('mkdirp')
8
- const path = require('path')
9
- const chalk = require('chalk')
7
+ const mkdirp = require('mkdirp');
8
+ const path = require('path');
9
+ const chalk = require('chalk');
10
10
 
11
- const helper = require('../util/helper')
12
- const {addlogs} = require('../util/log')
11
+ const helper = require('../util/helper');
12
+ const { addlogs } = require('../util/log');
13
13
 
14
- let config = require('../../config/default')
15
- const stack = require('../util/contentstack-management-sdk')
16
- const webhooksConfig = config.modules.webhooks
17
- let client
14
+ let config = require('../../config/default');
15
+ const stack = require('../util/contentstack-management-sdk');
16
+ const webhooksConfig = config.modules.webhooks;
17
+ let client;
18
18
 
19
19
  // Create folder for environments
20
20
 
@@ -22,46 +22,54 @@ function ExportWebhooks() {
22
22
  this.requestOptions = {
23
23
  include_count: true,
24
24
  asc: 'updated_at',
25
- }
26
- this.master = {}
27
- this.webhooks = {}
25
+ };
26
+ this.master = {};
27
+ this.webhooks = {};
28
28
  }
29
29
 
30
30
  ExportWebhooks.prototype.start = function (credentialConfig) {
31
- addlogs(config, 'Starting webhooks export', 'success')
32
- this.master = {}
33
- this.webhooks = {}
34
- let self = this
35
- config = credentialConfig
36
- client = stack.Client(config)
37
- const webhooksFolderPath = path.resolve(config.data, (config.branchName || ""), webhooksConfig.dirName)
38
- mkdirp.sync(webhooksFolderPath)
31
+ addlogs(config, 'Starting webhooks export', 'success');
32
+ this.master = {};
33
+ this.webhooks = {};
34
+ let self = this;
35
+ config = credentialConfig;
36
+ client = stack.Client(config);
37
+ const webhooksFolderPath = path.resolve(config.data, config.branchName || '', webhooksConfig.dirName);
38
+ mkdirp.sync(webhooksFolderPath);
39
39
  return new Promise(function (resolve, reject) {
40
- client.stack({api_key: config.source_stack, management_token: config.management_token}).webhook().fetchAll(self.requestOptions)
41
- .then(webhooks => {
42
- if (webhooks.items.length !== 0) {
43
- for (var i = 0, total = webhooks.count; i < total; i++) {
44
- const webUid = webhooks.items[i].uid
45
- self.master[webUid] = ''
46
- self.webhooks[webUid] = webhooks.items[i]
47
- delete self.webhooks[webUid].uid
48
- delete self.webhooks[webUid].SYS_ACL
40
+ client
41
+ .stack({ api_key: config.source_stack, management_token: config.management_token })
42
+ .webhook()
43
+ .fetchAll(self.requestOptions)
44
+ .then((webhooks) => {
45
+ if (webhooks.items.length !== 0) {
46
+ for (var i = 0, total = webhooks.count; i < total; i++) {
47
+ const webUid = webhooks.items[i].uid;
48
+ self.master[webUid] = '';
49
+ self.webhooks[webUid] = webhooks.items[i];
50
+ delete self.webhooks[webUid].uid;
51
+ delete self.webhooks[webUid].SYS_ACL;
52
+ }
53
+ helper.writeFile(path.join(webhooksFolderPath, webhooksConfig.fileName), self.webhooks);
54
+ addlogs(config, chalk.green('All the webhooks have been exported successfully'), 'success');
55
+ return resolve();
49
56
  }
50
- helper.writeFile(path.join(webhooksFolderPath, webhooksConfig.fileName), self.webhooks)
51
- addlogs(config, chalk.green('All the webhooks have been exported successfully'), 'success')
52
- return resolve()
53
- }
54
- addlogs(config, 'No webhooks found', 'success')
55
- return resolve()
56
- }).catch(function (error) {
57
- if (error.statusCode === 401) {
58
- addlogs(config, chalk.red('You are not allowed to export webhooks, Unless you provide email and password in config'), 'error')
59
- return resolve()
60
- }
61
- addlogs(config, error, 'error')
62
- return reject()
63
- })
64
- })
65
- }
57
+ addlogs(config, 'No webhooks found', 'success');
58
+ return resolve();
59
+ })
60
+ .catch(function (error) {
61
+ if (error.statusCode === 401) {
62
+ addlogs(
63
+ config,
64
+ chalk.red('You are not allowed to export webhooks, Unless you provide email and password in config'),
65
+ 'error',
66
+ );
67
+ return resolve();
68
+ }
69
+ addlogs(config, error, 'error');
70
+ return reject();
71
+ });
72
+ });
73
+ };
66
74
 
67
- module.exports = new ExportWebhooks()
75
+ module.exports = new ExportWebhooks();
@@ -4,57 +4,67 @@
4
4
  * MIT Licensed
5
5
  */
6
6
 
7
- const mkdirp = require('mkdirp')
8
- const path = require('path')
9
- const chalk = require('chalk')
7
+ const mkdirp = require('mkdirp');
8
+ const path = require('path');
9
+ const chalk = require('chalk');
10
10
 
11
- let helper = require('../util/helper')
12
- let {addlogs} = require('../util/log')
11
+ let helper = require('../util/helper');
12
+ let { addlogs } = require('../util/log');
13
13
 
14
- const stack = require('../util/contentstack-management-sdk')
15
- let config = require('../../config/default')
16
- let workFlowConfig = config.modules.workflows
17
- let client
14
+ const stack = require('../util/contentstack-management-sdk');
15
+ let config = require('../../config/default');
16
+ let workFlowConfig = config.modules.workflows;
17
+ let client;
18
18
 
19
19
  function ExportWorkFlows() {
20
- this.workflows = {}
20
+ this.workflows = {};
21
21
  }
22
22
 
23
23
  ExportWorkFlows.prototype.start = function (credentialConfig) {
24
- addlogs(config, 'Starting workflow export', 'success')
25
- this.workflows = {}
26
- let self = this
27
- config = credentialConfig
28
- client = stack.Client(config)
24
+ addlogs(config, 'Starting workflow export', 'success');
25
+ this.workflows = {};
26
+ let self = this;
27
+ config = credentialConfig;
28
+ client = stack.Client(config);
29
29
 
30
- let workflowsFolderPath = path.resolve(config.data, (config.branchName || ""), workFlowConfig.dirName)
31
- mkdirp.sync(workflowsFolderPath)
32
- return new Promise(function (resolve, reject) {
33
- return client.stack({api_key: config.source_stack, management_token: config.management_token}).workflow().fetchAll()
34
- .then(response => {
35
- if (response.items.length !== 0) {
36
- response.items.forEach(function (workflow) {
37
- addlogs(config, workflow.name + ' workflow was exported successfully', 'success')
38
- self.workflows[workflow.uid] = workflow
39
- let deleteItems = config.modules.workflows.invalidKeys
40
- deleteItems.forEach(e => delete workflow[e])
41
- })
42
- addlogs(config, chalk.green('All the workflow have been exported successfully'), 'success')
43
- }
44
- if (response.items.length === 0) {
45
- addlogs(config, 'No workflow were found in the Stack', 'success')
46
- }
47
- helper.writeFile(path.join(workflowsFolderPath, workFlowConfig.fileName), self.workflows)
48
- return resolve()
49
- }).catch(function (error) {
50
- if (error.statusCode === 401) {
51
- addlogs(config, chalk.red('You are not allowed to export workflow, Unless you provide email and password in config', 'error'))
52
- return resolve()
53
- }
54
- addlogs(config, error, 'error')
55
- return resolve()
56
- })
57
- })
58
- }
30
+ let workflowsFolderPath = path.resolve(config.data, config.branchName || '', workFlowConfig.dirName);
31
+ mkdirp.sync(workflowsFolderPath);
32
+ return new Promise(function (resolve) {
33
+ return client
34
+ .stack({ api_key: config.source_stack, management_token: config.management_token })
35
+ .workflow()
36
+ .fetchAll()
37
+ .then((response) => {
38
+ if (response.items.length !== 0) {
39
+ response.items.forEach(function (workflow) {
40
+ addlogs(config, workflow.name + ' workflow was exported successfully', 'success');
41
+ self.workflows[workflow.uid] = workflow;
42
+ let deleteItems = config.modules.workflows.invalidKeys;
43
+ deleteItems.forEach((e) => delete workflow[e]);
44
+ });
45
+ addlogs(config, chalk.green('All the workflow have been exported successfully'), 'success');
46
+ }
47
+ if (response.items.length === 0) {
48
+ addlogs(config, 'No workflow were found in the Stack', 'success');
49
+ }
50
+ helper.writeFile(path.join(workflowsFolderPath, workFlowConfig.fileName), self.workflows);
51
+ return resolve();
52
+ })
53
+ .catch(function (error) {
54
+ if (error.statusCode === 401) {
55
+ addlogs(
56
+ config,
57
+ chalk.red(
58
+ 'You are not allowed to export workflow, Unless you provide email and password in config',
59
+ 'error',
60
+ ),
61
+ );
62
+ return resolve();
63
+ }
64
+ addlogs(config, error, 'error');
65
+ return resolve();
66
+ });
67
+ });
68
+ };
59
69
 
60
- module.exports = new ExportWorkFlows()
70
+ module.exports = new ExportWorkFlows();
@@ -1,19 +1,39 @@
1
- const contentstacksdk = require('@contentstack/management')
1
+ const contentstacksdk = require('@contentstack/management');
2
+ const https = require('https');
3
+
4
+ const { addlogs } = require('./log');
2
5
 
3
6
  exports.Client = function (config) {
4
7
  const option = {
5
8
  host: config.host,
6
9
  authtoken: config.auth_token,
7
10
  api_key: config.source_stack,
11
+ maxRequests: 10,
12
+ retryLimit: 5,
13
+ timeout: 60000,
14
+ httpsAgent: new https.Agent({
15
+ maxSockets: 100,
16
+ maxFreeSockets: 10,
17
+ keepAlive: true,
18
+ timeout: 60000, // active socket keepalive for 60 seconds
19
+ freeSocketTimeout: 30000, // free socket keepalive for 30 seconds
20
+ }),
21
+ retryDelay: Math.floor(Math.random() * (8000 - 3000 + 1) + 3000),
8
22
  logHandler: (level, data) => {},
9
- }
23
+ retryCondition: (error) => {
24
+ if (error.response.status === 408) {
25
+ addlogs({ data: error.response }, 'Timeout error', 'error');
26
+ }
27
+ return false;
28
+ },
29
+ };
10
30
 
11
31
  if (typeof config.branchName === 'string') {
12
32
  option.headers = {
13
33
  branch: config.branchName,
14
- }
34
+ };
15
35
  }
16
36
 
17
- const client = contentstacksdk.client(option)
18
- return client
19
- }
37
+ const client = contentstacksdk.client(option);
38
+ return client;
39
+ };