@contentstack/cli-cm-import 0.1.1-beta.1 → 0.1.1-beta.12

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.
@@ -6,93 +6,153 @@
6
6
  let defaultConfig = require('../../config/default')
7
7
  let { initial } = require('../../app')
8
8
  let _ = require('lodash')
9
- const {cli} = require('cli-ux')
9
+ const { cli } = require('cli-ux')
10
10
  let message = require('../../../messages/index.json')
11
11
 
12
- exports.configWithMToken = function (config, managementTokens, moduleName, host) {
13
- let externalConfig = require(config)
14
- defaultConfig.management_token = managementTokens.token
15
- if (moduleName && moduleName !== undefined) {
16
- defaultConfig.moduleName = moduleName
17
- }
18
- defaultConfig.host = host.cma
19
- defaultConfig.cdn = host.cda
20
- defaultConfig = _.merge(defaultConfig, externalConfig)
21
- initial(defaultConfig)
12
+ exports.configWithMToken = function (config, managementTokens, moduleName, host, _authToken, backupdir, branchName) {
13
+ return new Promise(async function (resolve, reject) {
14
+ let externalConfig = require(config)
15
+ defaultConfig.management_token = managementTokens.token
16
+ defaultConfig.branchName = branchName
17
+ if (moduleName && moduleName !== undefined) {
18
+ defaultConfig.moduleName = moduleName
19
+ }
20
+ defaultConfig.host = host
21
+ if (backupdir) {
22
+ defaultConfig.useBackedupDir = backupdir;
23
+ }
24
+ defaultConfig.auth_token = _authToken
25
+ defaultConfig = _.merge(defaultConfig, externalConfig)
26
+ initial(defaultConfig)
27
+ .then(() => {
28
+ return resolve()
29
+ }).catch((error) => {
30
+ return reject(error)
31
+ })
32
+ })
22
33
  }
23
34
 
24
- exports.parameterWithMToken = function (masterLang, managementTokens, data, moduleName, host) {
25
- var masterloc = {master_locale: {code: masterLang}}
26
- defaultConfig.management_token = managementTokens.token
27
- defaultConfig.target_stack = managementTokens.apiKey
28
- if (moduleName && moduleName !== undefined) {
29
- defaultConfig.moduleName = moduleName
30
- }
31
- defaultConfig.data = data
32
- defaultConfig.host = host.cma
33
- defaultConfig.cdn = host.cda
34
- defaultConfig = _.merge(defaultConfig, masterloc)
35
- initial(defaultConfig)
35
+ exports.parameterWithMToken = function (managementTokens, data, moduleName, host, _authToken, backupdir, branchName) {
36
+ return new Promise(async function (resolve, reject) {
37
+ defaultConfig.management_token = managementTokens.token
38
+ defaultConfig.target_stack = managementTokens.apiKey
39
+ defaultConfig.auth_token = _authToken
40
+ defaultConfig.branchName = branchName
41
+ if (moduleName && moduleName !== undefined) {
42
+ defaultConfig.moduleName = moduleName
43
+ }
44
+ defaultConfig.data = data
45
+ defaultConfig.host = host
46
+ if (backupdir) {
47
+ defaultConfig.useBackedupDir = backupdir;
48
+ }
49
+ initial(defaultConfig)
50
+ .then(() => {
51
+ return resolve()
52
+ }).catch((error) => {
53
+ return reject(error)
54
+ })
55
+ })
36
56
  }
37
57
 
38
58
  // using ManagemetToken
39
- exports.withoutParameterMToken = async (managementTokens, moduleName, host) => {
40
- const masterLocale = await cli.prompt(message.promptMessageList.promptMasterLocale)
41
- // const stackUid = managementTokens.apiKey
42
- const exporteddata = await cli.prompt(message.promptMessageList.promptPathStoredData)
43
- var masterloc = {master_locale: {code: masterLocale}}
44
- defaultConfig.management_token = managementTokens.token
45
- defaultConfig.target_stack = managementTokens.apiKey
46
- if (moduleName && moduleName !== undefined) {
47
- defaultConfig.moduleName = moduleName
48
- }
49
- defaultConfig.data = exporteddata
50
- defaultConfig.host = host.cma
51
- defaultConfig.cdn = host.cda
52
- defaultConfig = _.merge(defaultConfig, masterloc)
53
- initial(defaultConfig)
59
+ exports.withoutParameterMToken = async (managementTokens, moduleName, host, _authToken, backupdir, branchName) => {
60
+ return new Promise(async function (resolve, reject) {
61
+ const exporteddata = await cli.prompt(message.promptMessageList.promptPathStoredData)
62
+ defaultConfig.management_token = managementTokens.token
63
+ defaultConfig.target_stack = managementTokens.apiKey
64
+ defaultConfig.auth_token = _authToken
65
+ defaultConfig.branchName = branchName
66
+ if (moduleName && moduleName !== undefined) {
67
+ defaultConfig.moduleName = moduleName
68
+ }
69
+ defaultConfig.data = exporteddata
70
+ defaultConfig.host = host
71
+ if (backupdir) {
72
+ defaultConfig.useBackedupDir = backupdir;
73
+ }
74
+ initial(defaultConfig)
75
+ .then(() => {
76
+ return resolve()
77
+ }).catch((error) => {
78
+ return reject(error)
79
+ })
80
+ })
54
81
  }
55
82
 
56
- exports.configWithAuthToken = function (config, _authToken, moduleName, host) {
57
- let externalConfig = require(config)
58
- defaultConfig.auth_token = _authToken
59
- if (moduleName && moduleName !== undefined) {
60
- defaultConfig.moduleName = moduleName
61
- }
62
- defaultConfig.host = host.cma
63
- defaultConfig.cdn = host.cda
64
- defaultConfig = _.merge(defaultConfig, externalConfig)
65
- initial(defaultConfig)
83
+ exports.configWithAuthToken = function (config, _authToken, moduleName, host, backupdir, branchName) {
84
+ return new Promise(async function (resolve, reject) {
85
+ let externalConfig = require(config)
86
+ defaultConfig.auth_token = _authToken
87
+ defaultConfig.branchName = branchName
88
+ if (moduleName && moduleName !== undefined) {
89
+ defaultConfig.moduleName = moduleName
90
+ }
91
+ defaultConfig.host = host
92
+
93
+ if (externalConfig.modules) {
94
+ defaultConfig.modules.types = externalConfig.modules
95
+ delete externalConfig.modules
96
+ }
97
+
98
+ if (backupdir) {
99
+ defaultConfig.useBackedupDir = backupdir;
100
+ }
101
+ defaultConfig = _.merge(defaultConfig, externalConfig)
102
+ initial(defaultConfig)
103
+ .then(() => {
104
+ return resolve()
105
+ }).catch((error) => {
106
+ return reject(error)
107
+ })
108
+ })
66
109
  }
67
110
 
68
- exports.parametersWithAuthToken = function (masterLang, _authToken, targetStack, data, moduleName, host) {
69
- var masterloc = {master_locale: {code: masterLang}}
70
- defaultConfig.auth_token = _authToken
71
- defaultConfig.target_stack = targetStack
72
- if (moduleName && moduleName !== undefined) {
73
- defaultConfig.moduleName = moduleName
74
- }
75
- defaultConfig.data = data
76
- defaultConfig.host = host.cma
77
- defaultConfig.cdn = host.cda
78
- defaultConfig = _.merge(defaultConfig, masterloc)
79
- initial(defaultConfig)
111
+ exports.parametersWithAuthToken = function (_authToken, targetStack, data, moduleName, host, backupdir, branchName) {
112
+ return new Promise(async function (resolve, reject) {
113
+ defaultConfig.auth_token = _authToken
114
+ defaultConfig.target_stack = targetStack
115
+ defaultConfig.branchName = branchName
116
+ if (moduleName && moduleName !== undefined && backupdir === undefined) {
117
+ defaultConfig.moduleName = moduleName
118
+ } else if (moduleName && moduleName !== undefined && backupdir !== undefined) {
119
+ defaultConfig.moduleName = moduleName
120
+ defaultConfig.useBackedupDir = backupdir
121
+ }
122
+ defaultConfig.data = data
123
+ defaultConfig.host = host
124
+
125
+ initial(defaultConfig)
126
+ .then(() => {
127
+ return resolve()
128
+ }).catch((error) => {
129
+ return reject(error)
130
+ })
131
+ })
80
132
  }
81
133
 
82
- exports.withoutParametersWithAuthToken = async (_authToken, moduleName, host) => {
134
+ exports.withoutParametersWithAuthToken = async (_authToken, moduleName, host, backupdir, branchName) => {
135
+ return new Promise(async function (resolve, reject) {
136
+ const stackUid = await cli.prompt(message.promptMessageList.promptTargetStack)
137
+ const exporteddata = await cli.prompt(message.promptMessageList.promptPathStoredData)
138
+ defaultConfig.auth_token = _authToken
139
+ defaultConfig.target_stack = stackUid
140
+ defaultConfig.data = exporteddata
141
+ defaultConfig.branchName = branchName
142
+ if (moduleName && moduleName !== undefined && backupdir === undefined) {
143
+ defaultConfig.moduleName = moduleName
144
+ } else if (moduleName && moduleName !== undefined && backupdir !== undefined) {
145
+ defaultConfig.moduleName = moduleName
146
+ defaultConfig.useBackedupDir = backupdir
147
+ }
148
+
149
+ defaultConfig.host = host
83
150
 
84
- const masterLocale = await cli.prompt(message.promptMessageList.promptMasterLocale)
85
- const stackUid = await cli.prompt(message.promptMessageList.promptTargetStack)
86
- const exporteddata = await cli.prompt(message.promptMessageList.promptPathStoredData)
87
- var masterloc = {master_locale: {code: masterLocale}}
88
- defaultConfig.auth_token = _authToken
89
- defaultConfig.target_stack = stackUid
90
- defaultConfig.data = exporteddata
91
- if (moduleName && moduleName !== undefined) {
92
- defaultConfig.moduleName = moduleName
93
- }
94
- defaultConfig.host = host.cma
95
- defaultConfig.cdn = host.cda
96
- defaultConfig = _.merge(defaultConfig, masterloc)
97
- initial(defaultConfig)
151
+ initial(defaultConfig)
152
+ .then(() => {
153
+ return resolve()
154
+ }).catch((error) => {
155
+ return reject(error)
156
+ })
157
+ })
98
158
  }
@@ -11,9 +11,10 @@ var path = require('path')
11
11
  var chalk = require('chalk')
12
12
  var {addlogs} = require('./log')
13
13
  var request = require('./request')
14
- // var config = require('../../config/');
15
14
  var defaultConfig = require('../../config/default')
15
+ const stack = require('./contentstack-management-sdk')
16
16
  var config
17
+
17
18
  exports.initialization = function(configData) {
18
19
  config = this.buildAppConfig(configData)
19
20
  var res = this.validateConfig(config)
@@ -23,40 +24,40 @@ exports.initialization = function(configData) {
23
24
  }
24
25
  }
25
26
 
26
- exports.validateConfig = function (config) {
27
- if (config.email && config.password && !config.target_stack) {
28
- addlogs(config, chalk.red('Kindly provide api_token'), 'error')
27
+ exports.validateConfig = function (importConfig) {
28
+ if (importConfig.email && importConfig.password && !importConfig.target_stack) {
29
+ addlogs(importConfig, chalk.red('Kindly provide api_token'), 'error')
29
30
  return 'error'
30
- } if(!config.email && !config.password && !config.management_token && config.target_stack && !config.auth_token) {
31
- addlogs(config, chalk.red('Kindly provide management_token or email and password'), 'error')
32
- return 'error'
33
- } else if(!config.email && !config.password && config.preserveStackVersion) {
34
- addlogs(config, chalk.red('Kindly provide Email and password for old version stack'), 'error')
31
+ } else if(!importConfig.email && !importConfig.password && !importConfig.management_token && importConfig.target_stack && !importConfig.auth_token) {
32
+ addlogs(importConfig, chalk.red('Kindly provide management_token or email and password'), 'error')
35
33
  return 'error'
36
- } else if(config.email && !config.password || !config.email && config.password) {
37
- addlogs(config, chalk.red('Kindly provide Email and password'), 'error')
34
+ } else if(!importConfig.email && !importConfig.password && importConfig.preserveStackVersion) {
35
+ addlogs(importConfig, chalk.red('Kindly provide Email and password for old version stack'), 'error')
38
36
  return 'error'
39
- }
40
- if(!config.languagesCode.includes(config.master_locale.code)) {
41
- addlogs(config, chalk.red('Kindly provide valid master_locale code'), 'error')
37
+ } else if(importConfig.email && !importConfig.password || !importConfig.email && importConfig.password) {
38
+ addlogs(importConfig, chalk.red('Kindly provide Email and password'), 'error')
42
39
  return 'error'
43
40
  }
41
+ // if(!importConfig.languagesCode.includes(importConfig.master_locale.code)) {
42
+ // addlogs(importConfig, chalk.red('Kindly provide valid master_locale code'), 'error')
43
+ // return 'error'
44
+ // }
44
45
  }
45
46
 
46
- exports.buildAppConfig = function (config) {
47
- config = _.merge(defaultConfig, config)
48
- return config
47
+ exports.buildAppConfig = function (importConfig) {
48
+ importConfig = _.merge(defaultConfig, importConfig)
49
+ return importConfig
49
50
  };
50
51
 
51
- exports.sanitizeStack = function (config) {
52
- if (typeof config.preserveStackVersion !== 'boolean' || !config.preserveStackVersion) {
52
+ exports.sanitizeStack = function (importConfig) {
53
+ if (typeof importConfig.preserveStackVersion !== 'boolean' || !importConfig.preserveStackVersion) {
53
54
  return Promise.resolve()
54
55
  }
55
- addlogs(config, 'Running script to maintain stack version.', 'success')
56
+ addlogs(importConfig, 'Running script to maintain stack version.', 'success')
56
57
  var getStackOptions = {
57
- url: config.host + config.apis.stacks,
58
+ url: importConfig.host + importConfig.apis.stacks,
58
59
  method: 'GET',
59
- headers: config.headers,
60
+ headers: importConfig.headers,
60
61
  json: true
61
62
  }
62
63
 
@@ -66,7 +67,7 @@ exports.sanitizeStack = function (config) {
66
67
  if (stackDetails.body && stackDetails.body.stack && stackDetails.body.stack.settings) {
67
68
  const newStackVersion = stackDetails.body.stack.settings.version
68
69
  const newStackDate = new Date(newStackVersion).toString()
69
- const stackFilePath = path.join(config.data, config.modules.stack.dirName, config.modules.stack.fileName)
70
+ const stackFilePath = path.join(importConfig.data, importConfig.modules.stack.dirName, importConfig.modules.stack.fileName)
70
71
 
71
72
  const oldStackDetails = fs.readFile(stackFilePath)
72
73
  if (!oldStackDetails || !oldStackDetails.settings || !oldStackDetails.settings.hasOwnProperty('version')) {
@@ -77,15 +78,15 @@ exports.sanitizeStack = function (config) {
77
78
  if (oldStackDate > newStackDate) {
78
79
  throw new Error('Migration Error. You cannot migrate data from new stack onto old. Kindly contact support@contentstack.com for more details.')
79
80
  } else if (oldStackDate === newStackDate) {
80
- addlogs(config, 'The version of both the stacks are same.', 'success')
81
+ addlogs(importConfig, 'The version of both the stacks are same.', 'success')
81
82
  return Promise.resolve()
82
83
  }
83
- addlogs(config, 'Updating stack version.', 'success')
84
+ addlogs(importConfig, 'Updating stack version.', 'success')
84
85
  // Update the new stack
85
86
  var updateStackOptions = {
86
- url: config.host + config.apis.stacks + 'settings/set-version',
87
+ url: importConfig.host + importConfig.apis.stacks + 'settings/set-version',
87
88
  method: 'PUT',
88
- headers: config.headers,
89
+ headers: importConfig.headers,
89
90
  body: {
90
91
  stack_settings: {
91
92
  version: '2017-10-14' // This can be used as a variable
@@ -95,7 +96,7 @@ exports.sanitizeStack = function (config) {
95
96
 
96
97
  return request(updateStackOptions)
97
98
  .then((response) => {
98
- addlogs(config, `Stack version preserved successfully!\n${JSON.stringify(response.body)}`, 'success')
99
+ addlogs(importConfig, `Stack version preserved successfully!\n${JSON.stringify(response.body)}`, 'success')
99
100
  return;
100
101
  })
101
102
  }
@@ -106,6 +107,77 @@ exports.sanitizeStack = function (config) {
106
107
  }
107
108
  }
108
109
 
110
+ exports.masterLocalDetails = function(credentialConfig) {
111
+ let client = stack.Client(credentialConfig)
112
+ return new Promise((resolve, reject) => {
113
+ var result = client.stack({ api_key: credentialConfig.target_stack, management_token: credentialConfig.management_token }).locale().query()
114
+ result.find()
115
+ .then(response => {
116
+ var masterLocalObj = response.items.filter(obj => {
117
+ if (obj.fallback_locale === null) {
118
+ return obj
119
+ }
120
+ });
121
+ return resolve(masterLocalObj[0])
122
+ }).catch(error => {
123
+ return reject(error)
124
+ })
125
+ })
126
+ };
127
+
128
+ exports.field_rules_update = function(importConfig, ctPath) {
129
+ return new Promise(function (resolve, reject) {
130
+ let client = stack.Client(importConfig)
131
+
132
+ fs.readFile(path.join(ctPath + '/field_rules_uid.json'), async (err, data) => {
133
+ if (err) {
134
+ throw err;
135
+ }
136
+ var ct_field_visibility_uid = JSON.parse(data)
137
+ let ct_files = fs.readdirSync(ctPath)
138
+ if (ct_field_visibility_uid && ct_field_visibility_uid != 'undefined') {
139
+ for (let index = 0; index < ct_field_visibility_uid.length; index++) {
140
+ if (ct_files.indexOf(ct_field_visibility_uid[index] + '.json') > -1) {
141
+ let schema = require(path.resolve(ctPath, ct_field_visibility_uid[index]))
142
+ // await field_rules_update(schema)
143
+ let fieldRuleLength = schema.field_rules.length
144
+ for (let k = 0; k < fieldRuleLength; k++) {
145
+ let fieldRuleConditionLength = schema.field_rules[k].conditions.length
146
+ for (let i = 0; i < fieldRuleConditionLength; i++) {
147
+ if (schema.field_rules[k].conditions[i].operand_field === 'reference') {
148
+ let entryMapperPath = path.resolve(importConfig.data, 'mapper', 'entries')
149
+ let entryUidMapperPath = path.join(entryMapperPath, 'uid-mapping.json')
150
+ let fieldRulesValue = schema.field_rules[k].conditions[i].value
151
+ let fieldRulesArray = fieldRulesValue.split('.')
152
+ let updatedValue = []
153
+ for (let j = 0; j < fieldRulesArray.length; j++) {
154
+ let splitedFieldRulesValue = fieldRulesArray[j]
155
+ let oldUid = helper.readFile(path.join(entryUidMapperPath))
156
+ if (oldUid.hasOwnProperty(splitedFieldRulesValue)) {
157
+ updatedValue.push(oldUid[splitedFieldRulesValue])
158
+ } else {
159
+ updatedValue.push(fieldRulesArray[j])
160
+ }
161
+ }
162
+ schema.field_rules[k].conditions[i].value = updatedValue.join('.')
163
+ }
164
+ }
165
+ }
166
+ let ctObj = client.stack({ api_key: importConfig.target_stack, management_token: importConfig.management_token }).contentType(schema.uid)
167
+ Object.assign(ctObj, _.cloneDeep(schema))
168
+ ctObj.update()
169
+ .then(() => {
170
+ return resolve()
171
+ }).catch(function (error) {
172
+ return reject(error)
173
+ })
174
+ }
175
+ }
176
+ }
177
+ })
178
+ })
179
+ }
180
+
109
181
  exports.getConfig = function() {
110
182
  return config
111
183
  };
@@ -91,9 +91,11 @@ function init (_logPath, logfileName) {
91
91
  }
92
92
 
93
93
  exports.addlogs = async (config, message, type) => {
94
+ var configLogPath
95
+ config.source_stack && config.target_stack ? configLogPath = config.data : configLogPath = config.oldPath
94
96
  if (type !== 'error') {
95
- init(config.oldPath, type).log(message)
97
+ init(configLogPath, type).log(message)
96
98
  } else {
97
- init(config.oldPath, type).error(message)
99
+ init(configLogPath, type).error(message)
98
100
  }
99
101
  }
@@ -34,9 +34,9 @@ module.exports = function (config) {
34
34
  }).catch(reject);
35
35
  } else if (config.management_token) {
36
36
  return resolve()
37
- } else if (config.auth_token) {
37
+ } else if (config.auth_token) {
38
38
  // return resolve()
39
- client.stack({api_key: config.target_stack, management_token: config.management_token}).users()
39
+ client.stack({api_key: config.target_stack, management_token: config.management_token}).fetch()
40
40
  .then(function () {
41
41
  return resolve()
42
42
  })
@@ -44,10 +44,10 @@ module.exports = function (config) {
44
44
  let errorstack_key = error.errors.api_key
45
45
  if(error.errors.api_key) {
46
46
  addlogs(config, chalk.red("Stack Api key " + errorstack_key[0], "Please enter valid Key"), 'error')
47
- return reject()
47
+ return reject(error)
48
48
  }
49
49
  addlogs(config, error.errorMessage, 'error')
50
- return reject()
50
+ return reject(error)
51
51
  })
52
52
  }
53
53
  });