@contentstack/cli-cm-export 0.1.1-beta.9 → 1.1.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.
@@ -1,158 +1,172 @@
1
1
  /*!
2
- * Contentstack Export
3
- * Copyright (c) 2019 Contentstack LLC
4
- * MIT Licensed
5
- */
6
- let defaultConfig = require('../../config/default')
7
- let message = require('../../../messages/index.json')
8
- let {initial} = require('../../app')
9
- let path = require('path')
2
+ * Contentstack Export
3
+ * Copyright (c) 2019 Contentstack LLC
4
+ * MIT Licensed
5
+ */
6
+ let defaultConfig = require('../../config/default');
7
+ let message = require('../../../messages/index.json');
8
+ let { initial } = require('../../app');
9
+ let path = require('path');
10
10
  const helper = require('../util/helper');
11
- let _ = require('lodash')
12
- const {cli} = require('cli-ux')
11
+ let _ = require('lodash');
12
+ const { cliux } = require('@contentstack/cli-utilities');
13
13
 
14
- exports.configWithMToken = function (config, managementTokens, host, contentTypes, branchName, securedAssets) {
15
- let externalConfig = require(config)
16
- defaultConfig.securedAssets = securedAssets
17
- defaultConfig.management_token = managementTokens.token
18
- defaultConfig.host = host.cma
19
- defaultConfig.cdn = host.cda
20
- defaultConfig.branchName = branchName
14
+ exports.configWithMToken = function (config, managementTokens, host, contentTypes, branchName, securedAssets, moduleName) {
15
+ let externalConfig = require(config);
16
+ defaultConfig.securedAssets = securedAssets;
17
+ defaultConfig.management_token = managementTokens.token;
18
+ defaultConfig.host = host.cma;
19
+ defaultConfig.cdn = host.cda;
20
+ defaultConfig.branchName = branchName;
21
+ defaultConfig.source_stack = managementTokens.apiKey;
21
22
  if (moduleName) {
22
- defaultConfig.moduleName = moduleName
23
+ defaultConfig.moduleName = moduleName;
23
24
  // Specfic content type setting is only for entries module
24
- if (
25
- moduleName === 'entries' &&
26
- Array.isArray(contentTypes) &&
27
- contentTypes.length > 0
28
- ) {
29
- defaultConfig.contentTypes = contentTypes
30
- }
25
+ if (moduleName === 'entries' && Array.isArray(contentTypes) && contentTypes.length > 0) {
26
+ defaultConfig.contentTypes = contentTypes;
27
+ }
31
28
  }
32
- defaultConfig = _.merge(defaultConfig, externalConfig)
33
- initial(defaultConfig)
34
- }
29
+ defaultConfig = _.merge(defaultConfig, externalConfig);
30
+ initial(defaultConfig);
31
+ };
35
32
 
36
- exports.parameterWithMToken = function (managementTokens, data, moduleName, host, _authToken, contentTypes, branchName, securedAssets) {
37
- defaultConfig.management_token = managementTokens.token
38
- defaultConfig.auth_token = _authToken
39
- defaultConfig.host = host.cma
40
- defaultConfig.cdn = host.cda
41
- defaultConfig.branchName = branchName
42
- defaultConfig.securedAssets = securedAssets
43
- if (moduleName) {
44
- defaultConfig.moduleName = moduleName
33
+ exports.parameterWithMToken = async function (
34
+ managementTokens,
35
+ data,
36
+ moduleName,
37
+ host,
38
+ _authToken,
39
+ contentTypes,
40
+ branchName,
41
+ securedAssets,
42
+ ) {
43
+ defaultConfig.management_token = managementTokens.token;
44
+ defaultConfig.auth_token = _authToken;
45
+ defaultConfig.host = host.cma;
46
+ defaultConfig.cdn = host.cda;
47
+ defaultConfig.branchName = branchName;
48
+ defaultConfig.securedAssets = securedAssets;
49
+ if (!moduleName) {
50
+ defaultConfig.contentTypes = contentTypes;
51
+ } else {
52
+ defaultConfig.moduleName = moduleName;
45
53
  // Specfic content type setting is only for entries module
46
- if (
47
- moduleName === 'entries' &&
48
- Array.isArray(contentTypes) &&
49
- contentTypes.length > 0
50
- ) {
51
- defaultConfig.contentTypes = contentTypes
54
+ if (moduleName === 'entries' && Array.isArray(contentTypes) && contentTypes.length > 0) {
55
+ defaultConfig.contentTypes = contentTypes;
52
56
  }
53
57
  }
54
- defaultConfig.source_stack = managementTokens.apiKey
55
- defaultConfig.data = data
56
- initial(defaultConfig)
57
- }
58
+ defaultConfig.source_stack = managementTokens.apiKey;
59
+ defaultConfig.data = data;
60
+ await initial(defaultConfig);
61
+ };
58
62
 
59
63
  // using ManagementToken
60
- exports.withoutParameterMToken = async (managementTokens, moduleName, host, _authToken, contentTypes, branchName, securedAssets) => {
61
- const stackUid = managementTokens.apiKey
62
- const pathOfExport = await cli.prompt(message.promptMessageList.promptPathStoredData)
63
- defaultConfig.management_token = managementTokens.token
64
- defaultConfig.host = host.cma
65
- defaultConfig.cdn = host.cda
66
- defaultConfig.branchName = branchName
67
- defaultConfig.auth_token = _authToken
68
- defaultConfig.securedAssets = securedAssets
64
+ exports.withoutParameterMToken = async (
65
+ managementTokens,
66
+ moduleName,
67
+ host,
68
+ _authToken,
69
+ contentTypes,
70
+ branchName,
71
+ securedAssets,
72
+ ) => {
73
+ const stackUid = managementTokens.apiKey;
74
+ const pathOfExport = await cliux.prompt(message.promptMessageList.promptPathStoredData);
75
+ defaultConfig.management_token = managementTokens.token;
76
+ defaultConfig.host = host.cma;
77
+ defaultConfig.cdn = host.cda;
78
+ defaultConfig.branchName = branchName;
79
+ defaultConfig.auth_token = _authToken;
80
+ defaultConfig.securedAssets = securedAssets;
69
81
  if (moduleName) {
70
- defaultConfig.moduleName = moduleName
82
+ defaultConfig.moduleName = moduleName;
71
83
  // Specfic content type setting is only for entries module
72
- if (
73
- moduleName === 'entries' &&
74
- Array.isArray(contentTypes) &&
75
- contentTypes.length > 0
76
- ) {
77
- defaultConfig.contentTypes = contentTypes
84
+ if (moduleName === 'entries' && Array.isArray(contentTypes) && contentTypes.length > 0) {
85
+ defaultConfig.contentTypes = contentTypes;
78
86
  }
79
87
  }
80
- defaultConfig.source_stack = stackUid
81
- defaultConfig.data = pathOfExport
82
- initial(defaultConfig)
83
- }
88
+ defaultConfig.source_stack = stackUid;
89
+ defaultConfig.data = pathOfExport;
90
+ initial(defaultConfig);
91
+ };
84
92
 
85
93
  exports.configWithAuthToken = function (config, _authToken, moduleName, host, contentTypes, branchName, securedAssets) {
86
- let externalConfig = helper.readFile(path.resolve(config))
87
- defaultConfig.auth_token = _authToken
88
- defaultConfig.host = host.cma
89
- defaultConfig.cdn = host.cda
90
- defaultConfig.branchName = branchName
91
- defaultConfig.securedAssets = securedAssets
94
+ let externalConfig = helper.readFile(path.resolve(config));
95
+ defaultConfig.auth_token = _authToken;
96
+ defaultConfig.host = host.cma;
97
+ defaultConfig.cdn = host.cda;
98
+ defaultConfig.branchName = branchName;
99
+ defaultConfig.securedAssets = securedAssets;
92
100
  if (moduleName) {
93
- defaultConfig.moduleName = moduleName
101
+ defaultConfig.moduleName = moduleName;
94
102
  // Specfic content type setting is only for entries module
95
- if (
96
- moduleName === 'entries' &&
97
- Array.isArray(contentTypes) &&
98
- contentTypes.length > 0
99
- ) {
100
- defaultConfig.contentTypes = contentTypes
103
+ if (moduleName === 'entries' && Array.isArray(contentTypes) && contentTypes.length > 0) {
104
+ defaultConfig.contentTypes = contentTypes;
101
105
  }
102
106
  }
103
- defaultConfig = _.merge(defaultConfig, externalConfig)
104
- initial(defaultConfig)
105
- }
107
+ defaultConfig = _.merge(defaultConfig, externalConfig);
108
+ initial(defaultConfig);
109
+ };
106
110
 
107
- exports.parametersWithAuthToken = function (_authToken, sourceStack, data, moduleName, host, contentTypes, branchName, securedAssets) {
108
- return new Promise(async(resolve, reject) => {
109
- defaultConfig.auth_token = _authToken
110
- defaultConfig.source_stack = sourceStack
111
+ exports.parametersWithAuthToken = function (
112
+ _authToken,
113
+ sourceStack,
114
+ data,
115
+ moduleName,
116
+ host,
117
+ contentTypes,
118
+ branchName,
119
+ securedAssets,
120
+ ) {
121
+ return new Promise(async (resolve, reject) => {
122
+ defaultConfig.auth_token = _authToken;
123
+ defaultConfig.source_stack = sourceStack;
111
124
  if (moduleName) {
112
- defaultConfig.moduleName = moduleName
125
+ defaultConfig.moduleName = moduleName;
113
126
  // Specfic content type setting is only for entries module
114
- if (
115
- moduleName === 'entries' &&
116
- Array.isArray(contentTypes) &&
117
- contentTypes.length > 0
118
- ) {
119
- defaultConfig.contentTypes = contentTypes
127
+ if (moduleName === 'entries' && Array.isArray(contentTypes) && contentTypes.length > 0) {
128
+ defaultConfig.contentTypes = contentTypes;
120
129
  }
121
130
  }
122
- defaultConfig.branchName = branchName
123
- defaultConfig.host = host.cma
124
- defaultConfig.cdn = host.cda
125
- defaultConfig.data = data
126
- defaultConfig.securedAssets = securedAssets
127
- var exportStart = initial(defaultConfig)
128
- exportStart.then(() => {
129
- return resolve()
130
- }).catch((error) => {
131
- return reject(error)
132
- })
133
- })
134
- }
131
+ defaultConfig.branchName = branchName;
132
+ defaultConfig.host = host.cma;
133
+ defaultConfig.cdn = host.cda;
134
+ defaultConfig.data = data;
135
+ defaultConfig.securedAssets = securedAssets;
136
+ var exportStart = initial(defaultConfig);
137
+ exportStart
138
+ .then(() => {
139
+ return resolve();
140
+ })
141
+ .catch((error) => {
142
+ return reject(error);
143
+ });
144
+ });
145
+ };
135
146
 
136
- exports.withoutParametersWithAuthToken = async (_authToken, moduleName, host, contentTypes, branchName, securedAssets) => {
137
- const stackUid = await cli.prompt(message.promptMessageList.promptSourceStack)
138
- const pathOfExport = await cli.prompt(message.promptMessageList.promptPathStoredData)
139
- defaultConfig.auth_token = _authToken
140
- defaultConfig.source_stack = stackUid
141
- defaultConfig.securedAssets = securedAssets
147
+ exports.withoutParametersWithAuthToken = async (
148
+ _authToken,
149
+ moduleName,
150
+ host,
151
+ contentTypes,
152
+ branchName,
153
+ securedAssets,
154
+ ) => {
155
+ const stackUid = await cliux.prompt(message.promptMessageList.promptSourceStack);
156
+ const pathOfExport = await cliux.prompt(message.promptMessageList.promptPathStoredData);
157
+ defaultConfig.auth_token = _authToken;
158
+ defaultConfig.source_stack = stackUid;
159
+ defaultConfig.securedAssets = securedAssets;
142
160
  if (moduleName) {
143
- defaultConfig.moduleName = moduleName
161
+ defaultConfig.moduleName = moduleName;
144
162
  // Specfic content type setting is only for entries module
145
- if (
146
- moduleName === 'entries' &&
147
- Array.isArray(contentTypes) &&
148
- contentTypes.length > 0
149
- ) {
150
- defaultConfig.contentTypes = contentTypes
163
+ if (moduleName === 'entries' && Array.isArray(contentTypes) && contentTypes.length > 0) {
164
+ defaultConfig.contentTypes = contentTypes;
151
165
  }
152
166
  }
153
- defaultConfig.branchName = branchName
154
- defaultConfig.data = pathOfExport
155
- defaultConfig.host = host.cma
156
- defaultConfig.cdn = host.cda
157
- initial(defaultConfig)
158
- }
167
+ defaultConfig.branchName = branchName;
168
+ defaultConfig.data = pathOfExport;
169
+ defaultConfig.host = host.cma;
170
+ defaultConfig.cdn = host.cda;
171
+ initial(defaultConfig);
172
+ };
@@ -1,41 +1,41 @@
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
- var fs = require('fs')
8
- var path = require('path')
9
- var mkdirp = require('mkdirp')
7
+ var fs = require('fs');
8
+ var path = require('path');
9
+ var mkdirp = require('mkdirp');
10
10
 
11
11
  exports.readFile = function (filePath, parse) {
12
- var data
13
- parse = (typeof parse === 'undefined') ? true : parse
14
- filePath = path.resolve(filePath)
12
+ var data;
13
+ parse = typeof parse === 'undefined' ? true : parse;
14
+ filePath = path.resolve(filePath);
15
15
  if (fs.existsSync(filePath)) {
16
- data = (parse) ? JSON.parse(fs.readFileSync(filePath, 'utf-8')) : data
16
+ data = parse ? JSON.parse(fs.readFileSync(filePath, 'utf-8')) : data;
17
17
  }
18
- return data
18
+ return data;
19
19
  };
20
20
 
21
21
  exports.writeFile = function (filePath, data) {
22
- data = (typeof data === 'object') ? JSON.stringify(data) : data || '{}'
23
- fs.writeFileSync(filePath, data)
22
+ data = typeof data === 'object' ? JSON.stringify(data) : data || '{}';
23
+ fs.writeFileSync(filePath, data);
24
24
  };
25
25
 
26
26
  exports.makeDirectory = function () {
27
27
  for (var key in arguments) {
28
- var dirname = path.resolve(arguments[key])
28
+ var dirname = path.resolve(arguments[key]);
29
29
  if (!fs.existsSync(dirname)) {
30
- mkdirp.sync(dirname)
30
+ mkdirp.sync(dirname);
31
31
  }
32
32
  }
33
- }
33
+ };
34
34
 
35
35
  exports.readdir = function (dirPath) {
36
36
  if (fs.existsSync(path)) {
37
- return fs.readdirSync(dirPath)
37
+ return fs.readdirSync(dirPath);
38
38
  } else {
39
- return []
39
+ return [];
40
40
  }
41
- }
41
+ };
@@ -1,13 +1,13 @@
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
- var _ = require('lodash')
8
- var defaultConfig = require('../../config/default')
9
- var {addlogs} = require('../util/log')
10
- const chalk = require('chalk')
7
+ var _ = require('lodash');
8
+ var defaultConfig = require('../../config/default');
9
+ var { addlogs } = require('../util/log');
10
+ const chalk = require('chalk');
11
11
 
12
12
  exports.validateConfig = function (config) {
13
13
  if (!config.host || !config.cdn) {
@@ -15,18 +15,31 @@ exports.validateConfig = function (config) {
15
15
  }
16
16
 
17
17
  if (config.email && config.password && !config.access_token && !config.source_stack) {
18
- throw new Error('Kindly provide access_token or api_token')
19
- } else if (!config.email && !config.password && !config.management_token && config.source_stack && !config.access_token && !config.auth_token) {
20
- throw new Error('Kindly provide management_token or email and password')
21
- } else if (config.email && config.password && !config.access_token && config.source_stack && !config.management_token && !config.auth_token) {
22
- throw new Error('Kindly provide access_token or management_token')
18
+ throw new Error('Kindly provide access_token or api_token');
19
+ } else if (
20
+ !config.email &&
21
+ !config.password &&
22
+ !config.management_token &&
23
+ config.source_stack &&
24
+ !config.access_token &&
25
+ !config.auth_token
26
+ ) {
27
+ throw new Error('Kindly provide management_token or email and password');
28
+ } else if (
29
+ config.email &&
30
+ config.password &&
31
+ !config.access_token &&
32
+ config.source_stack &&
33
+ !config.management_token &&
34
+ !config.auth_token
35
+ ) {
36
+ throw new Error('Kindly provide access_token or management_token');
23
37
  } else if (!config.email && !config.password && config.preserveStackVersion) {
24
- throw new Error('Kindly provide Email and password for stack details')
38
+ throw new Error('Kindly provide Email and password for stack details');
25
39
  }
26
- }
40
+ };
27
41
 
28
42
  exports.buildAppConfig = function (config) {
29
- config = _.merge(defaultConfig, config)
30
- return config
31
- }
32
-
43
+ config = _.merge(defaultConfig, config);
44
+ return config;
45
+ };
@@ -1,99 +1,155 @@
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
- var winston = require('winston')
8
- var path = require('path')
9
- var mkdirp = require('mkdirp')
10
- var slice = Array.prototype.slice
7
+ var winston = require('winston');
8
+ var path = require('path');
9
+ var mkdirp = require('mkdirp');
10
+ var slice = Array.prototype.slice;
11
+
12
+ const ansiRegexPattern = [
13
+ '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
14
+ '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
15
+ ].join('|');
11
16
 
12
17
  function returnString(args) {
13
- var returnStr = ''
18
+ var returnStr = '';
14
19
  if (args && args.length) {
15
- returnStr = args.map(function (item) {
16
- if (item && typeof (item) === 'object') {
17
- return JSON.stringify(item)
18
- }
19
- return item
20
- }).join(' ').trim()
20
+ returnStr = args
21
+ .map(function (item) {
22
+ if (item && typeof item === 'object') {
23
+ return JSON.stringify(item).replace(/authtoken\":\"blt................/g, 'authtoken":"blt....');
24
+ }
25
+ return item;
26
+ })
27
+ .join(' ')
28
+ .trim();
21
29
  }
22
- return returnStr
30
+ returnStr = returnStr.replace(new RegExp(ansiRegexPattern, 'g'), "").trim();
31
+ return returnStr;
23
32
  }
24
-
25
33
  var myCustomLevels = {
26
34
  levels: {
27
- error: 0,
28
35
  warn: 1,
29
36
  info: 2,
30
37
  debug: 3,
31
38
  },
32
39
  colors: {
40
+ //colors aren't being used anywhere as of now, we're using chalk to add colors while logging
33
41
  info: 'blue',
34
42
  debug: 'green',
35
43
  warn: 'yellow',
36
44
  error: 'red',
37
45
  },
38
- }
46
+ };
39
47
 
40
- function init (_logPath, logfileName) {
41
- var logsDir = path.resolve(_logPath, 'logs', 'export')
42
- // Create dir if doesn't already exist
43
- mkdirp.sync(logsDir)
44
- var logPath = path.join(logsDir, logfileName + '.log')
48
+ let logger;
49
+ let errorLogger;
45
50
 
46
- var transports = [new (winston.transports.File)({
47
- filename: logPath,
48
- maxFiles: 20,
49
- maxsize: 1000000,
50
- tailable: true,
51
- json: true,
52
- })]
51
+ let successTransport;
52
+ let errorTransport;
53
53
 
54
- transports.push(new (winston.transports.Console)())
54
+ function init(_logPath) {
55
+ if (!logger || !errorLogger) {
56
+ var logsDir = path.resolve(_logPath, 'logs', 'export');
57
+ // Create dir if doesn't already exist
58
+ mkdirp.sync(logsDir);
55
59
 
56
- var logger = new(winston.Logger)({
57
- transports: transports,
58
- levels: myCustomLevels.levels
59
- })
60
+ successTransport = {
61
+ filename: path.join(logsDir, 'success.log'),
62
+ maxFiles: 20,
63
+ maxsize: 1000000,
64
+ tailable: true,
65
+ json: true,
66
+ level: 'info',
67
+ };
68
+
69
+ errorTransport = {
70
+ filename: path.join(logsDir, 'error.log'),
71
+ maxFiles: 20,
72
+ maxsize: 1000000,
73
+ tailable: true,
74
+ json: true,
75
+ level: 'error',
76
+ };
77
+
78
+ logger = winston.createLogger({
79
+ transports: [
80
+ new winston.transports.File(successTransport),
81
+ new winston.transports.Console({ format: winston.format.simple() }),
82
+ ],
83
+ levels: myCustomLevels.levels,
84
+ });
85
+
86
+ errorLogger = winston.createLogger({
87
+ transports: [
88
+ new winston.transports.File(errorTransport),
89
+ new winston.transports.Console({ level: 'error', format: winston.format.simple() }),
90
+ ],
91
+ levels: { error: 0 },
92
+ });
93
+ }
60
94
 
61
95
  return {
62
96
  log: function () {
63
- var args = slice.call(arguments)
64
- var logString = returnString(args)
97
+ let args = slice.call(arguments);
98
+ let logString = returnString(args);
65
99
  if (logString) {
66
- logger.log('info', logString)
100
+ logger.log('info', logString);
67
101
  }
68
102
  },
69
103
  warn: function () {
70
- var args = slice.call(arguments)
71
- var logString = returnString(args)
104
+ let args = slice.call(arguments);
105
+ let logString = returnString(args);
72
106
  if (logString) {
73
- logger.log('warn', logString)
107
+ logger.log('warn', logString);
74
108
  }
75
109
  },
76
- error: function (args) {
77
- var args = slice.call(arguments)
78
- var logString = returnString(args)
110
+ error: function () {
111
+ let args = slice.call(arguments);
112
+ let logString = returnString(args);
79
113
  if (logString) {
80
- logger.log('error', logString)
114
+ errorLogger.log('error', logString);
81
115
  }
82
116
  },
83
117
  debug: function () {
84
- // var args = slice.call(arguments)
85
- var logString = returnString(args)
118
+ let args = slice.call(arguments);
119
+ let logString = returnString(args);
86
120
  if (logString) {
87
- logger.log('debug', logString)
121
+ logger.log('debug', logString);
88
122
  }
89
123
  },
90
- }
124
+ };
91
125
  }
92
126
 
93
127
  exports.addlogs = async (config, message, type) => {
128
+ // ignoring the type argument, as we are not using it to create a logfile anymore
94
129
  if (type !== 'error') {
95
- init(config.data, type).log(message)
130
+ // removed type argument from init method
131
+ init(config.data).log(message);
96
132
  } else {
97
- init(config.data, type).error(message)
133
+ init(config.data).error(message);
98
134
  }
99
- }
135
+ };
136
+
137
+ exports.unlinkFileLogger = () => {
138
+ if (logger) {
139
+ const transports = logger.transports;
140
+ transports.forEach((transport) => {
141
+ if (transport.name === 'file') {
142
+ logger.remove(transport);
143
+ }
144
+ });
145
+ }
146
+
147
+ if (errorLogger) {
148
+ const transports = errorLogger.transports;
149
+ transports.forEach((transport) => {
150
+ if (transport.name === 'file') {
151
+ errorLogger.remove(transport);
152
+ }
153
+ });
154
+ }
155
+ };