@contentstack/cli-cm-import 1.5.10 → 1.6.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.
- package/README.md +6 -14
- package/bin/dev +17 -0
- package/bin/dev.cmd +3 -0
- package/bin/run +6 -0
- package/bin/run.cmd +3 -0
- package/lib/commands/cm/stacks/import.d.ts +10 -0
- package/lib/commands/cm/stacks/import.js +111 -0
- package/lib/config/index.d.ts +3 -0
- package/lib/config/index.js +372 -0
- package/lib/import/index.d.ts +1 -0
- package/lib/import/index.js +8 -0
- package/lib/import/module-importer.d.ts +13 -0
- package/lib/import/module-importer.js +70 -0
- package/lib/import/modules/assets.d.ts +63 -0
- package/lib/import/modules/assets.js +265 -0
- package/lib/import/modules/base-class.d.ts +69 -0
- package/lib/import/modules/base-class.js +165 -0
- package/lib/import/modules/index.d.ts +2 -0
- package/lib/import/modules/index.js +19 -0
- package/lib/import/modules/locales.d.ts +31 -0
- package/lib/import/modules/locales.js +152 -0
- package/lib/import/modules-js/assets.d.ts +33 -0
- package/lib/import/modules-js/assets.js +415 -0
- package/lib/import/modules-js/content-types.d.ts +33 -0
- package/lib/import/modules-js/content-types.js +176 -0
- package/lib/import/modules-js/custom-roles.d.ts +15 -0
- package/lib/import/modules-js/custom-roles.js +141 -0
- package/lib/import/modules-js/entries.d.ts +54 -0
- package/lib/import/modules-js/entries.js +1260 -0
- package/lib/import/modules-js/environments.d.ts +13 -0
- package/lib/import/modules-js/environments.js +85 -0
- package/lib/import/modules-js/extensions.d.ts +17 -0
- package/lib/import/modules-js/extensions.js +86 -0
- package/lib/import/modules-js/global-fields.d.ts +13 -0
- package/lib/import/modules-js/global-fields.js +109 -0
- package/lib/import/modules-js/index.d.ts +1 -0
- package/lib/import/modules-js/index.js +33 -0
- package/lib/import/modules-js/labels.d.ts +20 -0
- package/lib/import/modules-js/labels.js +148 -0
- package/lib/import/modules-js/locales.d.ts +24 -0
- package/lib/import/modules-js/locales.js +196 -0
- package/lib/import/modules-js/marketplace-apps.d.ts +60 -0
- package/lib/import/modules-js/marketplace-apps.js +409 -0
- package/lib/import/modules-js/webhooks.d.ts +17 -0
- package/lib/import/modules-js/webhooks.js +85 -0
- package/lib/import/modules-js/workflows.d.ts +18 -0
- package/lib/import/modules-js/workflows.js +132 -0
- package/lib/types/default-config.d.ts +130 -0
- package/lib/types/default-config.js +2 -0
- package/lib/types/import-config.d.ts +51 -0
- package/lib/types/import-config.js +2 -0
- package/lib/types/index.d.ts +30 -0
- package/lib/types/index.js +2 -0
- package/lib/utils/asset-helper.d.ts +4 -0
- package/lib/utils/asset-helper.js +387 -0
- package/lib/utils/backup-handler.d.ts +2 -0
- package/lib/utils/backup-handler.js +31 -0
- package/lib/utils/common-helper.d.ts +20 -0
- package/lib/utils/common-helper.js +244 -0
- package/lib/utils/content-type-helper.d.ts +49 -0
- package/lib/utils/content-type-helper.js +143 -0
- package/lib/utils/entries-helper.d.ts +4 -0
- package/lib/utils/entries-helper.js +252 -0
- package/lib/utils/extension-helper.d.ts +10 -0
- package/lib/utils/extension-helper.js +72 -0
- package/lib/utils/file-helper.d.ts +14 -0
- package/lib/utils/file-helper.js +140 -0
- package/lib/utils/import-config-handler.d.ts +3 -0
- package/lib/utils/import-config-handler.js +73 -0
- package/lib/utils/index.d.ts +12 -0
- package/lib/utils/index.js +29 -0
- package/lib/utils/interactive.d.ts +2 -0
- package/lib/utils/interactive.js +23 -0
- package/lib/utils/logger.d.ts +8 -0
- package/lib/utils/logger.js +154 -0
- package/lib/utils/login-handler.d.ts +8 -0
- package/lib/utils/login-handler.js +53 -0
- package/lib/utils/marketplace-app-helper.d.ts +4 -0
- package/lib/utils/marketplace-app-helper.js +31 -0
- package/messages/index.json +1 -7
- package/oclif.manifest.json +2 -2
- package/package.json +47 -21
- package/src/app.js +0 -217
- package/src/commands/cm/stacks/import.js +0 -161
- package/src/config/default.js +0 -352
- package/src/lib/import/assets.js +0 -495
- package/src/lib/import/content-types.js +0 -201
- package/src/lib/import/custom-roles.js +0 -169
- package/src/lib/import/entries.js +0 -1480
- package/src/lib/import/environments.js +0 -106
- package/src/lib/import/extensions.js +0 -108
- package/src/lib/import/global-fields.js +0 -135
- package/src/lib/import/labels.js +0 -175
- package/src/lib/import/locales.js +0 -216
- package/src/lib/import/marketplace-apps.js +0 -542
- package/src/lib/import/webhooks.js +0 -113
- package/src/lib/import/workflows.js +0 -166
- package/src/lib/util/extensionsUidReplace.js +0 -67
- package/src/lib/util/fs.js +0 -124
- package/src/lib/util/import-flags.js +0 -187
- package/src/lib/util/index.js +0 -222
- package/src/lib/util/log.js +0 -144
- package/src/lib/util/login.js +0 -58
- package/src/lib/util/lookupReplaceAssets.js +0 -366
- package/src/lib/util/lookupReplaceEntries.js +0 -250
- package/src/lib/util/marketplace-app-helper.js +0 -31
- package/src/lib/util/removeReferenceFields.js +0 -59
- package/src/lib/util/schemaTemplate.js +0 -38
- package/src/lib/util/supress-mandatory-fields.js +0 -34
- package/src/lib/util/upload.js +0 -56
|
@@ -1,366 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Contentstack Import
|
|
3
|
-
* Copyright (c) 2019 Contentstack LLC
|
|
4
|
-
* MIT Licensed
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
let url = require('url');
|
|
8
|
-
let path = require('path');
|
|
9
|
-
let _ = require('lodash');
|
|
10
|
-
let { marked } = require('marked');
|
|
11
|
-
|
|
12
|
-
let helper = require('./fs');
|
|
13
|
-
|
|
14
|
-
// get assets object
|
|
15
|
-
module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMapperPath, installedExtensions) {
|
|
16
|
-
if (
|
|
17
|
-
!_.has(data, 'entry') ||
|
|
18
|
-
!_.has(data, 'content_type') ||
|
|
19
|
-
!_.isPlainObject(mappedAssetUids) ||
|
|
20
|
-
!_.isPlainObject(mappedAssetUrls) ||
|
|
21
|
-
typeof assetUidMapperPath !== 'string'
|
|
22
|
-
) {
|
|
23
|
-
throw new Error('Invalid inputs for lookupAssets!');
|
|
24
|
-
}
|
|
25
|
-
let parent = [];
|
|
26
|
-
let assetUids = [];
|
|
27
|
-
let assetUrls = [];
|
|
28
|
-
let unmatchedUids = [];
|
|
29
|
-
let unmatchedUrls = [];
|
|
30
|
-
let matchedUids = [];
|
|
31
|
-
let matchedUrls = [];
|
|
32
|
-
|
|
33
|
-
let find = function (schema, entryToFind) {
|
|
34
|
-
for (let i = 0, _i = schema.length; i < _i; i++) {
|
|
35
|
-
if (
|
|
36
|
-
schema[i].data_type === 'text' &&
|
|
37
|
-
schema[i].field_metadata &&
|
|
38
|
-
(schema[i].field_metadata.markdown || schema[i].field_metadata.rich_text_type)
|
|
39
|
-
) {
|
|
40
|
-
parent.push(schema[i].uid);
|
|
41
|
-
findFileUrls(schema[i], entryToFind, assetUrls);
|
|
42
|
-
parent.pop();
|
|
43
|
-
}
|
|
44
|
-
if (schema[i].data_type === 'group' || schema[i].data_type === 'global_field') {
|
|
45
|
-
parent.push(schema[i].uid);
|
|
46
|
-
find(schema[i].schema, entryToFind);
|
|
47
|
-
parent.pop();
|
|
48
|
-
}
|
|
49
|
-
if (schema[i].data_type === 'blocks') {
|
|
50
|
-
for (let j = 0, _j = schema[i].blocks.length; j < _j; j++) {
|
|
51
|
-
if (schema[i].blocks[j].schema) {
|
|
52
|
-
parent.push(schema[i].uid);
|
|
53
|
-
parent.push(schema[i].blocks[j].uid);
|
|
54
|
-
find(schema[i].blocks[j].schema, entryToFind);
|
|
55
|
-
parent.pop();
|
|
56
|
-
parent.pop();
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
// added rich_text_type field check because some marketplace extensions also
|
|
61
|
-
// have data_type has json
|
|
62
|
-
if (schema[i].data_type === 'json' && schema[i].field_metadata.rich_text_type) {
|
|
63
|
-
parent.push(schema[i].uid);
|
|
64
|
-
// findFileUrls(schema[i], entry, assetUrls)
|
|
65
|
-
findAssetIdsFromJsonRte(data.entry, data.content_type.schema);
|
|
66
|
-
// maybe only one of these checks would be enough
|
|
67
|
-
parent.pop();
|
|
68
|
-
} else if (
|
|
69
|
-
schema[i].data_type === 'json' &&
|
|
70
|
-
schema[i].field_metadata.extension &&
|
|
71
|
-
schema[i].field_metadata.is_asset
|
|
72
|
-
) {
|
|
73
|
-
findAssetIdsFromJsonCustomFields(data.entry, data.content_type.schema);
|
|
74
|
-
} else if (schema[i].data_type === 'json' && schema[i].field_metadata.extension) {
|
|
75
|
-
if (installedExtensions && installedExtensions[schema[i].extension_uid]) {
|
|
76
|
-
schema[i].extension_uid = installedExtensions[schema[i].extension_uid];
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
function findAssetIdsFromJsonCustomFields(entryObj, ctSchema) {
|
|
83
|
-
ctSchema.map((row) => {
|
|
84
|
-
if (row.data_type === 'json') {
|
|
85
|
-
if (entryObj[row.uid] && row.field_metadata.extension && row.field_metadata.is_asset) {
|
|
86
|
-
if (installedExtensions && installedExtensions[row.extension_uid]) {
|
|
87
|
-
row.extension_uid = installedExtensions[row.extension_uid];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (entryObj[row.uid].metadata && entryObj[row.uid].metadata.extension_uid) {
|
|
91
|
-
if (installedExtensions && installedExtensions[entryObj[row.uid].metadata.extension_uid]) {
|
|
92
|
-
entryObj[row.uid].metadata.extension_uid = installedExtensions[entryObj[row.uid].metadata.extension_uid];
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return row;
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function findAssetIdsFromJsonRte(entryObj, ctSchema) {
|
|
103
|
-
for (const element of ctSchema) {
|
|
104
|
-
switch (element.data_type) {
|
|
105
|
-
case 'blocks': {
|
|
106
|
-
if (entryObj[element.uid]) {
|
|
107
|
-
if (element.multiple) {
|
|
108
|
-
entryObj[element.uid].forEach((e) => {
|
|
109
|
-
let key = Object.keys(e).pop();
|
|
110
|
-
let subBlock = element.blocks.filter((block) => block.uid === key).pop();
|
|
111
|
-
findAssetIdsFromJsonRte(e[key], subBlock.schema);
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
|
-
case 'global_field':
|
|
118
|
-
case 'group': {
|
|
119
|
-
if (entryObj[element.uid]) {
|
|
120
|
-
if (element.multiple) {
|
|
121
|
-
entryObj[element.uid].forEach((e) => {
|
|
122
|
-
findAssetIdsFromJsonRte(e, element.schema);
|
|
123
|
-
});
|
|
124
|
-
} else {
|
|
125
|
-
findAssetIdsFromJsonRte(entryObj[element.uid], element.schema);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
break;
|
|
129
|
-
}
|
|
130
|
-
case 'json': {
|
|
131
|
-
if (entryObj[element.uid] && element.field_metadata.rich_text_type) {
|
|
132
|
-
if (element.multiple) {
|
|
133
|
-
entryObj[element.uid].forEach((jsonRteData) => {
|
|
134
|
-
gatherJsonRteAssetIds(jsonRteData);
|
|
135
|
-
});
|
|
136
|
-
} else {
|
|
137
|
-
gatherJsonRteAssetIds(entryObj[element.uid]);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function gatherJsonRteAssetIds(jsonRteData) {
|
|
147
|
-
jsonRteData.children.forEach((element) => {
|
|
148
|
-
if (element.type) {
|
|
149
|
-
switch (element.type) {
|
|
150
|
-
case 'a':
|
|
151
|
-
case 'p': {
|
|
152
|
-
if (element.children && element.children.length > 0) {
|
|
153
|
-
gatherJsonRteAssetIds(element);
|
|
154
|
-
}
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
case 'reference': {
|
|
158
|
-
if (Object.keys(element.attrs).length > 0 && element.attrs.type === 'asset') {
|
|
159
|
-
if (assetUids.indexOf(element.attrs['asset-uid']) === -1) {
|
|
160
|
-
assetUids.push(element.attrs['asset-uid']);
|
|
161
|
-
}
|
|
162
|
-
// assets references inserted as link inside entry reference inserted as link did not have asset-link property
|
|
163
|
-
// instead it had an 'href' property. I haven't seen 'asset-link' and 'href' together yet
|
|
164
|
-
// writing this condition assuming that this never occurs, need to confirm
|
|
165
|
-
// (element.attrs['asset-link']) ? assetUrls.push(element.attrs['asset-link']) : assetUrls.push(element.attrs['asset-link'])
|
|
166
|
-
if (element.attrs['asset-link']) {
|
|
167
|
-
if (assetUrls.indexOf(element.attrs['asset-link']) === -1) {
|
|
168
|
-
assetUrls.push(element.attrs['asset-link']);
|
|
169
|
-
}
|
|
170
|
-
} else if (element.attrs['href']) {
|
|
171
|
-
if (assetUrls.indexOf(element.attrs['href']) === -1) {
|
|
172
|
-
assetUrls.push(element.attrs['href']);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
if (element.children && element.children.length > 0) {
|
|
177
|
-
gatherJsonRteAssetIds(element);
|
|
178
|
-
}
|
|
179
|
-
break;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
find(data.content_type.schema, data.entry);
|
|
187
|
-
updateFileFields(data.entry, data, null, mappedAssetUids, matchedUids, unmatchedUids, mappedAssetUrls);
|
|
188
|
-
assetUids = _.uniq(assetUids);
|
|
189
|
-
assetUrls = _.uniq(assetUrls);
|
|
190
|
-
let entry = JSON.stringify(data.entry);
|
|
191
|
-
|
|
192
|
-
assetUrls.forEach(function (assetUrl) {
|
|
193
|
-
let mappedAssetUrl = mappedAssetUrls[assetUrl];
|
|
194
|
-
if (typeof mappedAssetUrl !== 'undefined') {
|
|
195
|
-
entry = entry.replace(new RegExp(assetUrl, 'img'), mappedAssetUrl);
|
|
196
|
-
matchedUrls.push(mappedAssetUrl);
|
|
197
|
-
} else {
|
|
198
|
-
unmatchedUrls.push(assetUrl);
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
assetUids.forEach(function (assetUid) {
|
|
203
|
-
let uid = mappedAssetUids[assetUid];
|
|
204
|
-
if (typeof uid !== 'undefined') {
|
|
205
|
-
entry = entry.replace(new RegExp(assetUid, 'img'), uid);
|
|
206
|
-
matchedUids.push(assetUid);
|
|
207
|
-
} else {
|
|
208
|
-
unmatchedUids.push(assetUid);
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
if (matchedUids.length) {
|
|
213
|
-
let matchedAssetUids = helper.readFileSync(path.join(assetUidMapperPath, 'matched-asset-uids.json'));
|
|
214
|
-
matchedAssetUids = matchedAssetUids || {};
|
|
215
|
-
if (matchedAssetUids.hasOwnProperty(data.content_type.uid)) {
|
|
216
|
-
matchedAssetUids[data.content_type.uid][data.entry.uid] = matchedUids;
|
|
217
|
-
} else {
|
|
218
|
-
matchedAssetUids[data.content_type.uid] = {
|
|
219
|
-
[data.entry.uid]: matchedUids,
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
if (!helper.fileExistsSync(path.join(assetUidMapperPath, 'matched-asset-uids.json'))) {
|
|
223
|
-
helper.writeFile(path.join(assetUidMapperPath, 'matched-asset-uids.json'));
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (unmatchedUids.length) {
|
|
228
|
-
let unmatchedAssetUids = helper.readFileSync(path.join(assetUidMapperPath, 'unmatched-asset-uids.json'));
|
|
229
|
-
unmatchedAssetUids = unmatchedAssetUids || {};
|
|
230
|
-
if (unmatchedAssetUids.hasOwnProperty(data.content_type.uid)) {
|
|
231
|
-
unmatchedAssetUids[data.content_type.uid][data.entry.uid] = unmatchedUids;
|
|
232
|
-
} else {
|
|
233
|
-
unmatchedAssetUids[data.content_type.uid] = {
|
|
234
|
-
[data.entry.uid]: unmatchedUids,
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
helper.writeFile(path.join(assetUidMapperPath, 'unmatched-asset-uids.json'));
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if (unmatchedUrls.length) {
|
|
241
|
-
let unmatchedAssetUrls = helper.readFileSync(path.join(assetUidMapperPath, 'unmatched-asset-urls.json'));
|
|
242
|
-
unmatchedAssetUrls = unmatchedAssetUrls || {};
|
|
243
|
-
if (unmatchedAssetUrls.hasOwnProperty(data.content_type.uid)) {
|
|
244
|
-
unmatchedAssetUrls[data.content_type.uid][data.entry.uid] = unmatchedUrls;
|
|
245
|
-
} else {
|
|
246
|
-
unmatchedAssetUrls[data.content_type.uid] = {
|
|
247
|
-
[data.entry.uid]: unmatchedUrls,
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
helper.writeFile(path.join(assetUidMapperPath, 'unmatched-asset-urls.json'));
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if (matchedUrls.length) {
|
|
254
|
-
let matchedAssetUrls = helper.readFileSync(path.join(assetUidMapperPath, 'matched-asset-urls.json'));
|
|
255
|
-
matchedAssetUrls = matchedAssetUrls || {};
|
|
256
|
-
if (matchedAssetUrls.hasOwnProperty(data.content_type.uid)) {
|
|
257
|
-
matchedAssetUrls[data.content_type.uid][data.entry.uid] = matchedUrls;
|
|
258
|
-
} else {
|
|
259
|
-
matchedAssetUrls[data.content_type.uid] = {
|
|
260
|
-
[data.entry.uid]: matchedUrls,
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
helper.writeFile(path.join(assetUidMapperPath, 'matched-asset-urls.json'));
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
return JSON.parse(entry);
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
function findFileUrls(schema, _entry, assetUrls) {
|
|
270
|
-
let markdownRegEx;
|
|
271
|
-
let markdownMatch;
|
|
272
|
-
|
|
273
|
-
// Regex to detect v2 asset uri patterns
|
|
274
|
-
let _matches, regex;
|
|
275
|
-
if (schema && schema.field_metadata && schema.field_metadata.markdown) {
|
|
276
|
-
regex = new RegExp(
|
|
277
|
-
// eslint-disable-next-line no-control-regex
|
|
278
|
-
'https://(contentstack-|)api.(built|contentstack).io/(.*?)/download(.*?)uid=([a-z0-9]+[^?&s\n])((.*)[\ns]?)',
|
|
279
|
-
'g',
|
|
280
|
-
);
|
|
281
|
-
} else {
|
|
282
|
-
regex = new RegExp(
|
|
283
|
-
'https://(contentstack-|)api.(built|contentstack).io/(.*?)/download(.*?)uid=([a-z0-9]+[^?&\'"])(.*?)',
|
|
284
|
-
'g',
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
while ((_matches = regex.exec(_entry)) !== null) {
|
|
288
|
-
if (_matches && _matches.length) {
|
|
289
|
-
if (_matches[0]) {
|
|
290
|
-
assetUrls.push(url.parse(_matches[0]).pathname.split('/').slice(1).join('/'));
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
let text;
|
|
295
|
-
// Regex to detect v3 asset uri patterns
|
|
296
|
-
if (schema && schema.field_metadata && schema.field_metadata.markdown) {
|
|
297
|
-
text = marked(JSON.stringify(_entry));
|
|
298
|
-
} else {
|
|
299
|
-
text = JSON.stringify(_entry);
|
|
300
|
-
}
|
|
301
|
-
markdownRegEx = new RegExp(
|
|
302
|
-
'(https://(assets|(eu-|azure-na-|azure-eu-)?images).contentstack.(io|com)/v3/assets/(.*?)/(.*?)/(.*?)/(.*?)(?="))',
|
|
303
|
-
'g',
|
|
304
|
-
);
|
|
305
|
-
while ((markdownMatch = markdownRegEx.exec(text)) !== null) {
|
|
306
|
-
if (markdownMatch && typeof markdownMatch[0] === 'string') {
|
|
307
|
-
assetUrls.push(markdownMatch[0]);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function updateFileFields(objekt, parent, pos, mappedAssetUids, matchedUids, unmatchedUids, mappedAssetUrls) {
|
|
313
|
-
if (_.isPlainObject(objekt) && _.has(objekt, 'filename') && _.has(objekt, 'uid')) {
|
|
314
|
-
if (typeof pos !== 'undefined') {
|
|
315
|
-
if (typeof pos === 'number' || typeof pos === 'string') {
|
|
316
|
-
const replacer = () => {
|
|
317
|
-
if (mappedAssetUids.hasOwnProperty(objekt.uid)) {
|
|
318
|
-
parent[pos] = mappedAssetUids[objekt.uid];
|
|
319
|
-
matchedUids.push(objekt.uid);
|
|
320
|
-
} else {
|
|
321
|
-
parent[pos] = '';
|
|
322
|
-
unmatchedUids.push(objekt.uid);
|
|
323
|
-
}
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
if (parent.uid && mappedAssetUids[parent.uid]) {
|
|
327
|
-
parent.uid = mappedAssetUids[parent.uid];
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
if (
|
|
331
|
-
objekt &&
|
|
332
|
-
_.isObject(parent[pos]) &&
|
|
333
|
-
parent[pos].uid &&
|
|
334
|
-
parent[pos].url &&
|
|
335
|
-
_.has(parent, 'asset') &&
|
|
336
|
-
_.has(parent, '_content_type_uid') &&
|
|
337
|
-
parent._content_type_uid === 'sys_assets'
|
|
338
|
-
) {
|
|
339
|
-
if (
|
|
340
|
-
_.has(parent, 'asset') &&
|
|
341
|
-
_.has(parent, '_content_type_uid') &&
|
|
342
|
-
parent._content_type_uid === 'sys_assets'
|
|
343
|
-
) {
|
|
344
|
-
parent = _.omit(parent, ['asset']);
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
if (objekt.uid && mappedAssetUids && mappedAssetUids[objekt.uid]) {
|
|
348
|
-
objekt.uid = mappedAssetUids[objekt.uid];
|
|
349
|
-
}
|
|
350
|
-
if (objekt.url && mappedAssetUrls && mappedAssetUrls[objekt.url]) {
|
|
351
|
-
objekt.url = mappedAssetUrls[objekt.url];
|
|
352
|
-
}
|
|
353
|
-
} else {
|
|
354
|
-
replacer();
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
} else if (_.isPlainObject(objekt)) {
|
|
359
|
-
for (let key in objekt) updateFileFields(objekt[key], objekt, key, mappedAssetUids, matchedUids, unmatchedUids);
|
|
360
|
-
} else if (_.isArray(objekt) && objekt.length) {
|
|
361
|
-
for (let i = 0; i <= objekt.length; i++)
|
|
362
|
-
updateFileFields(objekt[i], objekt, i, mappedAssetUids, matchedUids, unmatchedUids);
|
|
363
|
-
|
|
364
|
-
parent[pos] = _.compact(objekt);
|
|
365
|
-
}
|
|
366
|
-
}
|
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Contentstack Import
|
|
3
|
-
* Copyright (c) 2019 Contentstack LLC
|
|
4
|
-
* MIT Licensed
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// eslint-disable-next-line unicorn/filename-case
|
|
8
|
-
const path = require('path');
|
|
9
|
-
const _ = require('lodash');
|
|
10
|
-
|
|
11
|
-
const util = require('.');
|
|
12
|
-
const helper = require('./fs');
|
|
13
|
-
const config = util.getConfig();
|
|
14
|
-
// update references in entry object
|
|
15
|
-
module.exports = function (data, mappedUids, uidMapperPath) {
|
|
16
|
-
let parent = [];
|
|
17
|
-
let uids = [];
|
|
18
|
-
let unmapped = [];
|
|
19
|
-
let mapped = [];
|
|
20
|
-
|
|
21
|
-
let isNewRefFields = false;
|
|
22
|
-
let preserveStackVersion = config.preserveStackVersion;
|
|
23
|
-
|
|
24
|
-
function gatherJsonRteEntryIds(jsonRteData) {
|
|
25
|
-
jsonRteData.children.forEach((element) => {
|
|
26
|
-
if (element.type) {
|
|
27
|
-
switch (element.type) {
|
|
28
|
-
case 'a':
|
|
29
|
-
case 'p': {
|
|
30
|
-
if (element.children && element.children.length > 0) {
|
|
31
|
-
gatherJsonRteEntryIds(element);
|
|
32
|
-
}
|
|
33
|
-
break;
|
|
34
|
-
}
|
|
35
|
-
case 'reference': {
|
|
36
|
-
if (Object.keys(element.attrs).length > 0 && element.attrs.type === 'entry') {
|
|
37
|
-
if (uids.indexOf(element.attrs['entry-uid']) === -1) {
|
|
38
|
-
uids.push(element.attrs['entry-uid']);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (element.children && element.children.length > 0) {
|
|
42
|
-
gatherJsonRteEntryIds(element);
|
|
43
|
-
}
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const update = function (_parent, form_id, updateEntry) {
|
|
52
|
-
let _entry = updateEntry;
|
|
53
|
-
let len = _parent.length;
|
|
54
|
-
|
|
55
|
-
for (let j = 0; j < len; j++) {
|
|
56
|
-
if (_entry && _parent[j]) {
|
|
57
|
-
if (j === len - 1 && _entry[_parent[j]]) {
|
|
58
|
-
if (form_id !== '_assets') {
|
|
59
|
-
if (_entry[_parent[j]].length) {
|
|
60
|
-
_entry[_parent[j]].forEach((item, idx) => {
|
|
61
|
-
if (typeof item.uid === 'string' && item._content_type_uid) {
|
|
62
|
-
uids.push(item.uid);
|
|
63
|
-
} else if (typeof item === 'string' && preserveStackVersion === true) {
|
|
64
|
-
uids.push(item);
|
|
65
|
-
} else {
|
|
66
|
-
uids.push(item);
|
|
67
|
-
_entry[_parent[j]][idx] = {
|
|
68
|
-
uid: item,
|
|
69
|
-
_content_type_uid: form_id,
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
} else if (Array.isArray(_entry[_parent[j]])) {
|
|
75
|
-
for (const element of _entry[_parent[j]]) {
|
|
76
|
-
if (element.uid.length) {
|
|
77
|
-
uids.push(element.uid);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
} else if (_entry[_parent[j]].uid.length) {
|
|
81
|
-
uids.push(_entry[_parent[j]].uid);
|
|
82
|
-
}
|
|
83
|
-
} else {
|
|
84
|
-
_entry = _entry[_parent[j]];
|
|
85
|
-
let _keys = _.clone(_parent).splice(j + 1, len);
|
|
86
|
-
if (Array.isArray(_entry)) {
|
|
87
|
-
for (let i = 0, _i = _entry.length; i < _i; i++) {
|
|
88
|
-
update(_keys, form_id, _entry[i]);
|
|
89
|
-
}
|
|
90
|
-
} else if (!(_entry instanceof Object)) {
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
const find = function (schema, _entry) {
|
|
98
|
-
for (let i = 0, _i = schema.length; i < _i; i++) {
|
|
99
|
-
switch (schema[i].data_type) {
|
|
100
|
-
case 'reference':
|
|
101
|
-
if (Array.isArray(schema[i].reference_to)) {
|
|
102
|
-
isNewRefFields = true;
|
|
103
|
-
schema[i].reference_to.forEach((reference) => {
|
|
104
|
-
parent.push(schema[i].uid);
|
|
105
|
-
update(parent, reference, _entry);
|
|
106
|
-
parent.pop();
|
|
107
|
-
});
|
|
108
|
-
} else {
|
|
109
|
-
parent.push(schema[i].uid);
|
|
110
|
-
update(parent, schema[i].reference_to, _entry);
|
|
111
|
-
parent.pop();
|
|
112
|
-
}
|
|
113
|
-
break;
|
|
114
|
-
case 'global_field':
|
|
115
|
-
case 'group':
|
|
116
|
-
parent.push(schema[i].uid);
|
|
117
|
-
find(schema[i].schema, _entry);
|
|
118
|
-
parent.pop();
|
|
119
|
-
break;
|
|
120
|
-
case 'blocks':
|
|
121
|
-
for (let j = 0, _j = schema[i].blocks.length; j < _j; j++) {
|
|
122
|
-
parent.push(schema[i].uid);
|
|
123
|
-
parent.push(schema[i].blocks[j].uid);
|
|
124
|
-
find(schema[i].blocks[j].schema, _entry);
|
|
125
|
-
parent.pop();
|
|
126
|
-
parent.pop();
|
|
127
|
-
}
|
|
128
|
-
break;
|
|
129
|
-
case 'json':
|
|
130
|
-
if (schema[i].field_metadata.rich_text_type) {
|
|
131
|
-
findEntryIdsFromJsonRte(data.entry, data.content_type.schema);
|
|
132
|
-
}
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
function findEntryIdsFromJsonRte(entry, ctSchema) {
|
|
139
|
-
for (const element of ctSchema) {
|
|
140
|
-
switch (element.data_type) {
|
|
141
|
-
case 'blocks': {
|
|
142
|
-
if (entry[element.uid]) {
|
|
143
|
-
if (element.multiple) {
|
|
144
|
-
entry[element.uid].forEach((e) => {
|
|
145
|
-
let key = Object.keys(e).pop();
|
|
146
|
-
let subBlock = element.blocks.filter((e) => e.uid === key).pop();
|
|
147
|
-
findEntryIdsFromJsonRte(e[key], subBlock.schema);
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
break;
|
|
152
|
-
}
|
|
153
|
-
case 'global_field':
|
|
154
|
-
case 'group': {
|
|
155
|
-
if (entry[element.uid]) {
|
|
156
|
-
if (element.multiple) {
|
|
157
|
-
entry[element.uid].forEach((e) => {
|
|
158
|
-
findEntryIdsFromJsonRte(e, element.schema);
|
|
159
|
-
});
|
|
160
|
-
} else {
|
|
161
|
-
findEntryIdsFromJsonRte(entry[element.uid], element.schema);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
break;
|
|
165
|
-
}
|
|
166
|
-
case 'json': {
|
|
167
|
-
if (entry[element.uid] && element.field_metadata.rich_text_type) {
|
|
168
|
-
if (element.multiple) {
|
|
169
|
-
entry[element.uid].forEach((jsonRteData) => {
|
|
170
|
-
gatherJsonRteEntryIds(jsonRteData);
|
|
171
|
-
});
|
|
172
|
-
} else {
|
|
173
|
-
gatherJsonRteEntryIds(entry[element.uid]);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
break;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
find(data.content_type.schema, data.entry);
|
|
183
|
-
if (isNewRefFields) {
|
|
184
|
-
findUidsInNewRefFields(data.entry, uids);
|
|
185
|
-
}
|
|
186
|
-
uids = _.flattenDeep(uids);
|
|
187
|
-
// if no references are found, return
|
|
188
|
-
if (uids.length === 0) {
|
|
189
|
-
return data.entry;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
uids = _.uniq(uids);
|
|
193
|
-
let entry = JSON.stringify(data.entry);
|
|
194
|
-
uids.forEach(function (uid) {
|
|
195
|
-
if (mappedUids.hasOwnProperty(uid)) {
|
|
196
|
-
entry = entry.replace(new RegExp(uid, 'img'), mappedUids[uid]);
|
|
197
|
-
mapped.push(uid);
|
|
198
|
-
} else {
|
|
199
|
-
unmapped.push(uid);
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
if (unmapped.length > 0) {
|
|
204
|
-
let unmappedUids = helper.readFileSync(path.join(uidMapperPath, 'unmapped-uids.json'));
|
|
205
|
-
unmappedUids = unmappedUids || {};
|
|
206
|
-
if (unmappedUids.hasOwnProperty(data.content_type.uid)) {
|
|
207
|
-
unmappedUids[data.content_type.uid][data.entry.uid] = unmapped;
|
|
208
|
-
} else {
|
|
209
|
-
unmappedUids[data.content_type.uid] = {
|
|
210
|
-
[data.entry.uid]: unmapped,
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
// write the unmapped contents to ./mapper/language/unmapped-uids.json
|
|
214
|
-
helper.writeFile(path.join(uidMapperPath, 'unmapped-uids.json'), unmappedUids);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (mapped.length > 0) {
|
|
218
|
-
let _mappedUids = helper.readFileSync(path.join(uidMapperPath, 'mapped-uids.json'));
|
|
219
|
-
_mappedUids = _mappedUids || {};
|
|
220
|
-
if (_mappedUids.hasOwnProperty(data.content_type.uid)) {
|
|
221
|
-
_mappedUids[data.content_type.uid][data.entry.uid] = mapped;
|
|
222
|
-
} else {
|
|
223
|
-
_mappedUids[data.content_type.uid] = {
|
|
224
|
-
[data.entry.uid]: mapped,
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
// write the mapped contents to ./mapper/language/mapped-uids.json
|
|
228
|
-
helper.writeFile(path.join(uidMapperPath, 'mapped-uids.json'), _mappedUids);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return JSON.parse(entry);
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
function findUidsInNewRefFields(entry, uids) {
|
|
235
|
-
if (entry && typeof entry === 'object') {
|
|
236
|
-
if (entry.uid && entry._content_type_uid) {
|
|
237
|
-
uids.push(entry.uid);
|
|
238
|
-
} else if (Array.isArray(entry) && entry.length) {
|
|
239
|
-
entry.forEach(function (elem) {
|
|
240
|
-
findUidsInNewRefFields(elem, uids);
|
|
241
|
-
});
|
|
242
|
-
} else if (Object.keys(entry).length) {
|
|
243
|
-
for (let key in entry) {
|
|
244
|
-
if (key) {
|
|
245
|
-
findUidsInNewRefFields(entry[key], uids);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
const { formatError } = require('.');
|
|
2
|
-
const { cliux, configHandler } = require('@contentstack/cli-utilities');
|
|
3
|
-
|
|
4
|
-
const getAllStackSpecificApps = (developerHubBaseUrl, httpClient, config) => {
|
|
5
|
-
return httpClient
|
|
6
|
-
.get(`${developerHubBaseUrl}/installations?target_uids=${config.target_stack}`)
|
|
7
|
-
.then(({ data }) => data.data)
|
|
8
|
-
.catch((error) => log(config, `Failed to export marketplace-apps ${formatError(error)}`, 'error'));
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
const getDeveloperHubUrl = async (config) => {
|
|
12
|
-
const { cma, name } = configHandler.get('region') || {};
|
|
13
|
-
let developerHubBaseUrl = config.developerHubUrls[cma];
|
|
14
|
-
|
|
15
|
-
if (!developerHubBaseUrl) {
|
|
16
|
-
developerHubBaseUrl = await cliux.inquire({
|
|
17
|
-
type: 'input',
|
|
18
|
-
name: 'name',
|
|
19
|
-
validate: (url) => {
|
|
20
|
-
if (!url) return "Developer-hub URL can't be empty.";
|
|
21
|
-
|
|
22
|
-
return true;
|
|
23
|
-
},
|
|
24
|
-
message: `Enter the developer-hub base URL for the ${name} region -`,
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return developerHubBaseUrl.startsWith('http') ? developerHubBaseUrl : `https://${developerHubBaseUrl}`;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
module.exports = { getAllStackSpecificApps, getDeveloperHubUrl };
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
const _ = require('lodash');
|
|
2
|
-
|
|
3
|
-
/* eslint-disable no-empty */
|
|
4
|
-
const removeReferenceFields = (module.exports = async function (schema, flag, stackAPIClient) {
|
|
5
|
-
for (let i = 0; i < schema.length; i++) {
|
|
6
|
-
if (schema[i].data_type === 'group') {
|
|
7
|
-
removeReferenceFields(schema[i].schema, flag);
|
|
8
|
-
} else if (schema[i].data_type === 'blocks') {
|
|
9
|
-
for (var block in schema[i].blocks) {
|
|
10
|
-
removeReferenceFields(schema[i].blocks[block].schema, flag);
|
|
11
|
-
}
|
|
12
|
-
} else if (schema[i].data_type === 'reference' || schema[i].reference_to ) {
|
|
13
|
-
flag.supressed = true;
|
|
14
|
-
// Check if content-type exists
|
|
15
|
-
// If exists, then no change should be required.
|
|
16
|
-
let isContentTypeError = false;
|
|
17
|
-
for (let j = 0; j < schema[i].reference_to.length; j++) {
|
|
18
|
-
try {
|
|
19
|
-
await stackAPIClient.contentType(schema[i].reference_to[j]).fetch();
|
|
20
|
-
} catch (error) {
|
|
21
|
-
// Else warn and modify the schema object.
|
|
22
|
-
isContentTypeError = true;
|
|
23
|
-
console.warn(`Content-type ${schema[i].reference_to[j]} does not exist. Removing the field from schema`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (isContentTypeError) {
|
|
27
|
-
schema.splice(i, 1);
|
|
28
|
-
--i;
|
|
29
|
-
if (schema.length < 1) {
|
|
30
|
-
schema.push({
|
|
31
|
-
data_type: 'text',
|
|
32
|
-
display_name: 'dummyTest',
|
|
33
|
-
uid: 'dummy_test',
|
|
34
|
-
field_metadata: {
|
|
35
|
-
description: '',
|
|
36
|
-
default_value: '',
|
|
37
|
-
version: 3,
|
|
38
|
-
},
|
|
39
|
-
format: '',
|
|
40
|
-
error_messages: {
|
|
41
|
-
format: '',
|
|
42
|
-
},
|
|
43
|
-
multiple: false,
|
|
44
|
-
mandatory: false,
|
|
45
|
-
unique: false,
|
|
46
|
-
non_localizable: false,
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
} else if( // handling entry references in json rte
|
|
51
|
-
schema[i].data_type === 'json'
|
|
52
|
-
&& schema[i].field_metadata.rich_text_type
|
|
53
|
-
&& schema[i].field_metadata.embed_entry
|
|
54
|
-
&& schema[i].reference_to.length > 1) {
|
|
55
|
-
flag.supressed = true;
|
|
56
|
-
schema[i].reference_to = ["sys_assets"]
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
});
|