@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.
Files changed (110) hide show
  1. package/README.md +6 -14
  2. package/bin/dev +17 -0
  3. package/bin/dev.cmd +3 -0
  4. package/bin/run +6 -0
  5. package/bin/run.cmd +3 -0
  6. package/lib/commands/cm/stacks/import.d.ts +10 -0
  7. package/lib/commands/cm/stacks/import.js +111 -0
  8. package/lib/config/index.d.ts +3 -0
  9. package/lib/config/index.js +372 -0
  10. package/lib/import/index.d.ts +1 -0
  11. package/lib/import/index.js +8 -0
  12. package/lib/import/module-importer.d.ts +13 -0
  13. package/lib/import/module-importer.js +70 -0
  14. package/lib/import/modules/assets.d.ts +63 -0
  15. package/lib/import/modules/assets.js +265 -0
  16. package/lib/import/modules/base-class.d.ts +69 -0
  17. package/lib/import/modules/base-class.js +165 -0
  18. package/lib/import/modules/index.d.ts +2 -0
  19. package/lib/import/modules/index.js +19 -0
  20. package/lib/import/modules/locales.d.ts +31 -0
  21. package/lib/import/modules/locales.js +152 -0
  22. package/lib/import/modules-js/assets.d.ts +33 -0
  23. package/lib/import/modules-js/assets.js +415 -0
  24. package/lib/import/modules-js/content-types.d.ts +33 -0
  25. package/lib/import/modules-js/content-types.js +176 -0
  26. package/lib/import/modules-js/custom-roles.d.ts +15 -0
  27. package/lib/import/modules-js/custom-roles.js +141 -0
  28. package/lib/import/modules-js/entries.d.ts +54 -0
  29. package/lib/import/modules-js/entries.js +1260 -0
  30. package/lib/import/modules-js/environments.d.ts +13 -0
  31. package/lib/import/modules-js/environments.js +85 -0
  32. package/lib/import/modules-js/extensions.d.ts +17 -0
  33. package/lib/import/modules-js/extensions.js +86 -0
  34. package/lib/import/modules-js/global-fields.d.ts +13 -0
  35. package/lib/import/modules-js/global-fields.js +109 -0
  36. package/lib/import/modules-js/index.d.ts +1 -0
  37. package/lib/import/modules-js/index.js +33 -0
  38. package/lib/import/modules-js/labels.d.ts +20 -0
  39. package/lib/import/modules-js/labels.js +148 -0
  40. package/lib/import/modules-js/locales.d.ts +24 -0
  41. package/lib/import/modules-js/locales.js +196 -0
  42. package/lib/import/modules-js/marketplace-apps.d.ts +60 -0
  43. package/lib/import/modules-js/marketplace-apps.js +409 -0
  44. package/lib/import/modules-js/webhooks.d.ts +17 -0
  45. package/lib/import/modules-js/webhooks.js +85 -0
  46. package/lib/import/modules-js/workflows.d.ts +18 -0
  47. package/lib/import/modules-js/workflows.js +132 -0
  48. package/lib/types/default-config.d.ts +130 -0
  49. package/lib/types/default-config.js +2 -0
  50. package/lib/types/import-config.d.ts +51 -0
  51. package/lib/types/import-config.js +2 -0
  52. package/lib/types/index.d.ts +30 -0
  53. package/lib/types/index.js +2 -0
  54. package/lib/utils/asset-helper.d.ts +4 -0
  55. package/lib/utils/asset-helper.js +387 -0
  56. package/lib/utils/backup-handler.d.ts +2 -0
  57. package/lib/utils/backup-handler.js +31 -0
  58. package/lib/utils/common-helper.d.ts +20 -0
  59. package/lib/utils/common-helper.js +244 -0
  60. package/lib/utils/content-type-helper.d.ts +49 -0
  61. package/lib/utils/content-type-helper.js +143 -0
  62. package/lib/utils/entries-helper.d.ts +4 -0
  63. package/lib/utils/entries-helper.js +252 -0
  64. package/lib/utils/extension-helper.d.ts +10 -0
  65. package/lib/utils/extension-helper.js +72 -0
  66. package/lib/utils/file-helper.d.ts +14 -0
  67. package/lib/utils/file-helper.js +140 -0
  68. package/lib/utils/import-config-handler.d.ts +3 -0
  69. package/lib/utils/import-config-handler.js +73 -0
  70. package/lib/utils/index.d.ts +12 -0
  71. package/lib/utils/index.js +29 -0
  72. package/lib/utils/interactive.d.ts +2 -0
  73. package/lib/utils/interactive.js +23 -0
  74. package/lib/utils/logger.d.ts +8 -0
  75. package/lib/utils/logger.js +154 -0
  76. package/lib/utils/login-handler.d.ts +8 -0
  77. package/lib/utils/login-handler.js +53 -0
  78. package/lib/utils/marketplace-app-helper.d.ts +4 -0
  79. package/lib/utils/marketplace-app-helper.js +31 -0
  80. package/messages/index.json +1 -7
  81. package/oclif.manifest.json +2 -2
  82. package/package.json +47 -21
  83. package/src/app.js +0 -217
  84. package/src/commands/cm/stacks/import.js +0 -161
  85. package/src/config/default.js +0 -352
  86. package/src/lib/import/assets.js +0 -495
  87. package/src/lib/import/content-types.js +0 -201
  88. package/src/lib/import/custom-roles.js +0 -169
  89. package/src/lib/import/entries.js +0 -1480
  90. package/src/lib/import/environments.js +0 -106
  91. package/src/lib/import/extensions.js +0 -108
  92. package/src/lib/import/global-fields.js +0 -135
  93. package/src/lib/import/labels.js +0 -175
  94. package/src/lib/import/locales.js +0 -216
  95. package/src/lib/import/marketplace-apps.js +0 -542
  96. package/src/lib/import/webhooks.js +0 -113
  97. package/src/lib/import/workflows.js +0 -166
  98. package/src/lib/util/extensionsUidReplace.js +0 -67
  99. package/src/lib/util/fs.js +0 -124
  100. package/src/lib/util/import-flags.js +0 -187
  101. package/src/lib/util/index.js +0 -222
  102. package/src/lib/util/log.js +0 -144
  103. package/src/lib/util/login.js +0 -58
  104. package/src/lib/util/lookupReplaceAssets.js +0 -366
  105. package/src/lib/util/lookupReplaceEntries.js +0 -250
  106. package/src/lib/util/marketplace-app-helper.js +0 -31
  107. package/src/lib/util/removeReferenceFields.js +0 -59
  108. package/src/lib/util/schemaTemplate.js +0 -38
  109. package/src/lib/util/supress-mandatory-fields.js +0 -34
  110. package/src/lib/util/upload.js +0 -56
@@ -0,0 +1,252 @@
1
+ "use strict";
2
+ /**
3
+ * Entries lookup
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.lookupEntries = void 0;
7
+ const tslib_1 = require("tslib");
8
+ const path = tslib_1.__importStar(require("path"));
9
+ const _ = tslib_1.__importStar(require("lodash"));
10
+ const config_1 = tslib_1.__importDefault(require("../config"));
11
+ const fileHelper = tslib_1.__importStar(require("./file-helper"));
12
+ // update references in entry object
13
+ const lookupEntries = function (data, mappedUids, uidMapperPath) {
14
+ let parent = [];
15
+ let uids = [];
16
+ let unmapped = [];
17
+ let mapped = [];
18
+ let isNewRefFields = false;
19
+ let preserveStackVersion = config_1.default.preserveStackVersion;
20
+ function gatherJsonRteEntryIds(jsonRteData) {
21
+ jsonRteData.children.forEach((element) => {
22
+ if (element.type) {
23
+ switch (element.type) {
24
+ case 'a':
25
+ case 'p': {
26
+ if (element.children && element.children.length > 0) {
27
+ gatherJsonRteEntryIds(element);
28
+ }
29
+ break;
30
+ }
31
+ case 'reference': {
32
+ if (Object.keys(element.attrs).length > 0 && element.attrs.type === 'entry') {
33
+ if (uids.indexOf(element.attrs['entry-uid']) === -1) {
34
+ uids.push(element.attrs['entry-uid']);
35
+ }
36
+ }
37
+ if (element.children && element.children.length > 0) {
38
+ gatherJsonRteEntryIds(element);
39
+ }
40
+ break;
41
+ }
42
+ }
43
+ }
44
+ });
45
+ }
46
+ const update = function (_parent, form_id, updateEntry) {
47
+ let _entry = updateEntry;
48
+ let len = _parent.length;
49
+ for (let j = 0; j < len; j++) {
50
+ if (_entry && _parent[j]) {
51
+ if (j === len - 1 && _entry[_parent[j]]) {
52
+ if (form_id !== '_assets') {
53
+ if (_entry[_parent[j]].length) {
54
+ _entry[_parent[j]].forEach((item, idx) => {
55
+ if (typeof item.uid === 'string' && item._content_type_uid) {
56
+ uids.push(item.uid);
57
+ }
58
+ else if (typeof item === 'string' && preserveStackVersion === true) {
59
+ uids.push(item);
60
+ }
61
+ else {
62
+ uids.push(item);
63
+ _entry[_parent[j]][idx] = {
64
+ uid: item,
65
+ _content_type_uid: form_id,
66
+ };
67
+ }
68
+ });
69
+ }
70
+ }
71
+ else if (Array.isArray(_entry[_parent[j]])) {
72
+ for (const element of _entry[_parent[j]]) {
73
+ if (element.uid.length) {
74
+ uids.push(element.uid);
75
+ }
76
+ }
77
+ }
78
+ else if (_entry[_parent[j]].uid.length) {
79
+ uids.push(_entry[_parent[j]].uid);
80
+ }
81
+ }
82
+ else {
83
+ _entry = _entry[_parent[j]];
84
+ let _keys = _.clone(_parent).splice(j + 1, len);
85
+ if (Array.isArray(_entry)) {
86
+ for (let i = 0, _i = _entry.length; i < _i; i++) {
87
+ update(_keys, form_id, _entry[i]);
88
+ }
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
+ }
109
+ else {
110
+ parent.push(schema[i].uid);
111
+ update(parent, schema[i].reference_to, _entry);
112
+ parent.pop();
113
+ }
114
+ break;
115
+ case 'global_field':
116
+ case 'group':
117
+ parent.push(schema[i].uid);
118
+ find(schema[i].schema, _entry);
119
+ parent.pop();
120
+ break;
121
+ case 'blocks':
122
+ for (let j = 0, _j = schema[i].blocks.length; j < _j; j++) {
123
+ parent.push(schema[i].uid);
124
+ parent.push(schema[i].blocks[j].uid);
125
+ find(schema[i].blocks[j].schema, _entry);
126
+ parent.pop();
127
+ parent.pop();
128
+ }
129
+ break;
130
+ case 'json':
131
+ if (schema[i].field_metadata.rich_text_type) {
132
+ findEntryIdsFromJsonRte(data.entry, data.content_type.schema);
133
+ }
134
+ break;
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
+ }
161
+ else {
162
+ findEntryIdsFromJsonRte(entry[element.uid], element.schema);
163
+ }
164
+ }
165
+ break;
166
+ }
167
+ case 'json': {
168
+ if (entry[element.uid] && element.field_metadata.rich_text_type) {
169
+ if (element.multiple) {
170
+ entry[element.uid].forEach((jsonRteData) => {
171
+ gatherJsonRteEntryIds(jsonRteData);
172
+ });
173
+ }
174
+ else {
175
+ gatherJsonRteEntryIds(entry[element.uid]);
176
+ }
177
+ }
178
+ break;
179
+ }
180
+ }
181
+ }
182
+ }
183
+ find(data.content_type.schema, data.entry);
184
+ if (isNewRefFields) {
185
+ findUidsInNewRefFields(data.entry, uids);
186
+ }
187
+ uids = _.flattenDeep(uids);
188
+ // if no references are found, return
189
+ if (uids.length === 0) {
190
+ return data.entry;
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
+ }
199
+ else {
200
+ unmapped.push(uid);
201
+ }
202
+ });
203
+ if (unmapped.length > 0) {
204
+ let unmappedUids = fileHelper.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
+ }
209
+ else {
210
+ unmappedUids[data.content_type.uid] = {
211
+ [data.entry.uid]: unmapped,
212
+ };
213
+ }
214
+ // write the unmapped contents to ./mapper/language/unmapped-uids.json
215
+ fileHelper.writeFile(path.join(uidMapperPath, 'unmapped-uids.json'), unmappedUids);
216
+ }
217
+ if (mapped.length > 0) {
218
+ let _mappedUids = fileHelper.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
+ }
223
+ else {
224
+ _mappedUids[data.content_type.uid] = {
225
+ [data.entry.uid]: mapped,
226
+ };
227
+ }
228
+ // write the mapped contents to ./mapper/language/mapped-uids.json
229
+ fileHelper.writeFile(path.join(uidMapperPath, 'mapped-uids.json'), _mappedUids);
230
+ }
231
+ return JSON.parse(entry);
232
+ };
233
+ exports.lookupEntries = lookupEntries;
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
+ }
239
+ else if (Array.isArray(entry) && entry.length) {
240
+ entry.forEach(function (elem) {
241
+ findUidsInNewRefFields(elem, uids);
242
+ });
243
+ }
244
+ else if (Object.keys(entry).length) {
245
+ for (let key in entry) {
246
+ if (key) {
247
+ findUidsInNewRefFields(entry[key], uids);
248
+ }
249
+ }
250
+ }
251
+ }
252
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * extension lookup
3
+ */
4
+ /*!
5
+ * Contentstack Import
6
+ * Copyright (c) 2019 Contentstack LLC
7
+ * MIT Licensed
8
+ */
9
+ import { ImportConfig } from '../types';
10
+ export declare const lookupExtension: (config: ImportConfig, schema: any, preserveStackVersion: any, installedExtensions: any) => void;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ /**
3
+ * extension lookup
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.lookupExtension = void 0;
7
+ // eslint-disable-next-line unicorn/filename-case
8
+ let path = require('path');
9
+ const _ = require('lodash');
10
+ let helper = require('./file-helper');
11
+ // eslint-disable-next-line camelcase
12
+ const lookupExtension = function (config, schema, preserveStackVersion, installedExtensions) {
13
+ const extensionPath = path.resolve(config.data, 'mapper/extensions', 'uid-mapping.json');
14
+ const globalfieldsPath = path.resolve(config.data, 'mapper/globalfields', 'uid-mapping.json');
15
+ const marketplaceAppPath = path.resolve(config.data, 'marketplace_apps', 'marketplace_apps.json');
16
+ for (let i in schema) {
17
+ if (schema[i].data_type === 'group') {
18
+ (0, exports.lookupExtension)(config, schema[i].schema, preserveStackVersion, installedExtensions);
19
+ }
20
+ else if (schema[i].data_type === 'blocks') {
21
+ for (let block in schema[i].blocks) {
22
+ if (schema[i].blocks[block].hasOwnProperty('reference_to')) {
23
+ delete schema[i].blocks[block].schema;
24
+ }
25
+ else {
26
+ (0, exports.lookupExtension)(config, schema[i].blocks[block].schema, preserveStackVersion, installedExtensions);
27
+ }
28
+ }
29
+ }
30
+ else if (schema[i].data_type === 'reference' &&
31
+ !schema[i].field_metadata.hasOwnProperty('ref_multiple_content_types')) {
32
+ if (preserveStackVersion) {
33
+ // do nothing
34
+ }
35
+ else {
36
+ schema[i].reference_to = [schema[i].reference_to];
37
+ schema[i].field_metadata.ref_multiple_content_types = true;
38
+ }
39
+ }
40
+ else if (schema[i].data_type === 'global_field') {
41
+ let global_fields_key_value = schema[i].reference_to;
42
+ let global_fields_data = helper.readFileSync(path.join(globalfieldsPath));
43
+ if (global_fields_data && global_fields_data.hasOwnProperty(global_fields_key_value)) {
44
+ schema[i].reference_to = global_fields_data[global_fields_key_value];
45
+ }
46
+ }
47
+ else if (schema[i].hasOwnProperty('extension_uid')) {
48
+ const extension_key_value = schema[i].extension_uid;
49
+ const data = helper.readFileSync(path.join(extensionPath));
50
+ if (data && data.hasOwnProperty(extension_key_value)) {
51
+ // eslint-disable-next-line camelcase
52
+ schema[i].extension_uid = data[extension_key_value];
53
+ }
54
+ else if (schema[i].field_metadata && schema[i].field_metadata.extension) {
55
+ if (installedExtensions && installedExtensions[schema[i].extension_uid]) {
56
+ schema[i].extension_uid = installedExtensions[schema[i].extension_uid];
57
+ }
58
+ }
59
+ }
60
+ else if (schema[i].data_type === 'json' && schema[i].hasOwnProperty('plugins') && schema[i].plugins.length > 0) {
61
+ const newPluginUidsArray = [];
62
+ const data = helper.readFileSync(path.join(extensionPath));
63
+ schema[i].plugins.forEach((extension_key_value) => {
64
+ if (data && data.hasOwnProperty(extension_key_value)) {
65
+ newPluginUidsArray.push(data[extension_key_value]);
66
+ }
67
+ });
68
+ schema[i].plugins = newPluginUidsArray;
69
+ }
70
+ }
71
+ };
72
+ exports.lookupExtension = lookupExtension;
@@ -0,0 +1,14 @@
1
+ import { FsUtility } from '@contentstack/cli-utilities';
2
+ export declare const readFileSync: (filePath: string, parse?: boolean) => any;
3
+ export declare const readFile: (filePath: string, options?: {
4
+ type: string;
5
+ }) => Promise<any>;
6
+ export declare const readLargeFile: (filePath: string, opts?: any) => Promise<any>;
7
+ export declare const writeFileSync: (filePath: string, data: any) => void;
8
+ export declare const writeFile: (filePath: string, data: any) => Promise<any>;
9
+ export declare const writeLargeFile: (filePath: string, data: any) => Promise<any>;
10
+ export declare const makeDirectory: (dir: string) => void;
11
+ export declare const readdirSync: (dirPath: string) => any;
12
+ export declare const isFolderExist: (folderPath: string) => Promise<any>;
13
+ export declare const fileExistsSync: (path: string) => boolean;
14
+ export declare const fsUtil: FsUtility;
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fsUtil = exports.fileExistsSync = exports.isFolderExist = exports.readdirSync = exports.makeDirectory = exports.writeLargeFile = exports.writeFile = exports.writeFileSync = exports.readLargeFile = exports.readFile = exports.readFileSync = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const fs = tslib_1.__importStar(require("fs"));
6
+ const path = tslib_1.__importStar(require("path"));
7
+ const mkdirp_1 = tslib_1.__importDefault(require("mkdirp"));
8
+ const bigJSON = tslib_1.__importStar(require("big-json"));
9
+ const cli_utilities_1 = require("@contentstack/cli-utilities");
10
+ const readFileSync = function (filePath, parse = true) {
11
+ let data;
12
+ filePath = path.resolve(filePath);
13
+ if (fs.existsSync(filePath)) {
14
+ try {
15
+ data = parse ? JSON.parse(fs.readFileSync(filePath, 'utf-8')) : data;
16
+ }
17
+ catch (error) {
18
+ return data;
19
+ }
20
+ }
21
+ return data;
22
+ };
23
+ exports.readFileSync = readFileSync;
24
+ // by default file type is json
25
+ const readFile = async (filePath, options = { type: 'json' }) => {
26
+ return new Promise((resolve, reject) => {
27
+ filePath = path.resolve(filePath);
28
+ fs.readFile(filePath, 'utf-8', (error, data) => {
29
+ if (error) {
30
+ if (error.code === 'ENOENT') {
31
+ return resolve('');
32
+ }
33
+ reject(error);
34
+ }
35
+ else {
36
+ if (options.type !== 'json') {
37
+ return resolve(data);
38
+ }
39
+ resolve(JSON.parse(data));
40
+ }
41
+ });
42
+ });
43
+ };
44
+ exports.readFile = readFile;
45
+ const readLargeFile = function (filePath, opts) {
46
+ if (typeof filePath !== 'string') {
47
+ return;
48
+ }
49
+ filePath = path.resolve(filePath);
50
+ if (fs.existsSync(filePath)) {
51
+ return new Promise((resolve, reject) => {
52
+ const readStream = fs.createReadStream(filePath, { encoding: 'utf-8' });
53
+ const parseStream = bigJSON.createParseStream();
54
+ parseStream.on('data', function (data) {
55
+ if ((opts === null || opts === void 0 ? void 0 : opts.type) === 'array') {
56
+ return resolve(Object.values(data));
57
+ }
58
+ resolve(data);
59
+ });
60
+ parseStream.on('error', function (error) {
61
+ console.log('error', error);
62
+ reject(error);
63
+ });
64
+ readStream.pipe(parseStream);
65
+ });
66
+ }
67
+ };
68
+ exports.readLargeFile = readLargeFile;
69
+ const writeFileSync = function (filePath, data) {
70
+ data = typeof data === 'object' ? JSON.stringify(data) : data || '{}';
71
+ fs.writeFileSync(filePath, data);
72
+ };
73
+ exports.writeFileSync = writeFileSync;
74
+ const writeFile = function (filePath, data) {
75
+ return new Promise((resolve, reject) => {
76
+ data = typeof data === 'object' ? JSON.stringify(data) : data || '{}';
77
+ fs.writeFile(filePath, data, (error) => {
78
+ if (error) {
79
+ return reject(error);
80
+ }
81
+ resolve('done');
82
+ });
83
+ });
84
+ };
85
+ exports.writeFile = writeFile;
86
+ const writeLargeFile = function (filePath, data) {
87
+ if (typeof filePath !== 'string' || typeof data !== 'object') {
88
+ return;
89
+ }
90
+ filePath = path.resolve(filePath);
91
+ return new Promise((resolve, reject) => {
92
+ const stringifyStream = bigJSON.createStringifyStream({
93
+ body: data,
94
+ });
95
+ var writeStream = fs.createWriteStream(filePath, 'utf-8');
96
+ stringifyStream.pipe(writeStream);
97
+ writeStream.on('finish', () => {
98
+ resolve('');
99
+ });
100
+ writeStream.on('error', (error) => {
101
+ reject(error);
102
+ });
103
+ });
104
+ };
105
+ exports.writeLargeFile = writeLargeFile;
106
+ const makeDirectory = function (dir) {
107
+ for (let key in arguments) {
108
+ const dirname = path.resolve(arguments[key]);
109
+ if (!fs.existsSync(dirname)) {
110
+ mkdirp_1.default.sync(dirname);
111
+ }
112
+ }
113
+ };
114
+ exports.makeDirectory = makeDirectory;
115
+ const readdirSync = function (dirPath) {
116
+ if (fs.existsSync(dirPath)) {
117
+ return fs.readdirSync(dirPath);
118
+ }
119
+ else {
120
+ return [];
121
+ }
122
+ };
123
+ exports.readdirSync = readdirSync;
124
+ const isFolderExist = async (folderPath) => {
125
+ return new Promise((resolve, reject) => {
126
+ folderPath = path.resolve(folderPath);
127
+ fs.access(folderPath, (error) => {
128
+ if (error) {
129
+ return resolve(false);
130
+ }
131
+ resolve(true);
132
+ });
133
+ });
134
+ };
135
+ exports.isFolderExist = isFolderExist;
136
+ const fileExistsSync = function (path) {
137
+ return fs.existsSync(path);
138
+ };
139
+ exports.fileExistsSync = fileExistsSync;
140
+ exports.fsUtil = new cli_utilities_1.FsUtility();
@@ -0,0 +1,3 @@
1
+ import { ImportConfig } from '../types';
2
+ declare const setupConfig: (importCmdFlags: any) => Promise<ImportConfig>;
3
+ export default setupConfig;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const merge_1 = tslib_1.__importDefault(require("merge"));
5
+ const path = tslib_1.__importStar(require("path"));
6
+ const lodash_1 = require("lodash");
7
+ const cli_utilities_1 = require("@contentstack/cli-utilities");
8
+ const config_1 = tslib_1.__importDefault(require("../config"));
9
+ const file_helper_1 = require("./file-helper");
10
+ const interactive_1 = require("./interactive");
11
+ const login_handler_1 = tslib_1.__importDefault(require("./login-handler"));
12
+ const setupConfig = async (importCmdFlags) => {
13
+ let config = (0, merge_1.default)({}, config_1.default);
14
+ // setup the config
15
+ if (importCmdFlags['config']) {
16
+ let externalConfig = await (0, file_helper_1.readFile)(importCmdFlags['config']);
17
+ if ((0, lodash_1.isArray)(externalConfig['modules'])) {
18
+ config.modules.types = (0, lodash_1.filter)(config.modules.types, (module) => (0, lodash_1.includes)(externalConfig['modules'], module));
19
+ externalConfig = (0, lodash_1.omit)(externalConfig, ['modules']);
20
+ }
21
+ config = merge_1.default.recursive(config, externalConfig);
22
+ }
23
+ config.contentDir = importCmdFlags['data'] || importCmdFlags['data-dir'] || config.data || (await (0, interactive_1.askContentDir)());
24
+ config.contentDir = path.resolve(config.contentDir);
25
+ //Note to support the old key
26
+ config.data = config.contentDir;
27
+ const managementTokenAlias = importCmdFlags['management-token-alias'] || importCmdFlags['alias'];
28
+ if (managementTokenAlias) {
29
+ const { token, apiKey } = cli_utilities_1.configHandler.get(`tokens.${managementTokenAlias}`);
30
+ config.management_token = token;
31
+ config.apiKey = apiKey;
32
+ if (!config.management_token) {
33
+ throw new Error(`No management token found on given alias ${managementTokenAlias}`);
34
+ }
35
+ }
36
+ if (!config.management_token) {
37
+ if (!(0, cli_utilities_1.isAuthenticated)()) {
38
+ if (config.email && config.password) {
39
+ await (0, login_handler_1.default)(config);
40
+ }
41
+ else {
42
+ throw new Error('Please login or provide an alias for the management token');
43
+ }
44
+ }
45
+ else {
46
+ config.apiKey =
47
+ importCmdFlags['stack-uid'] || importCmdFlags['stack-api-key'] || config.target_stack || (await (0, interactive_1.askAPIKey)());
48
+ if (typeof config.apiKey !== 'string') {
49
+ throw new Error('Invalid API key received');
50
+ }
51
+ }
52
+ }
53
+ config.isAuthenticated = (0, cli_utilities_1.isAuthenticated)();
54
+ //Note to support the old key
55
+ config.source_stack = config.apiKey;
56
+ config.importWebhookStatus = importCmdFlags.importWebhookStatus;
57
+ config.forceStopMarketplaceAppsPrompt = importCmdFlags.yes;
58
+ if (importCmdFlags['branch']) {
59
+ config.branchName = importCmdFlags['branch'];
60
+ config.branchDir = path.join(config.contentDir, config.branchName);
61
+ }
62
+ if (importCmdFlags['module']) {
63
+ config.moduleName = importCmdFlags['module'];
64
+ config.singleModuleImport = true;
65
+ }
66
+ if (importCmdFlags['backup-dir']) {
67
+ config.useBackedupDir = importCmdFlags['backup-dir'];
68
+ }
69
+ // Note to support old modules
70
+ config.target_stack = config.apiKey;
71
+ return config;
72
+ };
73
+ exports.default = setupConfig;
@@ -0,0 +1,12 @@
1
+ export * as interactive from './interactive';
2
+ export { default as setupImportConfig } from './import-config-handler';
3
+ export * as fileHelper from './file-helper';
4
+ export { fsUtil } from './file-helper';
5
+ export { default as backupHandler } from './backup-handler';
6
+ export { log, unlinkFileLogger } from './logger';
7
+ export { uploadAssetHelper, lookupAssets } from './asset-helper';
8
+ export { getDeveloperHubUrl } from './marketplace-app-helper';
9
+ export { schemaTemplate, suppressSchemaReference, removeReferenceFields } from './content-type-helper';
10
+ export { lookupExtension } from './extension-helper';
11
+ export { lookupEntries } from './entries-helper';
12
+ export * from './common-helper';
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.lookupEntries = exports.lookupExtension = exports.removeReferenceFields = exports.suppressSchemaReference = exports.schemaTemplate = exports.getDeveloperHubUrl = exports.lookupAssets = exports.uploadAssetHelper = exports.unlinkFileLogger = exports.log = exports.backupHandler = exports.fsUtil = exports.fileHelper = exports.setupImportConfig = exports.interactive = void 0;
4
+ const tslib_1 = require("tslib");
5
+ exports.interactive = tslib_1.__importStar(require("./interactive"));
6
+ var import_config_handler_1 = require("./import-config-handler");
7
+ Object.defineProperty(exports, "setupImportConfig", { enumerable: true, get: function () { return tslib_1.__importDefault(import_config_handler_1).default; } });
8
+ exports.fileHelper = tslib_1.__importStar(require("./file-helper"));
9
+ var file_helper_1 = require("./file-helper");
10
+ Object.defineProperty(exports, "fsUtil", { enumerable: true, get: function () { return file_helper_1.fsUtil; } });
11
+ var backup_handler_1 = require("./backup-handler");
12
+ Object.defineProperty(exports, "backupHandler", { enumerable: true, get: function () { return tslib_1.__importDefault(backup_handler_1).default; } });
13
+ var logger_1 = require("./logger");
14
+ Object.defineProperty(exports, "log", { enumerable: true, get: function () { return logger_1.log; } });
15
+ Object.defineProperty(exports, "unlinkFileLogger", { enumerable: true, get: function () { return logger_1.unlinkFileLogger; } });
16
+ var asset_helper_1 = require("./asset-helper");
17
+ Object.defineProperty(exports, "uploadAssetHelper", { enumerable: true, get: function () { return asset_helper_1.uploadAssetHelper; } });
18
+ Object.defineProperty(exports, "lookupAssets", { enumerable: true, get: function () { return asset_helper_1.lookupAssets; } });
19
+ var marketplace_app_helper_1 = require("./marketplace-app-helper");
20
+ Object.defineProperty(exports, "getDeveloperHubUrl", { enumerable: true, get: function () { return marketplace_app_helper_1.getDeveloperHubUrl; } });
21
+ var content_type_helper_1 = require("./content-type-helper");
22
+ Object.defineProperty(exports, "schemaTemplate", { enumerable: true, get: function () { return content_type_helper_1.schemaTemplate; } });
23
+ Object.defineProperty(exports, "suppressSchemaReference", { enumerable: true, get: function () { return content_type_helper_1.suppressSchemaReference; } });
24
+ Object.defineProperty(exports, "removeReferenceFields", { enumerable: true, get: function () { return content_type_helper_1.removeReferenceFields; } });
25
+ var extension_helper_1 = require("./extension-helper");
26
+ Object.defineProperty(exports, "lookupExtension", { enumerable: true, get: function () { return extension_helper_1.lookupExtension; } });
27
+ var entries_helper_1 = require("./entries-helper");
28
+ Object.defineProperty(exports, "lookupEntries", { enumerable: true, get: function () { return entries_helper_1.lookupEntries; } });
29
+ tslib_1.__exportStar(require("./common-helper"), exports);
@@ -0,0 +1,2 @@
1
+ export declare const askContentDir: () => Promise<string>;
2
+ export declare const askAPIKey: () => Promise<string>;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.askAPIKey = exports.askContentDir = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const cli_utilities_1 = require("@contentstack/cli-utilities");
6
+ const path = tslib_1.__importStar(require("path"));
7
+ const askContentDir = async () => {
8
+ const result = await cli_utilities_1.cliux.inquire({
9
+ type: 'input',
10
+ message: 'Enter the path for the content',
11
+ name: 'dir',
12
+ });
13
+ return path.resolve(result);
14
+ };
15
+ exports.askContentDir = askContentDir;
16
+ const askAPIKey = async () => {
17
+ return cli_utilities_1.cliux.inquire({
18
+ type: 'input',
19
+ message: 'Enter the stack api key',
20
+ name: 'apiKey',
21
+ });
22
+ };
23
+ exports.askAPIKey = askAPIKey;
@@ -0,0 +1,8 @@
1
+ /*!
2
+ * Contentstack Export
3
+ * Copyright (c) 2019 Contentstack LLC
4
+ * MIT Licensed
5
+ */
6
+ import { ImportConfig } from '../types';
7
+ export declare const log: (config: ImportConfig, message: any, type: string) => Promise<void>;
8
+ export declare const unlinkFileLogger: () => void;