@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
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+ const path = require('path');
3
+ const chalk = require('chalk');
4
+ const mkdirp = require('mkdirp');
5
+ const { merge } = require('lodash');
6
+ const { fileHelper, log, formatError } = require('../../utils');
7
+ const { default: config } = require('../../config');
8
+ module.exports = class ExportCustomRoles {
9
+ constructor(exportConfig, stackAPIClient) {
10
+ this.roles = {};
11
+ this.customRoles = {};
12
+ this.EXISTING_ROLES = {
13
+ Admin: 1,
14
+ Developer: 1,
15
+ 'Content Manager': 1,
16
+ };
17
+ this.rolesConfig = config.modules.customRoles;
18
+ this.config = merge(config, exportConfig);
19
+ this.stackAPIClient = stackAPIClient;
20
+ }
21
+ async start() {
22
+ const self = this;
23
+ try {
24
+ log(this.config, 'Starting roles export', 'success');
25
+ const rolesFolderPath = path.resolve(this.config.data, this.config.branchName || '', this.rolesConfig.dirName);
26
+ mkdirp.sync(rolesFolderPath);
27
+ const roles = await self.stackAPIClient.role().fetchAll({ include_rules: true, include_permissions: true });
28
+ const customRoles = roles.items.filter((role) => !self.EXISTING_ROLES[role.name]);
29
+ if (!customRoles.length) {
30
+ log(self.config, 'No custom roles were found in the Stack', 'success');
31
+ return;
32
+ }
33
+ await self.getCustomRolesLocales(customRoles, path.join(rolesFolderPath, self.rolesConfig.customRolesLocalesFileName), self.stackAPIClient, self.config);
34
+ self.customRoles = {};
35
+ customRoles.forEach((role) => {
36
+ log(self.config, `'${role.name}' role was exported successfully`, 'success');
37
+ self.customRoles[role.uid] = role;
38
+ });
39
+ fileHelper.writeFileSync(path.join(rolesFolderPath, self.rolesConfig.fileName), self.customRoles);
40
+ log(self.config, chalk.green('All the custom roles have been exported successfully'), 'success');
41
+ }
42
+ catch (error) {
43
+ if (error.statusCode === 401) {
44
+ log(self.config, 'You are not allowed to export roles, Unless you provide email and password in config', 'error');
45
+ return;
46
+ }
47
+ log(self.config, `Error occurred in exporting roles. ${formatError(error)}`, 'error');
48
+ throw error;
49
+ }
50
+ }
51
+ async getCustomRolesLocales(customRoles, customRolesLocalesFilepath, stackAPIClient, config) {
52
+ const localesMap = {};
53
+ for (const role of customRoles) {
54
+ const rulesLocales = role.rules.find((rule) => rule.module === 'locale');
55
+ if (rulesLocales.locales && rulesLocales.locales.length) {
56
+ rulesLocales.locales.forEach((locale) => {
57
+ localesMap[locale] = 1;
58
+ });
59
+ }
60
+ }
61
+ if (Object.keys(localesMap).length) {
62
+ const locales = await stackAPIClient.locale().query({}).find();
63
+ const sourceLocalesMap = {};
64
+ for (const locale of locales.items) {
65
+ sourceLocalesMap[locale.uid] = locale;
66
+ }
67
+ for (const locale in localesMap) {
68
+ if (sourceLocalesMap[locale] !== undefined) {
69
+ delete sourceLocalesMap[locale]['stackHeaders'];
70
+ }
71
+ localesMap[locale] = sourceLocalesMap[locale];
72
+ }
73
+ fileHelper.writeFileSync(customRolesLocalesFilepath, localesMap);
74
+ }
75
+ }
76
+ };
@@ -0,0 +1,18 @@
1
+ export = EntriesExport;
2
+ declare class EntriesExport {
3
+ constructor(exportConfig: any, stackAPIClient: any);
4
+ stackAPIClient: any;
5
+ exportConfig: any;
6
+ entriesConfig: any;
7
+ entriesRootPath: string;
8
+ localesFilePath: string;
9
+ schemaFilePath: string;
10
+ fetchConcurrency: any;
11
+ writeConcurrency: any;
12
+ start(): Promise<void>;
13
+ getEntries(requestOptions: any, skip?: number, entries?: {}): any;
14
+ getEntryByVersion(requestOptions: any, version: any, entries?: any[]): any;
15
+ getEntriesCount(requestOptions: any): Promise<any>;
16
+ createRequestObjects(locales: any, contentTypes: any): any[];
17
+ sanitizeAttribs(entries: any, entriesList?: {}): {};
18
+ }
@@ -0,0 +1,141 @@
1
+ const path = require('path');
2
+ const chalk = require('chalk');
3
+ const { executeTask, formatError, fileHelper, log } = require('../../utils');
4
+ class EntriesExport {
5
+ constructor(exportConfig, stackAPIClient) {
6
+ this.stackAPIClient = stackAPIClient;
7
+ this.exportConfig = exportConfig;
8
+ this.entriesConfig = exportConfig.modules.entries;
9
+ this.entriesRootPath = path.resolve(exportConfig.data, exportConfig.branchName || '', this.entriesConfig.dirName);
10
+ this.localesFilePath = path.resolve(exportConfig.data, exportConfig.branchName || '', exportConfig.modules.locales.dirName, exportConfig.modules.locales.fileName);
11
+ this.schemaFilePath = path.resolve(exportConfig.data, exportConfig.branchName || '', exportConfig.modules.content_types.dirName, 'schema.json');
12
+ this.fetchConcurrency = this.entriesConfig.fetchConcurrency || exportConfig.fetchConcurrency;
13
+ this.writeConcurrency = this.entriesConfig.writeConcurrency || exportConfig.writeConcurrency;
14
+ }
15
+ async start() {
16
+ try {
17
+ log(this.exportConfig, 'Starting entries export', 'info');
18
+ const locales = await fileHelper.readFile(this.localesFilePath);
19
+ const contentTypes = await fileHelper.readFile(this.schemaFilePath);
20
+ if (contentTypes.length === 0) {
21
+ log(this.exportConfig, 'No content types found to export entries', 'info');
22
+ return;
23
+ }
24
+ const entryRequestOptions = this.createRequestObjects(locales, contentTypes);
25
+ for (let requestOption of entryRequestOptions) {
26
+ log(this.exportConfig, `Starting export of entries of content_type - ${requestOption.content_type} locale - ${requestOption.locale}`, 'info');
27
+ await fileHelper.makeDirectory(path.join(this.entriesRootPath, requestOption.content_type));
28
+ const entries = await this.getEntries(requestOption);
29
+ let entriesFilePath = path.join(this.entriesRootPath, requestOption.content_type, requestOption.locale + '.json');
30
+ await fileHelper.writeLargeFile(entriesFilePath, entries);
31
+ log(this.exportConfig, `Exported entries of type '${requestOption.content_type}' locale '${requestOption.locale}'`, 'success');
32
+ if (this.exportConfig.versioning) {
33
+ log(this.exportConfig, `Started export versioned entries of type '${requestOption.content_type}' locale '${requestOption.locale}'`, 'info');
34
+ for (let entry of entries) {
35
+ const versionedEntries = await this.getEntryByVersion(Object.assign(Object.assign({}, requestOption), { uid: entry.uid }), entry._version);
36
+ let versionedEntryPath = path.join(this.entriesRootPath, requestOption.locale, requestOption.content_type, entry.uid);
37
+ await fileHelper.makeDirectory(versionedEntryPath);
38
+ if (versionedEntries.length > 0) {
39
+ const write = (versionedEntry) => fileHelper.writeFile(path.join(versionedEntryPath, 'version-' + versionedEntry._version + '.json'), versionedEntry);
40
+ await executeTask(versionedEntries, write.bind(this), { concurrency: this.writeConcurrency });
41
+ log(this.exportConfig, `Exported versioned entries of type '${requestOption.content_type}' locale '${requestOption.locale}'`, 'success');
42
+ }
43
+ }
44
+ }
45
+ }
46
+ log(this.exportConfig, chalk.green('Entries exported successfully'), 'success');
47
+ }
48
+ catch (error) {
49
+ log(this.exportConfig, `Failed to export entries ${formatError(error)}`, 'error');
50
+ throw new Error('Failed to export entries');
51
+ }
52
+ }
53
+ async getEntries(requestOptions, skip = 0, entries = {}) {
54
+ let requestObject = {
55
+ locale: requestOptions.locale,
56
+ skip,
57
+ limit: this.entriesConfig.limit,
58
+ include_count: true,
59
+ include_publish_details: true,
60
+ query: {
61
+ locale: requestOptions.locale,
62
+ },
63
+ };
64
+ const entriesSearchResponse = await this.stackAPIClient
65
+ .contentType(requestOptions.content_type)
66
+ .entry()
67
+ .query(requestObject)
68
+ .find();
69
+ if (Array.isArray(entriesSearchResponse.items) && entriesSearchResponse.items.length > 0) {
70
+ // clean up attribs and add to parent entry list
71
+ this.sanitizeAttribs(entriesSearchResponse.items, entries);
72
+ skip += this.entriesConfig.limit || 100;
73
+ if (skip > entriesSearchResponse.count) {
74
+ return entries;
75
+ }
76
+ return await this.getEntries(requestOptions, skip, entries);
77
+ }
78
+ return entries;
79
+ }
80
+ async getEntryByVersion(requestOptions, version, entries = []) {
81
+ const queryRequestObject = {
82
+ locale: requestOptions.locale,
83
+ except: {
84
+ BASE: this.entriesConfig.invalidKeys,
85
+ },
86
+ version,
87
+ };
88
+ const entryResponse = await this.stackAPIClient
89
+ .contentType(requestOptions.content_type)
90
+ .entry(requestOptions.uid)
91
+ .fetch(queryRequestObject);
92
+ entries.push(entryResponse);
93
+ if (--version > 0) {
94
+ return await this.getEntryByVersion(requestOptions, version, entries);
95
+ }
96
+ return entries;
97
+ }
98
+ async getEntriesCount(requestOptions) {
99
+ let requestObject = {
100
+ locale: requestOptions.locale,
101
+ limit: 1,
102
+ include_count: true,
103
+ include_publish_details: true,
104
+ query: {
105
+ locale: requestOptions.locale,
106
+ },
107
+ };
108
+ const entriesSearchResponse = await this.stackAPIClient
109
+ .contentType(requestOptions.content_type)
110
+ .entry()
111
+ .query(requestObject)
112
+ .find();
113
+ return entriesSearchResponse.count;
114
+ }
115
+ createRequestObjects(locales, contentTypes) {
116
+ let requestObjects = [];
117
+ contentTypes.forEach((contentType) => {
118
+ if (Object.keys(locales).length !== 0) {
119
+ for (let locale in locales) {
120
+ requestObjects.push({
121
+ content_type: contentType.uid,
122
+ locale: locales[locale].code,
123
+ });
124
+ }
125
+ }
126
+ requestObjects.push({
127
+ content_type: contentType.uid,
128
+ locale: this.exportConfig.master_locale.code,
129
+ });
130
+ });
131
+ return requestObjects;
132
+ }
133
+ sanitizeAttribs(entries, entriesList = {}) {
134
+ entries.forEach((entry) => {
135
+ this.entriesConfig.invalidKeys.forEach((key) => delete entry[key]);
136
+ entriesList[entry.uid] = entry;
137
+ });
138
+ return entriesList;
139
+ }
140
+ }
141
+ module.exports = EntriesExport;
@@ -0,0 +1,16 @@
1
+ export = ExportEnvironments;
2
+ declare class ExportEnvironments {
3
+ constructor(exportConfig: any, stackAPIClient: any);
4
+ config: {};
5
+ master: {};
6
+ environments: {};
7
+ requestOptions: {
8
+ json: boolean;
9
+ qs: {
10
+ asc: string;
11
+ include_count: boolean;
12
+ };
13
+ };
14
+ stackAPIClient: any;
15
+ start(): Promise<any>;
16
+ }
@@ -0,0 +1,62 @@
1
+ /*!
2
+ * Contentstack Export
3
+ * Copyright (c) 2019 Contentstack LLC
4
+ * MIT Licensed
5
+ */
6
+ const path = require('path');
7
+ const chalk = require('chalk');
8
+ const mkdirp = require('mkdirp');
9
+ const { merge } = require('lodash');
10
+ const { fileHelper, log, formatError } = require('../../utils');
11
+ module.exports = class ExportEnvironments {
12
+ constructor(exportConfig, stackAPIClient) {
13
+ this.config = {};
14
+ this.master = {};
15
+ this.environments = {};
16
+ this.requestOptions = {
17
+ json: true,
18
+ qs: {
19
+ asc: 'updated_at',
20
+ include_count: true,
21
+ },
22
+ };
23
+ this.config = merge(this.config, exportConfig);
24
+ this.stackAPIClient = stackAPIClient;
25
+ }
26
+ start() {
27
+ const self = this;
28
+ const environmentConfig = self.config.modules.environments;
29
+ const environmentsFolderPath = path.resolve(self.config.data, self.config.branchName || '', environmentConfig.dirName);
30
+ // Create folder for environments
31
+ fileHelper.makeDirectory(environmentsFolderPath);
32
+ log(this.config, 'Starting environment export', 'success');
33
+ return new Promise(function (resolve, reject) {
34
+ self.stackAPIClient
35
+ .environment()
36
+ .query(self.requestOptions.qs)
37
+ .find()
38
+ .then((environmentResponse) => {
39
+ if (environmentResponse.items.length !== 0) {
40
+ for (let i = 0, total = environmentResponse.count; i < total; i++) {
41
+ const envUid = environmentResponse.items[i].uid;
42
+ self.master[envUid] = '';
43
+ self.environments[envUid] = environmentResponse.items[i];
44
+ delete self.environments[envUid].uid;
45
+ delete self.environments[envUid]['ACL'];
46
+ }
47
+ fileHelper.writeFileSync(path.join(environmentsFolderPath, environmentConfig.fileName), self.environments);
48
+ log(self.config, chalk.green('All the environments have been exported successfully'), 'success');
49
+ return resolve();
50
+ }
51
+ if (environmentResponse.items.length === 0) {
52
+ log(self.config, 'No environments found', 'success');
53
+ resolve();
54
+ }
55
+ })
56
+ .catch((error) => {
57
+ log(self.config, `Environments export failed. ${formatError(error)}`, 'error');
58
+ reject(error);
59
+ });
60
+ });
61
+ }
62
+ };
@@ -0,0 +1,18 @@
1
+ export = ExportExtensions;
2
+ declare class ExportExtensions {
3
+ constructor(exportConfig: any, stackAPIClient: any);
4
+ master: {};
5
+ extensions: {};
6
+ extensionConfig: {
7
+ dirName: string;
8
+ fileName: string;
9
+ dependencies?: import("../../types").Modules[];
10
+ };
11
+ queryRequestOptions: {
12
+ asc: string;
13
+ include_count: boolean;
14
+ };
15
+ config: any;
16
+ stackAPIClient: any;
17
+ start(): Promise<any>;
18
+ }
@@ -0,0 +1,57 @@
1
+ /*!
2
+ * Contentstack Export
3
+ * Copyright (c) 2019 Contentstack LLC
4
+ * MIT Licensed
5
+ */
6
+ const mkdirp = require('mkdirp');
7
+ const path = require('path');
8
+ const chalk = require('chalk');
9
+ const { merge } = require('lodash');
10
+ const { formatError, log, fileHelper } = require('../../utils');
11
+ const { default: config } = require('../../config');
12
+ module.exports = class ExportExtensions {
13
+ constructor(exportConfig, stackAPIClient) {
14
+ this.master = {};
15
+ this.extensions = {};
16
+ this.extensionConfig = config.modules.extensions;
17
+ this.queryRequestOptions = {
18
+ asc: 'updated_at',
19
+ include_count: true,
20
+ };
21
+ this.config = merge(config, exportConfig);
22
+ this.stackAPIClient = stackAPIClient;
23
+ }
24
+ start() {
25
+ log(this.config, 'Starting extension export', 'success');
26
+ const self = this;
27
+ const extensionsFolderPath = path.resolve(this.config.data, this.config.branchName || '', this.extensionConfig.dirName);
28
+ // Create folder for extensions
29
+ mkdirp.sync(extensionsFolderPath);
30
+ return new Promise(function (resolve, reject) {
31
+ self.stackAPIClient
32
+ .extension()
33
+ .query(self.queryRequestOptions)
34
+ .find()
35
+ .then((extension) => {
36
+ if (extension.items.length !== 0) {
37
+ for (let i = 0, total = extension.count; i < total; i++) {
38
+ const extUid = extension.items[i].uid;
39
+ self.master[extUid] = '';
40
+ self.extensions[extUid] = extension.items[i];
41
+ delete self.extensions[extUid].uid;
42
+ delete self.extensions[extUid].SYS_ACL;
43
+ }
44
+ fileHelper.writeFileSync(path.join(extensionsFolderPath, self.extensionConfig.fileName), self.extensions);
45
+ log(self.config, chalk.green('All the extensions have been exported successfully'), 'success');
46
+ return resolve();
47
+ }
48
+ log(self.config, 'No extensions found', 'success');
49
+ resolve();
50
+ })
51
+ .catch((error) => {
52
+ log(self.config, `Failed to export extensions. ${formatError(error)}`, 'error');
53
+ reject();
54
+ });
55
+ });
56
+ }
57
+ };
@@ -0,0 +1,22 @@
1
+ export = ExportGlobalFields;
2
+ declare class ExportGlobalFields {
3
+ constructor(exportConfig: any, stackAPIClient: any);
4
+ limit: number;
5
+ config: {};
6
+ global_fields: any[];
7
+ master: {};
8
+ globalfields: {};
9
+ requestOptions: {};
10
+ globalfieldsFolderPath: string;
11
+ globalfieldsConfig: {
12
+ dirName: string;
13
+ fileName: string;
14
+ validKeys: string[];
15
+ dependencies?: import("../../types").Modules[];
16
+ };
17
+ validKeys: string[];
18
+ stackAPIClient: any;
19
+ start(): Promise<any>;
20
+ getGlobalFields(skip: any, globalFieldConfig: any): Promise<any>;
21
+ writeGlobalFields(): Promise<any>;
22
+ }
@@ -0,0 +1,107 @@
1
+ /*!
2
+ * Contentstack Export
3
+ * Copyright (c) 2019 Contentstack LLC
4
+ * MIT Licensed
5
+ */
6
+ const path = require('path');
7
+ const chalk = require('chalk');
8
+ const mkdirp = require('mkdirp');
9
+ const { merge } = require('lodash');
10
+ const { formatError, log, fileHelper } = require('../../utils');
11
+ const { default: config } = require('../../config');
12
+ module.exports = class ExportGlobalFields {
13
+ constructor(exportConfig, stackAPIClient) {
14
+ this.limit = 100;
15
+ this.config = {};
16
+ this.global_fields = [];
17
+ this.master = {};
18
+ this.globalfields = {};
19
+ this.requestOptions = {};
20
+ this.globalfieldsConfig = config.modules.globalfields;
21
+ this.validKeys = config.modules.globalfields.validKeys;
22
+ this.requestOptions = {
23
+ qs: {
24
+ skip: 0,
25
+ limit: this.limit,
26
+ asc: 'updated_at',
27
+ include_count: true,
28
+ },
29
+ };
30
+ this.config = merge(config, exportConfig);
31
+ this.stackAPIClient = stackAPIClient;
32
+ this.globalfieldsFolderPath = path.resolve(this.config.data, this.config.branchName || '', this.globalfieldsConfig.dirName);
33
+ }
34
+ start() {
35
+ const self = this;
36
+ // Create folder for Global Fields
37
+ mkdirp.sync(self.globalfieldsFolderPath);
38
+ log(self.config, 'Starting Global Fields export', 'success');
39
+ return new Promise(function (resolve, reject) {
40
+ try {
41
+ return self
42
+ .getGlobalFields(0, self.config)
43
+ .then(function (result) {
44
+ if (!result) {
45
+ return self.writeGlobalFields().then(resolve).catch(reject);
46
+ }
47
+ return resolve();
48
+ })
49
+ .catch(reject);
50
+ }
51
+ catch (error) {
52
+ log(self.config, error, 'error');
53
+ return reject(error);
54
+ }
55
+ });
56
+ }
57
+ getGlobalFields(skip, globalFieldConfig) {
58
+ const self = this;
59
+ self.requestOptions.qs.skip = skip;
60
+ return new Promise(function (resolve, reject) {
61
+ self.stackAPIClient
62
+ .globalField()
63
+ .query(self.requestOptions.qs)
64
+ .find()
65
+ .then((globalFieldResponse) => {
66
+ try {
67
+ if (globalFieldResponse.items.length === 0) {
68
+ log(globalFieldConfig, 'No global fields found', 'success');
69
+ return resolve('No Global Fields');
70
+ }
71
+ globalFieldResponse.items.forEach(function (globalField) {
72
+ for (const key in globalField) {
73
+ if (self.validKeys.indexOf(key) === -1) {
74
+ delete globalField[key];
75
+ }
76
+ }
77
+ self.global_fields.push(globalField);
78
+ });
79
+ skip += self.limit;
80
+ if (skip >= globalFieldResponse.count) {
81
+ return resolve();
82
+ }
83
+ return self.getGlobalFields(skip, globalFieldConfig).then(resolve).catch(reject);
84
+ }
85
+ catch (error) {
86
+ log(globalFieldConfig, `Failed to export global-fields. ${formatError(error)}`, 'error');
87
+ reject(error);
88
+ }
89
+ })
90
+ .catch(reject);
91
+ });
92
+ }
93
+ writeGlobalFields() {
94
+ const self = this;
95
+ return new Promise(function (resolve, reject) {
96
+ try {
97
+ fileHelper.writeFileSync(path.join(self.globalfieldsFolderPath, self.globalfieldsConfig.fileName), self.global_fields);
98
+ log(self.config, chalk.green('Global Fields export completed successfully'), 'success');
99
+ resolve();
100
+ }
101
+ catch (error) {
102
+ log(self.config, error, 'error');
103
+ reject(error);
104
+ }
105
+ });
106
+ }
107
+ };
@@ -0,0 +1,2 @@
1
+ export = startModuleExport;
2
+ declare function startModuleExport(modulePayload: any): Promise<any>;
@@ -0,0 +1,31 @@
1
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2
+ if (k2 === undefined) k2 = k;
3
+ var desc = Object.getOwnPropertyDescriptor(m, k);
4
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5
+ desc = { enumerable: true, get: function() { return m[k]; } };
6
+ }
7
+ Object.defineProperty(o, k2, desc);
8
+ }) : (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ o[k2] = m[k];
11
+ }));
12
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
13
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
14
+ }) : function(o, v) {
15
+ o["default"] = v;
16
+ });
17
+ var __importStar = (this && this.__importStar) || function (mod) {
18
+ if (mod && mod.__esModule) return mod;
19
+ var result = {};
20
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
21
+ __setModuleDefault(result, mod);
22
+ return result;
23
+ };
24
+ async function startModuleExport(modulePayload) {
25
+ var _a;
26
+ const { moduleName, exportConfig, stackAPIClient } = modulePayload;
27
+ const { default: ModuleRunner } = await (_a = `./${moduleName}.js`, Promise.resolve().then(() => __importStar(require(_a))));
28
+ const moduleRunner = new ModuleRunner(exportConfig, stackAPIClient);
29
+ return moduleRunner.start();
30
+ }
31
+ module.exports = startModuleExport;
@@ -0,0 +1,14 @@
1
+ export = ExportLabels;
2
+ declare class ExportLabels {
3
+ constructor(exportConfig: any, stackAPIClient: any);
4
+ labels: {};
5
+ labelConfig: {
6
+ dirName: string;
7
+ fileName: string;
8
+ invalidKeys: string[];
9
+ dependencies?: import("../../types").Modules[];
10
+ };
11
+ config: any;
12
+ stackAPIClient: any;
13
+ start(): Promise<any>;
14
+ }
@@ -0,0 +1,56 @@
1
+ /*!
2
+ * Contentstack Export
3
+ * Copyright (c) 2019 Contentstack LLC
4
+ * MIT Licensed
5
+ */
6
+ const path = require('path');
7
+ const chalk = require('chalk');
8
+ const mkdirp = require('mkdirp');
9
+ const { merge } = require('lodash');
10
+ const { formatError, log, fileHelper } = require('../../utils');
11
+ const { default: config } = require('../../config');
12
+ module.exports = class ExportLabels {
13
+ constructor(exportConfig, stackAPIClient) {
14
+ this.labels = {};
15
+ this.labelConfig = config.modules.labels;
16
+ this.config = merge(config, exportConfig);
17
+ this.stackAPIClient = stackAPIClient;
18
+ }
19
+ start() {
20
+ log(this.config, 'Starting labels export', 'success');
21
+ const self = this;
22
+ const labelsFolderPath = path.resolve(config.data, this.config.branchName || '', self.labelConfig.dirName);
23
+ // Create locale folder
24
+ mkdirp.sync(labelsFolderPath);
25
+ return new Promise(function (resolve, reject) {
26
+ return self.stackAPIClient
27
+ .label()
28
+ .query()
29
+ .find()
30
+ .then((response) => {
31
+ if (response.items.length !== 0) {
32
+ response.items.forEach(function (label) {
33
+ log(self.config, `'${label.name}' label was exported successfully`, 'success');
34
+ self.labels[label.uid] = label;
35
+ const deleteItems = self.config.modules.labels.invalidKeys;
36
+ deleteItems.forEach((e) => delete label[e]);
37
+ });
38
+ log(self.config, chalk.green('All the labels have been exported successfully'), 'success');
39
+ }
40
+ else {
41
+ log(self.config, 'No labels found', 'success');
42
+ }
43
+ fileHelper.writeFileSync(path.join(labelsFolderPath, self.labelConfig.fileName), self.labels);
44
+ resolve();
45
+ })
46
+ .catch(function (error) {
47
+ if (error.statusCode === 401) {
48
+ log(self.config, 'You are not allowed to export label, Unless you provide email and password in config', 'error');
49
+ return resolve();
50
+ }
51
+ log(self.config, `Failed to export labels. ${formatError(error)}`, 'error');
52
+ reject();
53
+ });
54
+ });
55
+ }
56
+ };
@@ -0,0 +1,23 @@
1
+ export = LocaleExport;
2
+ declare class LocaleExport {
3
+ constructor(exportConfig: any, stackAPIClient: any);
4
+ stackAPIClient: any;
5
+ exportConfig: any;
6
+ localeConfig: any;
7
+ masterLocaleConfig: any;
8
+ qs: {
9
+ include_count: boolean;
10
+ asc: string;
11
+ only: {
12
+ BASE: any;
13
+ };
14
+ };
15
+ localesPath: string;
16
+ locales: {};
17
+ masterLocale: {};
18
+ fetchConcurrency: any;
19
+ writeConcurrency: any;
20
+ start(): Promise<void>;
21
+ getLocales(skip?: number): any;
22
+ sanitizeAttribs(locales: any): void;
23
+ }