@contentstack/cli-cm-import 0.1.1-beta.1 → 0.1.1-beta.10
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.
- package/README.md +10 -11
- package/oclif.manifest.json +1 -1
- package/package.json +12 -9
- package/src/app.js +108 -75
- package/src/commands/cm/import.js +82 -38
- package/src/config/default.js +238 -211
- package/src/lib/import/assets.js +42 -38
- package/src/lib/import/content-types.js +90 -95
- package/src/lib/import/entries.js +426 -399
- package/src/lib/import/environments.js +1 -1
- package/src/lib/import/global-fields.js +3 -0
- package/src/lib/import/labels.js +43 -37
- package/src/lib/import/locales.js +32 -21
- package/src/lib/import/workflows.js +118 -0
- package/src/lib/util/contentstack-management-sdk.js +21 -9
- package/src/lib/util/extensionsUidReplace.js +27 -29
- package/src/lib/util/import-flags.js +129 -75
- package/src/lib/util/index.js +77 -5
- package/src/lib/util/log.js +4 -2
- package/src/lib/util/login.js +2 -2
- package/src/lib/util/lookupReplaceEntries.js +92 -94
- package/src/lib/util/supress-mandatory-fields.js +1 -1
|
@@ -6,93 +6,147 @@
|
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
exports.configWithMToken = function (config, managementTokens, moduleName, host, _authToken, backupdir) {
|
|
13
|
+
return new Promise(async function (resolve, reject) {
|
|
14
|
+
let externalConfig = require(config)
|
|
15
|
+
defaultConfig.management_token = managementTokens.token
|
|
16
|
+
if (moduleName && moduleName !== undefined) {
|
|
17
|
+
defaultConfig.moduleName = moduleName
|
|
18
|
+
}
|
|
19
|
+
defaultConfig.host = host
|
|
20
|
+
if (backupdir) {
|
|
21
|
+
defaultConfig.useBackedupDir = backupdir;
|
|
22
|
+
}
|
|
23
|
+
defaultConfig.auth_token = _authToken
|
|
24
|
+
defaultConfig = _.merge(defaultConfig, externalConfig)
|
|
25
|
+
initial(defaultConfig)
|
|
26
|
+
.then(() => {
|
|
27
|
+
return resolve()
|
|
28
|
+
}).catch((error) => {
|
|
29
|
+
return reject()
|
|
30
|
+
})
|
|
31
|
+
})
|
|
22
32
|
}
|
|
23
33
|
|
|
24
|
-
exports.parameterWithMToken = function (
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
exports.parameterWithMToken = function (managementTokens, data, moduleName, host, _authToken, backupdir) {
|
|
35
|
+
return new Promise(async function (resolve, reject) {
|
|
36
|
+
defaultConfig.management_token = managementTokens.token
|
|
37
|
+
defaultConfig.target_stack = managementTokens.apiKey
|
|
38
|
+
defaultConfig.auth_token = _authToken
|
|
39
|
+
if (moduleName && moduleName !== undefined) {
|
|
40
|
+
defaultConfig.moduleName = moduleName
|
|
41
|
+
}
|
|
42
|
+
defaultConfig.data = data
|
|
43
|
+
defaultConfig.host = host
|
|
44
|
+
if (backupdir) {
|
|
45
|
+
defaultConfig.useBackedupDir = backupdir;
|
|
46
|
+
}
|
|
47
|
+
initial(defaultConfig)
|
|
48
|
+
.then(() => {
|
|
49
|
+
return resolve()
|
|
50
|
+
}).catch((error) => {
|
|
51
|
+
return reject()
|
|
52
|
+
})
|
|
53
|
+
})
|
|
36
54
|
}
|
|
37
55
|
|
|
38
56
|
// using ManagemetToken
|
|
39
|
-
exports.withoutParameterMToken = async (managementTokens, moduleName, host) => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
exports.withoutParameterMToken = async (managementTokens, moduleName, host, _authToken, backupdir) => {
|
|
58
|
+
return new Promise(async function (resolve, reject) {
|
|
59
|
+
const exporteddata = await cli.prompt(message.promptMessageList.promptPathStoredData)
|
|
60
|
+
defaultConfig.management_token = managementTokens.token
|
|
61
|
+
defaultConfig.target_stack = managementTokens.apiKey
|
|
62
|
+
defaultConfig.auth_token = _authToken
|
|
63
|
+
if (moduleName && moduleName !== undefined) {
|
|
64
|
+
defaultConfig.moduleName = moduleName
|
|
65
|
+
}
|
|
66
|
+
defaultConfig.data = exporteddata
|
|
67
|
+
defaultConfig.host = host
|
|
68
|
+
if (backupdir) {
|
|
69
|
+
defaultConfig.useBackedupDir = backupdir;
|
|
70
|
+
}
|
|
71
|
+
initial(defaultConfig)
|
|
72
|
+
.then(() => {
|
|
73
|
+
return resolve()
|
|
74
|
+
}).catch((error) => {
|
|
75
|
+
return reject()
|
|
76
|
+
})
|
|
77
|
+
})
|
|
54
78
|
}
|
|
55
79
|
|
|
56
|
-
exports.configWithAuthToken = function (config, _authToken, moduleName, host) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
80
|
+
exports.configWithAuthToken = function (config, _authToken, moduleName, host, backupdir) {
|
|
81
|
+
return new Promise(async function (resolve, reject) {
|
|
82
|
+
let externalConfig = require(config)
|
|
83
|
+
defaultConfig.auth_token = _authToken
|
|
84
|
+
if (moduleName && moduleName !== undefined) {
|
|
85
|
+
defaultConfig.moduleName = moduleName
|
|
86
|
+
}
|
|
87
|
+
defaultConfig.host = host
|
|
88
|
+
|
|
89
|
+
if (externalConfig.modules) {
|
|
90
|
+
defaultConfig.modules.types = externalConfig.modules
|
|
91
|
+
delete externalConfig.modules
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (backupdir) {
|
|
95
|
+
defaultConfig.useBackedupDir = backupdir;
|
|
96
|
+
}
|
|
97
|
+
defaultConfig = _.merge(defaultConfig, externalConfig)
|
|
98
|
+
initial(defaultConfig)
|
|
99
|
+
.then(() => {
|
|
100
|
+
return resolve()
|
|
101
|
+
}).catch((error) => {
|
|
102
|
+
return reject()
|
|
103
|
+
})
|
|
104
|
+
})
|
|
66
105
|
}
|
|
67
106
|
|
|
68
|
-
exports.parametersWithAuthToken = function (
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
107
|
+
exports.parametersWithAuthToken = function (_authToken, targetStack, data, moduleName, host, backupdir) {
|
|
108
|
+
return new Promise(async function (resolve, reject) {
|
|
109
|
+
defaultConfig.auth_token = _authToken
|
|
110
|
+
defaultConfig.target_stack = targetStack
|
|
111
|
+
if (moduleName && moduleName !== undefined && backupdir === undefined) {
|
|
112
|
+
defaultConfig.moduleName = moduleName
|
|
113
|
+
} else if (moduleName && moduleName !== undefined && backupdir !== undefined) {
|
|
114
|
+
defaultConfig.moduleName = moduleName
|
|
115
|
+
defaultConfig.useBackedupDir = backupdir
|
|
116
|
+
}
|
|
117
|
+
defaultConfig.data = data
|
|
118
|
+
defaultConfig.host = host
|
|
119
|
+
|
|
120
|
+
initial(defaultConfig)
|
|
121
|
+
.then(() => {
|
|
122
|
+
return resolve()
|
|
123
|
+
}).catch((error) => {
|
|
124
|
+
return reject()
|
|
125
|
+
})
|
|
126
|
+
})
|
|
80
127
|
}
|
|
81
128
|
|
|
82
|
-
exports.withoutParametersWithAuthToken = async (_authToken, moduleName, host) => {
|
|
129
|
+
exports.withoutParametersWithAuthToken = async (_authToken, moduleName, host, backupdir) => {
|
|
130
|
+
return new Promise(async function (resolve, reject) {
|
|
131
|
+
const stackUid = await cli.prompt(message.promptMessageList.promptTargetStack)
|
|
132
|
+
const exporteddata = await cli.prompt(message.promptMessageList.promptPathStoredData)
|
|
133
|
+
defaultConfig.auth_token = _authToken
|
|
134
|
+
defaultConfig.target_stack = stackUid
|
|
135
|
+
defaultConfig.data = exporteddata
|
|
136
|
+
if (moduleName && moduleName !== undefined && backupdir === undefined) {
|
|
137
|
+
defaultConfig.moduleName = moduleName
|
|
138
|
+
} else if (moduleName && moduleName !== undefined && backupdir !== undefined) {
|
|
139
|
+
defaultConfig.moduleName = moduleName
|
|
140
|
+
defaultConfig.useBackedupDir = backupdir
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
defaultConfig.host = host
|
|
83
144
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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)
|
|
145
|
+
initial(defaultConfig)
|
|
146
|
+
.then(() => {
|
|
147
|
+
return resolve()
|
|
148
|
+
}).catch((error) => {
|
|
149
|
+
return reject()
|
|
150
|
+
})
|
|
151
|
+
})
|
|
98
152
|
}
|
package/src/lib/util/index.js
CHANGED
|
@@ -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)
|
|
@@ -37,10 +38,10 @@ exports.validateConfig = function (config) {
|
|
|
37
38
|
addlogs(config, chalk.red('Kindly provide Email and password'), 'error')
|
|
38
39
|
return 'error'
|
|
39
40
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
41
|
+
// if(!config.languagesCode.includes(config.master_locale.code)) {
|
|
42
|
+
// addlogs(config, chalk.red('Kindly provide valid master_locale code'), 'error')
|
|
43
|
+
// return 'error'
|
|
44
|
+
// }
|
|
44
45
|
}
|
|
45
46
|
|
|
46
47
|
exports.buildAppConfig = function (config) {
|
|
@@ -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(config, ctPath) {
|
|
129
|
+
return new Promise(function (resolve, reject) {
|
|
130
|
+
let client = stack.Client(config)
|
|
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(config.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: config.target_stack, management_token: config.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
|
};
|
package/src/lib/util/log.js
CHANGED
|
@@ -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(
|
|
97
|
+
init(configLogPath, type).log(message)
|
|
96
98
|
} else {
|
|
97
|
-
init(
|
|
99
|
+
init(configLogPath, type).error(message)
|
|
98
100
|
}
|
|
99
101
|
}
|
package/src/lib/util/login.js
CHANGED
|
@@ -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}).
|
|
39
|
+
client.stack({api_key: config.target_stack, management_token: config.management_token}).fetch()
|
|
40
40
|
.then(function () {
|
|
41
41
|
return resolve()
|
|
42
42
|
})
|
|
@@ -4,170 +4,168 @@
|
|
|
4
4
|
* MIT Licensed
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
var
|
|
7
|
+
// eslint-disable-next-line unicorn/filename-case
|
|
8
|
+
var path = require('path')
|
|
9
|
+
var _ = require('lodash')
|
|
9
10
|
|
|
10
|
-
var util = require('
|
|
11
|
-
var helper = require('./fs')
|
|
12
|
-
var config = util.getConfig()
|
|
11
|
+
var util = require('.')
|
|
12
|
+
var helper = require('./fs')
|
|
13
|
+
var config = util.getConfig()
|
|
13
14
|
// update references in entry object
|
|
14
15
|
module.exports = function (data, mappedUids, uidMapperPath) {
|
|
15
|
-
var parent = []
|
|
16
|
-
var uids = []
|
|
17
|
-
var unmapped = []
|
|
18
|
-
var mapped = []
|
|
16
|
+
var parent = []
|
|
17
|
+
var uids = []
|
|
18
|
+
var unmapped = []
|
|
19
|
+
var mapped = []
|
|
19
20
|
|
|
20
|
-
var isNewRefFields = false
|
|
21
|
-
var preserveStackVersion = config.preserveStackVersion
|
|
21
|
+
var isNewRefFields = false
|
|
22
|
+
var preserveStackVersion = config.preserveStackVersion
|
|
22
23
|
|
|
23
24
|
var update = function (parent, form_id, entry) {
|
|
24
|
-
var _entry = entry
|
|
25
|
-
|
|
25
|
+
var _entry = entry
|
|
26
|
+
var len = parent.length
|
|
26
27
|
for (var j = 0; j < len; j++) {
|
|
27
28
|
if (_entry && parent[j]) {
|
|
28
|
-
if (j
|
|
29
|
+
if (j === (len - 1) && _entry[parent[j]]) {
|
|
29
30
|
if (form_id !== '_assets') {
|
|
30
31
|
if (_entry[parent[j]].length) {
|
|
31
32
|
_entry[parent[j]].forEach((item, idx) => {
|
|
32
33
|
if (typeof item.uid === 'string' && item._content_type_uid) {
|
|
33
|
-
uids.push(item.uid)
|
|
34
|
+
uids.push(item.uid)
|
|
34
35
|
} else if (typeof item === 'string' && (preserveStackVersion === true)) {
|
|
35
|
-
uids.push(item)
|
|
36
|
+
uids.push(item)
|
|
36
37
|
} else {
|
|
37
|
-
uids.push(item)
|
|
38
|
+
uids.push(item)
|
|
38
39
|
_entry[parent[j]][idx] = {
|
|
39
40
|
uid: item,
|
|
40
|
-
_content_type_uid: form_id
|
|
41
|
-
}
|
|
41
|
+
_content_type_uid: form_id,
|
|
42
|
+
}
|
|
42
43
|
}
|
|
43
|
-
})
|
|
44
|
+
})
|
|
44
45
|
}
|
|
45
|
-
} else {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
uids.push(_entry[parent[j]][k]['uid']);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
} else {
|
|
53
|
-
if (_entry[parent[j]]['uid'].length) {
|
|
54
|
-
uids.push(_entry[parent[j]]['uid']);
|
|
46
|
+
} else if (Array.isArray(_entry[parent[j]])) {
|
|
47
|
+
for (var k = 0; k < _entry[parent[j]].length; k++) {
|
|
48
|
+
if (_entry[parent[j]][k].uid.length) {
|
|
49
|
+
uids.push(_entry[parent[j]][k].uid)
|
|
55
50
|
}
|
|
56
51
|
}
|
|
52
|
+
} else if (_entry[parent[j]].uid.length) {
|
|
53
|
+
uids.push(_entry[parent[j]].uid)
|
|
57
54
|
}
|
|
58
55
|
} else {
|
|
59
|
-
_entry = _entry[parent[j]]
|
|
60
|
-
var _keys = _.clone(parent).splice(eval(j + 1), len)
|
|
61
|
-
if (_entry
|
|
56
|
+
_entry = _entry[parent[j]]
|
|
57
|
+
var _keys = _.clone(parent).splice(eval(j + 1), len)
|
|
58
|
+
if (Array.isArray(_entry)) {
|
|
62
59
|
for (var i = 0, _i = _entry.length; i < _i; i++) {
|
|
63
|
-
update(_keys, form_id, _entry[i])
|
|
60
|
+
update(_keys, form_id, _entry[i])
|
|
64
61
|
}
|
|
65
62
|
} else if (!(_entry instanceof Object)) {
|
|
66
|
-
break
|
|
63
|
+
break
|
|
67
64
|
}
|
|
68
65
|
}
|
|
69
66
|
}
|
|
70
67
|
}
|
|
71
|
-
}
|
|
68
|
+
}
|
|
72
69
|
var find = function (schema, entry) {
|
|
73
70
|
for (var i = 0, _i = schema.length; i < _i; i++) {
|
|
74
71
|
switch (schema[i].data_type) {
|
|
75
72
|
case 'reference':
|
|
76
|
-
if (schema[i].reference_to
|
|
77
|
-
isNewRefFields = true
|
|
78
|
-
schema[i].reference_to.forEach(
|
|
79
|
-
parent.push(schema[i].uid)
|
|
80
|
-
update(parent, reference, entry)
|
|
81
|
-
parent.pop()
|
|
82
|
-
})
|
|
73
|
+
if (Array.isArray(schema[i].reference_to)) {
|
|
74
|
+
isNewRefFields = true
|
|
75
|
+
schema[i].reference_to.forEach(reference => {
|
|
76
|
+
parent.push(schema[i].uid)
|
|
77
|
+
update(parent, reference, entry)
|
|
78
|
+
parent.pop()
|
|
79
|
+
})
|
|
83
80
|
} else {
|
|
84
|
-
parent.push(schema[i].uid)
|
|
85
|
-
update(parent, schema[i].reference_to, entry)
|
|
86
|
-
parent.pop()
|
|
81
|
+
parent.push(schema[i].uid)
|
|
82
|
+
update(parent, schema[i].reference_to, entry)
|
|
83
|
+
parent.pop()
|
|
87
84
|
}
|
|
88
|
-
break
|
|
85
|
+
break
|
|
89
86
|
case 'group':
|
|
90
|
-
parent.push(schema[i].uid)
|
|
91
|
-
find(schema[i].schema, entry)
|
|
92
|
-
parent.pop()
|
|
93
|
-
break
|
|
87
|
+
parent.push(schema[i].uid)
|
|
88
|
+
find(schema[i].schema, entry)
|
|
89
|
+
parent.pop()
|
|
90
|
+
break
|
|
94
91
|
case 'blocks':
|
|
95
92
|
for (var j = 0, _j = schema[i].blocks.length; j < _j; j++) {
|
|
96
|
-
parent.push(schema[i].uid)
|
|
97
|
-
parent.push(schema[i].blocks[j].uid)
|
|
98
|
-
find(schema[i].blocks[j].schema, entry)
|
|
99
|
-
parent.pop()
|
|
100
|
-
parent.pop()
|
|
93
|
+
parent.push(schema[i].uid)
|
|
94
|
+
parent.push(schema[i].blocks[j].uid)
|
|
95
|
+
find(schema[i].blocks[j].schema, entry)
|
|
96
|
+
parent.pop()
|
|
97
|
+
parent.pop()
|
|
101
98
|
}
|
|
102
|
-
break
|
|
99
|
+
break
|
|
103
100
|
}
|
|
104
101
|
}
|
|
105
|
-
}
|
|
106
|
-
find(data.content_type.schema, data.entry)
|
|
102
|
+
}
|
|
103
|
+
find(data.content_type.schema, data.entry)
|
|
107
104
|
if (isNewRefFields) {
|
|
108
|
-
findUidsInNewRefFields(data.entry, uids)
|
|
105
|
+
findUidsInNewRefFields(data.entry, uids)
|
|
109
106
|
}
|
|
110
|
-
uids = _.flattenDeep(uids)
|
|
111
|
-
// if no references are found, return
|
|
107
|
+
uids = _.flattenDeep(uids)
|
|
108
|
+
// if no references are found, return
|
|
112
109
|
if (uids.length === 0) {
|
|
113
|
-
return data.entry
|
|
110
|
+
return data.entry
|
|
114
111
|
}
|
|
115
112
|
|
|
116
|
-
uids = _.uniq(uids)
|
|
117
|
-
var entry = JSON.stringify(data.entry)
|
|
118
|
-
uids.forEach(function (uid) {
|
|
113
|
+
uids = _.uniq(uids)
|
|
114
|
+
var entry = JSON.stringify(data.entry)
|
|
115
|
+
uids.forEach(function (uid) {
|
|
119
116
|
if (mappedUids.hasOwnProperty(uid)) {
|
|
120
|
-
entry = entry.replace(new RegExp(uid, 'img'), mappedUids[uid])
|
|
121
|
-
mapped.push(uid)
|
|
117
|
+
entry = entry.replace(new RegExp(uid, 'img'), mappedUids[uid])
|
|
118
|
+
mapped.push(uid)
|
|
122
119
|
} else {
|
|
123
|
-
unmapped.push(uid)
|
|
120
|
+
unmapped.push(uid)
|
|
124
121
|
}
|
|
125
|
-
})
|
|
122
|
+
})
|
|
126
123
|
|
|
127
|
-
if (unmapped.length) {
|
|
128
|
-
var unmappedUids = helper.readFile(path.join(uidMapperPath, 'unmapped-uids.json'))
|
|
129
|
-
unmappedUids = unmappedUids || {}
|
|
124
|
+
if (unmapped.length > 0) {
|
|
125
|
+
var unmappedUids = helper.readFile(path.join(uidMapperPath, 'unmapped-uids.json'))
|
|
126
|
+
unmappedUids = unmappedUids || {}
|
|
130
127
|
if (unmappedUids.hasOwnProperty(data.content_type.uid)) {
|
|
131
|
-
unmappedUids[data.content_type.uid][data.entry.uid] = unmapped
|
|
128
|
+
unmappedUids[data.content_type.uid][data.entry.uid] = unmapped
|
|
132
129
|
} else {
|
|
133
130
|
unmappedUids[data.content_type.uid] = {
|
|
134
|
-
[data.entry.uid]: unmapped
|
|
135
|
-
}
|
|
131
|
+
[data.entry.uid]: unmapped,
|
|
132
|
+
}
|
|
136
133
|
}
|
|
137
134
|
// write the unmapped contents to ./mapper/language/unmapped-uids.json
|
|
138
|
-
helper.writeFile(path.join(uidMapperPath, 'unmapped-uids.json'), unmappedUids)
|
|
135
|
+
helper.writeFile(path.join(uidMapperPath, 'unmapped-uids.json'), unmappedUids)
|
|
139
136
|
}
|
|
140
137
|
|
|
141
|
-
if (mapped.length) {
|
|
142
|
-
var _mappedUids = helper.readFile(path.join(uidMapperPath, 'mapped-uids.json'))
|
|
143
|
-
_mappedUids = _mappedUids || {}
|
|
138
|
+
if (mapped.length > 0) {
|
|
139
|
+
var _mappedUids = helper.readFile(path.join(uidMapperPath, 'mapped-uids.json'))
|
|
140
|
+
_mappedUids = _mappedUids || {}
|
|
144
141
|
if (_mappedUids.hasOwnProperty(data.content_type.uid)) {
|
|
145
|
-
_mappedUids[data.content_type.uid][data.entry.uid] = mapped
|
|
142
|
+
_mappedUids[data.content_type.uid][data.entry.uid] = mapped
|
|
146
143
|
} else {
|
|
147
144
|
_mappedUids[data.content_type.uid] = {
|
|
148
|
-
[data.entry.uid]: mapped
|
|
149
|
-
}
|
|
145
|
+
[data.entry.uid]: mapped,
|
|
146
|
+
}
|
|
150
147
|
}
|
|
151
148
|
// write the mapped contents to ./mapper/language/mapped-uids.json
|
|
152
|
-
helper.writeFile(path.join(uidMapperPath, 'mapped-uids.json'), _mappedUids)
|
|
149
|
+
helper.writeFile(path.join(uidMapperPath, 'mapped-uids.json'), _mappedUids)
|
|
153
150
|
}
|
|
154
151
|
|
|
155
|
-
return JSON.parse(entry)
|
|
156
|
-
}
|
|
152
|
+
return JSON.parse(entry)
|
|
153
|
+
}
|
|
157
154
|
|
|
158
155
|
function findUidsInNewRefFields(entry, uids) {
|
|
159
156
|
if (entry && typeof entry === 'object') {
|
|
160
157
|
if (entry.uid && entry._content_type_uid) {
|
|
161
|
-
uids.push(entry.uid)
|
|
162
|
-
} else if (entry
|
|
163
|
-
entry.forEach(function(elem) {
|
|
164
|
-
findUidsInNewRefFields(elem, uids)
|
|
165
|
-
})
|
|
158
|
+
uids.push(entry.uid)
|
|
159
|
+
} else if (Array.isArray(entry) && entry.length) {
|
|
160
|
+
entry.forEach(function (elem) {
|
|
161
|
+
findUidsInNewRefFields(elem, uids)
|
|
162
|
+
})
|
|
166
163
|
} else if (Object.keys(entry).length) {
|
|
167
164
|
for (var key in entry) {
|
|
168
|
-
|
|
165
|
+
if (key) {
|
|
166
|
+
findUidsInNewRefFields(entry[key], uids)
|
|
167
|
+
}
|
|
169
168
|
}
|
|
170
169
|
}
|
|
171
170
|
}
|
|
172
|
-
|
|
173
|
-
}
|
|
171
|
+
}
|