@contentstack/cli-cm-export 1.5.9 → 1.7.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 (113) hide show
  1. package/README.md +2 -2
  2. package/lib/commands/cm/stacks/export.d.ts +10 -0
  3. package/lib/commands/cm/stacks/export.js +108 -0
  4. package/lib/config/index.d.ts +3 -0
  5. package/lib/config/index.js +403 -0
  6. package/lib/export/index.d.ts +1 -0
  7. package/lib/export/index.js +8 -0
  8. package/lib/export/module-exporter.d.ts +15 -0
  9. package/lib/export/module-exporter.js +93 -0
  10. package/lib/export/modules/assets.d.ts +54 -0
  11. package/lib/export/modules/assets.js +303 -0
  12. package/lib/export/modules/base-class.d.ts +48 -0
  13. package/lib/export/modules/base-class.js +117 -0
  14. package/lib/export/modules/content-types.d.ts +15 -0
  15. package/lib/export/modules/content-types.js +72 -0
  16. package/lib/export/modules/custom-roles.d.ts +16 -0
  17. package/lib/export/modules/custom-roles.js +81 -0
  18. package/lib/export/modules/environments.d.ts +12 -0
  19. package/lib/export/modules/environments.js +64 -0
  20. package/lib/export/modules/extensions.d.ts +12 -0
  21. package/lib/export/modules/extensions.js +64 -0
  22. package/lib/export/modules/global-fields.d.ts +14 -0
  23. package/lib/export/modules/global-fields.js +59 -0
  24. package/lib/export/modules/index.d.ts +3 -0
  25. package/lib/export/modules/index.js +38 -0
  26. package/lib/export/modules/labels.d.ts +12 -0
  27. package/lib/export/modules/labels.js +64 -0
  28. package/lib/export/modules/locales.d.ts +16 -0
  29. package/lib/export/modules/locales.js +68 -0
  30. package/lib/export/modules/marketplace-apps.d.ts +18 -0
  31. package/lib/export/modules/marketplace-apps.js +119 -0
  32. package/lib/export/modules/webhooks.d.ts +12 -0
  33. package/lib/export/modules/webhooks.js +63 -0
  34. package/lib/export/modules-js/assets.d.ts +43 -0
  35. package/lib/export/modules-js/assets.js +391 -0
  36. package/lib/export/modules-js/content-types.d.ts +21 -0
  37. package/lib/export/modules-js/content-types.js +75 -0
  38. package/lib/export/modules-js/custom-roles.d.ts +21 -0
  39. package/lib/export/modules-js/custom-roles.js +76 -0
  40. package/lib/export/modules-js/entries.d.ts +18 -0
  41. package/lib/export/modules-js/entries.js +141 -0
  42. package/lib/export/modules-js/environments.d.ts +16 -0
  43. package/lib/export/modules-js/environments.js +62 -0
  44. package/lib/export/modules-js/extensions.d.ts +18 -0
  45. package/lib/export/modules-js/extensions.js +57 -0
  46. package/lib/export/modules-js/global-fields.d.ts +22 -0
  47. package/lib/export/modules-js/global-fields.js +107 -0
  48. package/lib/export/modules-js/index.d.ts +2 -0
  49. package/lib/export/modules-js/index.js +31 -0
  50. package/lib/export/modules-js/labels.d.ts +14 -0
  51. package/lib/export/modules-js/labels.js +56 -0
  52. package/lib/export/modules-js/locales.d.ts +23 -0
  53. package/lib/export/modules-js/locales.js +67 -0
  54. package/lib/export/modules-js/marketplace-apps.d.ts +20 -0
  55. package/lib/export/modules-js/marketplace-apps.js +125 -0
  56. package/lib/export/modules-js/stack.d.ts +18 -0
  57. package/lib/export/modules-js/stack.js +91 -0
  58. package/lib/export/modules-js/webhooks.d.ts +18 -0
  59. package/lib/export/modules-js/webhooks.js +60 -0
  60. package/lib/export/modules-js/workflows.d.ts +16 -0
  61. package/lib/export/modules-js/workflows.js +89 -0
  62. package/lib/types/default-config.d.ts +158 -0
  63. package/lib/types/default-config.js +2 -0
  64. package/lib/types/export-config.d.ts +36 -0
  65. package/lib/types/export-config.js +2 -0
  66. package/lib/types/index.d.ts +88 -0
  67. package/lib/types/index.js +2 -0
  68. package/lib/utils/basic-login.d.ts +8 -0
  69. package/lib/utils/basic-login.js +45 -0
  70. package/lib/utils/common-helper.d.ts +11 -0
  71. package/lib/utils/common-helper.js +78 -0
  72. package/lib/utils/export-config-handler.d.ts +3 -0
  73. package/lib/utils/export-config-handler.js +72 -0
  74. package/lib/utils/file-helper.d.ts +14 -0
  75. package/lib/utils/file-helper.js +120 -0
  76. package/lib/utils/index.d.ts +10 -0
  77. package/lib/utils/index.js +21 -0
  78. package/lib/utils/interactive.d.ts +7 -0
  79. package/lib/utils/interactive.js +84 -0
  80. package/lib/utils/logger.d.ts +8 -0
  81. package/lib/utils/logger.js +154 -0
  82. package/lib/utils/marketplace-app-helper.d.ts +11 -0
  83. package/lib/utils/marketplace-app-helper.js +55 -0
  84. package/lib/utils/setup-branches.d.ts +3 -0
  85. package/lib/utils/setup-branches.js +49 -0
  86. package/lib/utils/setup-export-dir.d.ts +2 -0
  87. package/lib/utils/setup-export-dir.js +12 -0
  88. package/messages/index.json +1 -7
  89. package/oclif.manifest.json +1 -1
  90. package/package.json +38 -23
  91. package/src/app.js +0 -156
  92. package/src/commands/cm/stacks/export.js +0 -202
  93. package/src/config/default.js +0 -360
  94. package/src/lib/export/assets.js +0 -454
  95. package/src/lib/export/content-types.js +0 -90
  96. package/src/lib/export/custom-roles.js +0 -92
  97. package/src/lib/export/entries.js +0 -200
  98. package/src/lib/export/environments.js +0 -74
  99. package/src/lib/export/extensions.js +0 -69
  100. package/src/lib/export/global-fields.js +0 -122
  101. package/src/lib/export/labels.js +0 -67
  102. package/src/lib/export/locales.js +0 -72
  103. package/src/lib/export/marketplace-apps.js +0 -187
  104. package/src/lib/export/stack.js +0 -98
  105. package/src/lib/export/webhooks.js +0 -76
  106. package/src/lib/export/workflows.js +0 -106
  107. package/src/lib/util/export-flags.js +0 -193
  108. package/src/lib/util/helper.js +0 -113
  109. package/src/lib/util/index.js +0 -80
  110. package/src/lib/util/log.js +0 -161
  111. package/src/lib/util/login.js +0 -79
  112. package/src/lib/util/marketplace-app-helper.js +0 -24
  113. package/src/lib/util/setup-branches.js +0 -56
@@ -1,454 +0,0 @@
1
- /*!
2
- * Contentstack Export
3
- * Copyright (c) 2019 Contentstack LLC
4
- * MIT Licensed
5
- */
6
-
7
- const mkdirp = require('mkdirp');
8
- const path = require('path');
9
- const fs = require('fs');
10
- const Promise = require('bluebird');
11
- const _ = require('lodash');
12
- const chalk = require('chalk');
13
- const progress = require('progress-stream');
14
- const { HttpClient, configHandler } = require('@contentstack/cli-utilities');
15
-
16
- const helper = require('../util/helper');
17
- const { addlogs } = require('../util/log');
18
-
19
- let config = require('../../config/default');
20
- const { formatError } = require('../util');
21
-
22
- module.exports = class ExportAssets {
23
- config;
24
- bLimit;
25
- vLimit;
26
- invalidKeys;
27
- folderJSONPath;
28
- folderData = [];
29
- assetsFolderPath;
30
- assetContents = {};
31
- httpClient = HttpClient.create();
32
- assetConfig = config.modules.assets;
33
-
34
- constructor(exportConfig, stackAPIClient) {
35
- this.stackAPIClient = stackAPIClient;
36
- this.config = _.merge(config, exportConfig);
37
- this.folderData = [];
38
- this.assetContents = {};
39
- this.assetDownloadRetry = {};
40
- this.assetDownloadRetryLimit = 3;
41
- this.invalidKeys = this.assetConfig.invalidKeys;
42
- this.bLimit = this.assetConfig.batchLimit || 15;
43
- this.vLimit = this.assetConfig.downloadLimit || this.config.fetchConcurrency || 3;
44
- }
45
-
46
- start() {
47
- const self = this;
48
- this.assetsFolderPath = path.resolve(this.config.data, this.config.branchName || '', this.assetConfig.dirName);
49
- this.assetContentsFile = path.resolve(this.assetsFolderPath, 'assets.json');
50
- this.folderJSONPath = path.resolve(this.assetsFolderPath, 'folders.json');
51
-
52
- addlogs(this.config, 'Starting assets export', 'success');
53
-
54
- // Create asset folder
55
- mkdirp.sync(this.assetsFolderPath);
56
-
57
- return new Promise((resolve, reject) => {
58
- // TBD: getting all the assets should have optimized
59
- return self
60
- .getAssetCount()
61
- .then((count) => {
62
- const assetBatches = [];
63
-
64
- if (typeof count !== 'number' || count === 0) {
65
- addlogs(self.config, 'No assets found', 'success');
66
- return resolve();
67
- }
68
- for (let i = 0; i <= count; i += self.bLimit) {
69
- assetBatches.push(i);
70
- }
71
-
72
- return Promise.map(
73
- assetBatches,
74
- (batch) => {
75
- return self
76
- .getAssetJSON(batch)
77
- .then((assetsJSON) => {
78
- return Promise.map(
79
- assetsJSON,
80
- (assetJSON) => {
81
- if (self.assetConfig.downloadVersionAssets) {
82
- return self
83
- .getVersionedAssetJSON(assetJSON.uid, assetJSON._version)
84
- .then(() => {
85
- self.assetContents[assetJSON.uid] = assetJSON;
86
- })
87
- .catch((error) => {
88
- addlogs(
89
- self.config,
90
- `Asset '${assetJSON.uid}' failed to download.\n ${formatError(error)}`,
91
- 'error'
92
- );
93
- addlogs(self.config, error, 'error');
94
- });
95
- } else {
96
- return self
97
- .downloadAsset(assetJSON)
98
- .then(() => {
99
- self.assetContents[assetJSON.uid] = assetJSON;
100
- })
101
- .catch((err) => {
102
- addlogs(
103
- self.config,
104
- `Asset '${assetJSON.uid}' download failed. ${formatError(err)}`,
105
- 'error',
106
- );
107
- return err;
108
- });
109
- }
110
- },
111
- { concurrency: self.vLimit },
112
- )
113
- .then(() => {
114
- addlogs(self.config, 'Batch no ' + (batch + 1) + ' of assets is complete', 'success');
115
- // helper.writeFileSync(this.assetContentsFile, self.assetContents)
116
- })
117
- .catch((error) => {
118
- addlogs(self.config, `Asset batch ${batch + 1} failed to download`, 'error');
119
- addlogs(self.config, formatError(error), 'error');
120
- addlogs(self.config, error, 'error');
121
- });
122
- })
123
- .catch((error) => {
124
- addlogs(self.config, error, 'error');
125
- reject(error);
126
- });
127
- },
128
- { concurrency: self.assetConfig.concurrencyLimit || 1 },
129
- )
130
- .then(() => {
131
- helper.writeFileSync(self.assetContentsFile, self.assetContents);
132
-
133
- return self
134
- .exportFolders()
135
- .then(() => {
136
- addlogs(self.config, chalk.green('Asset export completed successfully'), 'success');
137
- return resolve();
138
- })
139
- .catch((error) => {
140
- addlogs(self.config, error, 'error');
141
- reject(error);
142
- });
143
- })
144
- .catch((error) => {
145
- helper.writeFileSync(self.assetContentsFile, self.assetContents);
146
- addlogs(
147
- self.config,
148
- `Asset export failed. ${formatError(error)}`,
149
- 'error'
150
- );
151
- addlogs(self.config, error, 'error');
152
- reject(error);
153
- });
154
- })
155
- .catch((error) => {
156
- addlogs(self.config, error, 'error');
157
- reject(error);
158
- });
159
- });
160
- }
161
-
162
- exportFolders() {
163
- const self = this;
164
- return new Promise((resolve, reject) => {
165
- return self
166
- .getAssetCount(true)
167
- .then((fCount) => {
168
- if (fCount === 0) {
169
- addlogs(self.config, 'No folders were found in the stack!', 'success');
170
- return resolve();
171
- }
172
-
173
- return self
174
- .getFolderJSON(0, fCount)
175
- .then(() => {
176
- // asset folders have been successfully exported
177
- addlogs(self.config, 'Asset-folders have been successfully exported!', 'success');
178
- return resolve();
179
- })
180
- .catch((error) => {
181
- addlogs(self.config, `Error while exporting asset-folders!\n ${formatError(error)}`, 'error');
182
- return reject(error);
183
- });
184
- })
185
- .catch((error) => {
186
- addlogs(self.config, error, 'error');
187
- // error while fetching asset folder count
188
- return reject(error);
189
- });
190
- });
191
- }
192
-
193
- getFolderJSON(skip, fCount) {
194
- const self = this;
195
- return new Promise((resolve, reject) => {
196
- if (typeof skip !== 'number') {
197
- skip = 0;
198
- }
199
-
200
- if (skip >= fCount) {
201
- helper.writeFileSync(self.folderJSONPath, self.folderData);
202
- return resolve();
203
- }
204
-
205
- const queryRequestObj = {
206
- skip,
207
- include_folders: true,
208
- query: { is_dir: true },
209
- };
210
-
211
- self.stackAPIClient
212
- .asset()
213
- .query(queryRequestObj)
214
- .find()
215
- .then((response) => {
216
- skip += 100;
217
- self.folderData.push(...response.items);
218
- return self.getFolderJSON(skip, fCount).then(resolve).catch(reject);
219
- })
220
- .catch((error) => reject(error));
221
- });
222
- }
223
-
224
- getAssetCount(folder) {
225
- const self = this;
226
- return new Promise((resolve, reject) => {
227
- if (folder && typeof folder === 'boolean') {
228
- const queryOptions = {
229
- skip: 99999990,
230
- include_count: true,
231
- include_folders: true,
232
- query: { is_dir: true },
233
- };
234
- self.stackAPIClient
235
- .asset()
236
- .query(queryOptions)
237
- .find()
238
- .then((asset) => {
239
- return resolve(asset.count);
240
- })
241
- .catch((error) => {
242
- addlogs(self.config, error, 'error');
243
- });
244
- } else {
245
- const queryOptions = { skip: 99999990, include_count: true };
246
- self.stackAPIClient
247
- .asset()
248
- .query(queryOptions)
249
- .find()
250
- .then(({ count }) => resolve(count))
251
- .catch((error) => {
252
- addlogs(self.config, error, 'error');
253
- reject(error);
254
- });
255
- }
256
- });
257
- }
258
-
259
- getAssetJSON(skip) {
260
- const self = this;
261
- return new Promise((resolve, reject) => {
262
- if (typeof skip !== 'number') {
263
- skip = 0;
264
- }
265
- const queryRequestObj = {
266
- skip: skip,
267
- limit: self.bLimit,
268
- include_publish_details: true,
269
- except: {
270
- BASE: self.invalidKeys,
271
- },
272
- };
273
-
274
- self.stackAPIClient
275
- .asset()
276
- .query(queryRequestObj)
277
- .find()
278
- .then(({ items }) => resolve(items))
279
- .catch((error) => {
280
- addlogs(self.config, error, 'error');
281
- return reject();
282
- });
283
- });
284
- }
285
-
286
- getVersionedAssetJSON(uid, version, bucket) {
287
- const self = this;
288
- const assetVersionInfo = bucket || [];
289
-
290
- return new Promise((resolve, reject) => {
291
- if (self.assetDownloadRetry[uid + version] > self.assetDownloadRetryLimit) {
292
- console.log('Reached max', self.assetDownloadRetry[uid + version]);
293
- return reject(new Error('Asset Max download retry limit exceeded! ' + uid));
294
- }
295
-
296
- if (version <= 0) {
297
- const assetVersionInfoFile = path.resolve(self.assetsFolderPath, uid, '_contentstack_' + uid + '.json');
298
- helper.writeFileSync(assetVersionInfoFile, assetVersionInfo);
299
- return resolve();
300
- }
301
- const queryrequestOption = {
302
- version: version,
303
- include_publish_details: true,
304
- except: {
305
- BASE: self.invalidKeys,
306
- },
307
- };
308
-
309
- self.stackAPIClient
310
- .asset(uid)
311
- .fetch(queryrequestOption)
312
- .then((versionedAssetJSONResponse) => {
313
- self
314
- .downloadAsset(versionedAssetJSONResponse)
315
- .then(() => {
316
- assetVersionInfo.splice(0, 0, versionedAssetJSONResponse);
317
- // Remove duplicates
318
- assetVersionInfo = _.uniqWith(assetVersionInfo, _.isEqual);
319
- self.getVersionedAssetJSON(uid, --version, assetVersionInfo).then(resolve).catch(reject);
320
- })
321
- .catch(reject);
322
- })
323
- .catch((error) => {
324
- addlogs(self.config, error, 'error');
325
-
326
- if (error.status === 408) {
327
- console.log('retrying', uid);
328
- // retrying when timeout
329
- self.assetDownloadRetry[uid + version]
330
- ? ++self.assetDownloadRetry[uid + version]
331
- : (self.assetDownloadRetry[uid + version] = 1);
332
- return self.getVersionedAssetJSON(uid, version, assetVersionInfo).then(resolve).catch(reject);
333
- }
334
- reject(error);
335
- });
336
- });
337
- }
338
-
339
- downloadAsset(asset) {
340
- const self = this;
341
- return new Promise(async (resolve, reject) => {
342
- const assetFolderPath = path.resolve(self.assetsFolderPath, asset.uid);
343
- const assetFilePath = path.resolve(assetFolderPath, asset.filename);
344
-
345
- if (fs.existsSync(assetFilePath)) {
346
- addlogs(
347
- self.config,
348
- 'Skipping download of { title: ' + asset.filename + ', uid: ' + asset.uid + ' }, as they already exist',
349
- 'success',
350
- );
351
- return resolve();
352
- }
353
- self.assetStream = {
354
- url: self.config.securedAssets
355
- ? `${asset.url}?authtoken=${configHandler.get('authtoken')}`
356
- : asset.url,
357
- };
358
-
359
- mkdirp.sync(assetFolderPath);
360
- const assetFileStream = fs.createWriteStream(assetFilePath);
361
- self.assetStream.url = encodeURI(self.assetStream.url);
362
- self.httpClient
363
- .options({ responseType: 'stream' })
364
- .get(self.assetStream.url)
365
- .then(({ data: assetStreamRequest }) => {
366
- if (self.assetConfig.enableDownloadStatus) {
367
- const str = progress({
368
- time: 5000,
369
- length: assetStreamRequest.headers['content-length'],
370
- });
371
- str.on('progress', (progressData) => {
372
- console.log(`${asset.filename}: ${Math.round(progressData.percentage)}%`);
373
- });
374
- assetStreamRequest.pipe(str).pipe(assetFileStream);
375
- }
376
- assetStreamRequest.pipe(assetFileStream);
377
- })
378
- .catch((error) => {
379
- addlogs(self.config, error, 'error');
380
- reject(error);
381
- });
382
- assetFileStream
383
- .on('close', () => {
384
- addlogs(self.config, 'Downloaded ' + asset.filename + ': ' + asset.uid + ' successfully!', 'success');
385
- return resolve();
386
- })
387
- .on('error', (error) => {
388
- addlogs(self.config, `Download ${asset.filename}: ${asset.uid} failed!`, 'error');
389
- addlogs(self.config, error, 'error');
390
- reject(error);
391
- });
392
- });
393
- }
394
-
395
- getFolders() {
396
- const self = this;
397
- return new Promise((resolve, reject) => {
398
- return self
399
- .getAssetCount(true)
400
- .then((count) => {
401
- if (count === 0) {
402
- addlogs(self.config, 'No folders were found in the stack', 'success');
403
- return resolve();
404
- }
405
- return self
406
- .getFolderDetails(0, count)
407
- .then(() => {
408
- addlogs(self.config, chalk.green('Exported asset-folders successfully!'), 'success');
409
- return resolve();
410
- })
411
- .catch((error) => {
412
- addlogs(self.config, error, 'error');
413
- reject(error);
414
- });
415
- })
416
- .catch((error) => {
417
- addlogs(self.config, error, 'error');
418
- reject(error);
419
- });
420
- });
421
- }
422
-
423
- getFolderDetails(skip, tCount) {
424
- const self = this;
425
- return new Promise((resolve, reject) => {
426
- if (typeof skip !== 'number') {
427
- skip = 0;
428
- }
429
- if (skip > tCount) {
430
- helper.writeFileSync(self.folderJSONPath, self.folderContents);
431
- return resolve();
432
- }
433
- const queryRequestObj = {
434
- skip: skip,
435
- include_folders: true,
436
- query: { is_dir: true },
437
- };
438
- self.stackAPIClient
439
- .asset()
440
- .query(queryRequestObj)
441
- .find()
442
- .then((folderDetailsResponse) => {
443
- for (let i in folderDetailsResponse.items) {
444
- self.folderContents.push(folderDetailsResponse.items[i]);
445
- }
446
- skip += 100;
447
- return self.getFolderDetails(skip, tCount).then(resolve).catch(reject);
448
- })
449
- .catch((error) => {
450
- addlogs(self.config, error, 'error');
451
- });
452
- });
453
- }
454
- };
@@ -1,90 +0,0 @@
1
- const path = require('path');
2
- const fileHelper = require('../util/helper');
3
- const chalk = require('chalk');
4
- const { executeTask, formatError } = require('../util');
5
- const { addlogs } = require('../util/log');
6
-
7
- class ContentTypesExport {
8
- constructor(exportConfig, stackAPIClient) {
9
- this.stackAPIClient = stackAPIClient;
10
- this.exportConfig = exportConfig;
11
- this.contentTypesConfig = exportConfig.modules.content_types;
12
- this.qs = {
13
- include_count: true,
14
- asc: 'updated_at',
15
- limit: this.contentTypesConfig.limit,
16
- include_global_field_schema: true,
17
- };
18
- // If content type id is provided then use it as part of query
19
- if (Array.isArray(this.exportConfig.contentTypes) && this.exportConfig.length > 0) {
20
- this.qs.uid = { $in: this.exportConfig.contentTypes };
21
- }
22
- this.contentTypesPath = path.resolve(
23
- exportConfig.data,
24
- exportConfig.branchName || '',
25
- this.contentTypesConfig.dirName,
26
- );
27
- this.contentTypes = [];
28
- this.fetchConcurrency = this.contentTypesConfig.fetchConcurrency || this.exportConfig.fetchConcurrency;
29
- this.writeConcurrency = this.contentTypesConfig.writeConcurrency || this.exportConfig.writeConcurrency;
30
- }
31
-
32
- async start() {
33
- try {
34
- addlogs(this.exportConfig, 'Starting content type export', 'success');
35
- await fileHelper.makeDirectory(this.contentTypesPath);
36
- await this.getContentTypes();
37
- await this.writeContentTypes(this.contentTypes);
38
- addlogs(this.exportConfig, chalk.green('Content type(s) exported successfully'), 'success');
39
- } catch (error) {
40
- addlogs(this.exportConfig, `Failed to export content types ${formatError(error)}`, 'error');
41
- throw new Error('Failed to export content types');
42
- }
43
- }
44
-
45
- async getContentTypes(skip = 0) {
46
- if (skip) {
47
- this.qs.skip = skip;
48
- }
49
-
50
- const contentTypeSearchResponse = await this.stackAPIClient.contentType().query(this.qs).find();
51
- if (Array.isArray(contentTypeSearchResponse.items) && contentTypeSearchResponse.items.length > 0) {
52
- let updatedContentTypes = this.sanitizeAttribs(contentTypeSearchResponse.items);
53
- this.contentTypes.push(...updatedContentTypes);
54
-
55
- skip += this.contentTypesConfig.limit;
56
- if (skip > contentTypeSearchResponse.count) {
57
- return;
58
- }
59
- return await this.getContentTypes(skip);
60
- } else {
61
- addlogs(this.exportConfig, 'No content types returned for the given query', 'info');
62
- }
63
- }
64
-
65
- sanitizeAttribs(contentTypes) {
66
- let updatedContentTypes = [];
67
- contentTypes.forEach((contentType) => {
68
- for (let key in contentType) {
69
- if (this.contentTypesConfig.validKeys.indexOf(key) === -1) {
70
- delete contentType[key];
71
- }
72
- }
73
- updatedContentTypes.push(contentType);
74
- });
75
- return updatedContentTypes;
76
- }
77
-
78
- async writeContentTypes(contentTypes) {
79
- function write(contentType) {
80
- return fileHelper.writeFile(
81
- path.join(this.contentTypesPath,
82
- `${contentType.uid === 'schema' ? 'schema|1' : contentType.uid}.json`),
83
- contentType);
84
- }
85
- await executeTask(contentTypes, write.bind(this), { concurrency: this.writeConcurrency });
86
- return fileHelper.writeFile(path.join(this.contentTypesPath, 'schema.json'), contentTypes);
87
- }
88
- }
89
-
90
- module.exports = ContentTypesExport;
@@ -1,92 +0,0 @@
1
- 'use strict';
2
-
3
- const path = require('path');
4
- const chalk = require('chalk');
5
- const mkdirp = require('mkdirp');
6
- const { merge } = require('lodash');
7
- const helper = require('../util/helper');
8
- const { addlogs } = require('../util/log');
9
- const { formatError } = require('../util');
10
- const config = require('../../config/default');
11
-
12
- module.exports = class ExportCustomRoles {
13
- roles = {};
14
- customRoles = {};
15
- EXISTING_ROLES = {
16
- Admin: 1,
17
- Developer: 1,
18
- 'Content Manager': 1,
19
- };
20
- rolesConfig = config.modules.customRoles;
21
-
22
- constructor(exportConfig, stackAPIClient) {
23
- this.config = merge(config, exportConfig);
24
- this.stackAPIClient = stackAPIClient;
25
- }
26
-
27
- async start() {
28
- const self = this;
29
-
30
- try {
31
- addlogs(this.config, 'Starting roles export', 'success');
32
- const rolesFolderPath = path.resolve(this.config.data, this.config.branchName || '', this.rolesConfig.dirName);
33
- mkdirp.sync(rolesFolderPath);
34
- const roles = await self.stackAPIClient.role().fetchAll({ include_rules: true, include_permissions: true });
35
- const customRoles = roles.items.filter((role) => !self.EXISTING_ROLES[role.name]);
36
- if (!customRoles.length) {
37
- addlogs(self.config, 'No custom roles were found in the Stack', 'success');
38
- return;
39
- }
40
- await self.getCustomRolesLocales(
41
- customRoles,
42
- path.join(rolesFolderPath, self.rolesConfig.customRolesLocalesFileName),
43
- self.stackAPIClient,
44
- self.config,
45
- );
46
- self.customRoles = {};
47
- customRoles.forEach((role) => {
48
- addlogs(self.config, `'${role.name}' role was exported successfully`, 'success');
49
- self.customRoles[role.uid] = role;
50
- });
51
- helper.writeFileSync(path.join(rolesFolderPath, self.rolesConfig.fileName), self.customRoles);
52
- addlogs(self.config, chalk.green('All the custom roles have been exported successfully'), 'success');
53
- } catch (error) {
54
- if (error.statusCode === 401) {
55
- addlogs(
56
- self.config,
57
- 'You are not allowed to export roles, Unless you provide email and password in config',
58
- 'error',
59
- );
60
- return;
61
- }
62
- addlogs(self.config, `Error occurred in exporting roles. ${formatError(error)}`, 'error');
63
- throw error;
64
- }
65
- }
66
-
67
- async getCustomRolesLocales(customRoles, customRolesLocalesFilepath, stackAPIClient, config) {
68
- const localesMap = {};
69
- for (const role of customRoles) {
70
- const rulesLocales = role.rules.find((rule) => rule.module === 'locale');
71
- if (rulesLocales.locales && rulesLocales.locales.length) {
72
- rulesLocales.locales.forEach((locale) => {
73
- localesMap[locale] = 1;
74
- });
75
- }
76
- }
77
- if (Object.keys(localesMap).length) {
78
- const locales = await stackAPIClient.locale().query({}).find();
79
- const sourceLocalesMap = {};
80
- for (const locale of locales.items) {
81
- sourceLocalesMap[locale.uid] = locale;
82
- }
83
- for (const locale in localesMap) {
84
- if(sourceLocalesMap[locale]!==undefined) {
85
- delete sourceLocalesMap[locale]['stackHeaders'];
86
- }
87
- localesMap[locale] = sourceLocalesMap[locale];
88
- }
89
- helper.writeFileSync(customRolesLocalesFilepath, localesMap);
90
- }
91
- }
92
- };