@contentstack/cli-cm-export 1.5.9 → 1.6.1

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 (97) 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 +393 -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/index.d.ts +3 -0
  15. package/lib/export/modules/index.js +38 -0
  16. package/lib/export/modules/locales.d.ts +16 -0
  17. package/lib/export/modules/locales.js +68 -0
  18. package/lib/export/modules-js/assets.d.ts +43 -0
  19. package/lib/export/modules-js/assets.js +391 -0
  20. package/lib/export/modules-js/content-types.d.ts +21 -0
  21. package/lib/export/modules-js/content-types.js +75 -0
  22. package/lib/export/modules-js/custom-roles.d.ts +21 -0
  23. package/lib/export/modules-js/custom-roles.js +76 -0
  24. package/lib/export/modules-js/entries.d.ts +18 -0
  25. package/lib/export/modules-js/entries.js +141 -0
  26. package/lib/export/modules-js/environments.d.ts +16 -0
  27. package/lib/export/modules-js/environments.js +62 -0
  28. package/lib/export/modules-js/extensions.d.ts +18 -0
  29. package/lib/export/modules-js/extensions.js +57 -0
  30. package/lib/export/modules-js/global-fields.d.ts +22 -0
  31. package/lib/export/modules-js/global-fields.js +107 -0
  32. package/lib/export/modules-js/index.d.ts +1 -0
  33. package/lib/export/modules-js/index.js +33 -0
  34. package/lib/export/modules-js/labels.d.ts +14 -0
  35. package/lib/export/modules-js/labels.js +56 -0
  36. package/lib/export/modules-js/locales.d.ts +23 -0
  37. package/lib/export/modules-js/locales.js +67 -0
  38. package/lib/export/modules-js/marketplace-apps.d.ts +21 -0
  39. package/lib/export/modules-js/marketplace-apps.js +144 -0
  40. package/lib/export/modules-js/stack.d.ts +18 -0
  41. package/lib/export/modules-js/stack.js +91 -0
  42. package/lib/export/modules-js/webhooks.d.ts +18 -0
  43. package/lib/export/modules-js/webhooks.js +60 -0
  44. package/lib/export/modules-js/workflows.d.ts +16 -0
  45. package/lib/export/modules-js/workflows.js +89 -0
  46. package/lib/types/default-config.d.ts +158 -0
  47. package/lib/types/default-config.js +2 -0
  48. package/lib/types/export-config.d.ts +34 -0
  49. package/lib/types/export-config.js +2 -0
  50. package/lib/types/index.d.ts +45 -0
  51. package/lib/types/index.js +2 -0
  52. package/lib/utils/basic-login.d.ts +8 -0
  53. package/lib/utils/basic-login.js +45 -0
  54. package/lib/utils/common-helper.d.ts +11 -0
  55. package/lib/utils/common-helper.js +78 -0
  56. package/lib/utils/export-config-handler.d.ts +3 -0
  57. package/lib/utils/export-config-handler.js +72 -0
  58. package/lib/utils/file-helper.d.ts +14 -0
  59. package/lib/utils/file-helper.js +120 -0
  60. package/lib/utils/index.d.ts +10 -0
  61. package/lib/utils/index.js +21 -0
  62. package/lib/utils/interactive.d.ts +6 -0
  63. package/lib/utils/interactive.js +71 -0
  64. package/lib/utils/logger.d.ts +8 -0
  65. package/lib/utils/logger.js +154 -0
  66. package/lib/utils/marketplace-app-helper.d.ts +1 -0
  67. package/lib/utils/marketplace-app-helper.js +23 -0
  68. package/lib/utils/setup-branches.d.ts +3 -0
  69. package/lib/utils/setup-branches.js +49 -0
  70. package/lib/utils/setup-export-dir.d.ts +2 -0
  71. package/lib/utils/setup-export-dir.js +12 -0
  72. package/messages/index.json +1 -7
  73. package/oclif.manifest.json +1 -1
  74. package/package.json +37 -23
  75. package/src/app.js +0 -156
  76. package/src/commands/cm/stacks/export.js +0 -202
  77. package/src/config/default.js +0 -360
  78. package/src/lib/export/assets.js +0 -454
  79. package/src/lib/export/content-types.js +0 -90
  80. package/src/lib/export/custom-roles.js +0 -92
  81. package/src/lib/export/entries.js +0 -200
  82. package/src/lib/export/environments.js +0 -74
  83. package/src/lib/export/extensions.js +0 -69
  84. package/src/lib/export/global-fields.js +0 -122
  85. package/src/lib/export/labels.js +0 -67
  86. package/src/lib/export/locales.js +0 -72
  87. package/src/lib/export/marketplace-apps.js +0 -187
  88. package/src/lib/export/stack.js +0 -98
  89. package/src/lib/export/webhooks.js +0 -76
  90. package/src/lib/export/workflows.js +0 -106
  91. package/src/lib/util/export-flags.js +0 -193
  92. package/src/lib/util/helper.js +0 -113
  93. package/src/lib/util/index.js +0 -80
  94. package/src/lib/util/log.js +0 -161
  95. package/src/lib/util/login.js +0 -79
  96. package/src/lib/util/marketplace-app-helper.js +0 -24
  97. package/src/lib/util/setup-branches.js +0 -56
@@ -1,200 +0,0 @@
1
- const path = require('path');
2
- const chalk = require('chalk');
3
- const { addlogs } = require('../util/log');
4
- const fileHelper = require('../util/helper');
5
- const { executeTask, formatError } = require('../util');
6
-
7
- class EntriesExport {
8
- constructor(exportConfig, stackAPIClient) {
9
- this.stackAPIClient = stackAPIClient;
10
- this.exportConfig = exportConfig;
11
- this.entriesConfig = exportConfig.modules.entries;
12
- this.entriesRootPath = path.resolve(exportConfig.data, exportConfig.branchName || '', this.entriesConfig.dirName);
13
- this.localesFilePath = path.resolve(
14
- exportConfig.data,
15
- exportConfig.branchName || '',
16
- exportConfig.modules.locales.dirName,
17
- exportConfig.modules.locales.fileName,
18
- );
19
- this.schemaFilePath = path.resolve(
20
- exportConfig.data,
21
- exportConfig.branchName || '',
22
- exportConfig.modules.content_types.dirName,
23
- 'schema.json',
24
- );
25
- this.fetchConcurrency = this.entriesConfig.fetchConcurrency || exportConfig.fetchConcurrency;
26
- this.writeConcurrency = this.entriesConfig.writeConcurrency || exportConfig.writeConcurrency;
27
- }
28
-
29
- async start() {
30
- try {
31
- addlogs(this.exportConfig, 'Starting entries export', 'info');
32
- const locales = await fileHelper.readFile(this.localesFilePath);
33
- const contentTypes = await fileHelper.readFile(this.schemaFilePath);
34
- if (contentTypes.length === 0) {
35
- addlogs(this.exportConfig, 'No content types found to export entries', 'info');
36
- return;
37
- }
38
- const entryRequestOptions = this.createRequestObjects(locales, contentTypes);
39
- for (let requestOption of entryRequestOptions) {
40
- addlogs(
41
- this.exportConfig,
42
- `Starting export of entries of content_type - ${requestOption.content_type} locale - ${requestOption.locale}`,
43
- 'info',
44
- );
45
- await fileHelper.makeDirectory(path.join(this.entriesRootPath, requestOption.content_type));
46
- const entries = await this.getEntries(requestOption);
47
- let entriesFilePath = path.join(
48
- this.entriesRootPath,
49
- requestOption.content_type,
50
- requestOption.locale + '.json',
51
- );
52
- await fileHelper.writeLargeFile(entriesFilePath, entries);
53
- addlogs(
54
- this.exportConfig,
55
- `Exported entries of type '${requestOption.content_type}' locale '${requestOption.locale}'`,
56
- 'success',
57
- );
58
- if (this.exportConfig.versioning) {
59
- addlogs(
60
- this.exportConfig,
61
- `Started export versioned entries of type '${requestOption.content_type}' locale '${requestOption.locale}'`,
62
- 'info',
63
- );
64
- for (let entry of entries) {
65
- const versionedEntries = await this.getEntryByVersion(
66
- {
67
- ...requestOption,
68
- uid: entry.uid,
69
- },
70
- entry._version,
71
- );
72
- let versionedEntryPath = path.join(
73
- this.entriesRootPath,
74
- requestOption.locale,
75
- requestOption.content_type,
76
- entry.uid,
77
- );
78
- await fileHelper.makeDirectory(versionedEntryPath);
79
- if (versionedEntries.length > 0) {
80
- const write = (versionedEntry) =>
81
- fileHelper.writeFile(
82
- path.join(versionedEntryPath, 'version-' + versionedEntry._version + '.json'),
83
- versionedEntry,
84
- );
85
- await executeTask(versionedEntries, write.bind(this), { concurrency: this.writeConcurrency });
86
- addlogs(
87
- this.exportConfig,
88
- `Exported versioned entries of type '${requestOption.content_type}' locale '${requestOption.locale}'`,
89
- 'success',
90
- );
91
- }
92
- }
93
- }
94
- }
95
- addlogs(this.exportConfig, chalk.green('Entries exported successfully'), 'success');
96
- } catch (error) {
97
- addlogs(this.exportConfig, `Failed to export entries ${formatError(error)}`, 'error');
98
- throw new Error('Failed to export entries');
99
- }
100
- }
101
-
102
- async getEntries(requestOptions, skip = 0, entries = {}) {
103
- let requestObject = {
104
- locale: requestOptions.locale,
105
- skip,
106
- limit: this.entriesConfig.limit,
107
- include_count: true,
108
- include_publish_details: true,
109
- query: {
110
- locale: requestOptions.locale,
111
- },
112
- };
113
-
114
- const entriesSearchResponse = await this.stackAPIClient
115
- .contentType(requestOptions.content_type)
116
- .entry()
117
- .query(requestObject)
118
- .find();
119
-
120
- if (Array.isArray(entriesSearchResponse.items) && entriesSearchResponse.items.length > 0) {
121
- // clean up attribs and add to parent entry list
122
- this.sanitizeAttribs(entriesSearchResponse.items, entries);
123
- skip += this.entriesConfig.limit || 100;
124
- if (skip > entriesSearchResponse.count) {
125
- return entries;
126
- }
127
- return await this.getEntries(requestOptions, skip, entries);
128
- }
129
- return entries;
130
- }
131
-
132
- async getEntryByVersion(requestOptions, version, entries = []) {
133
- const queryRequestObject = {
134
- locale: requestOptions.locale,
135
- except: {
136
- BASE: this.entriesConfig.invalidKeys,
137
- },
138
- version,
139
- };
140
- const entryResponse = await this.stackAPIClient
141
- .contentType(requestOptions.content_type)
142
- .entry(requestOptions.uid)
143
- .fetch(queryRequestObject);
144
- entries.push(entryResponse);
145
- if (--version > 0) {
146
- return await this.getEntryByVersion(requestOptions, version, entries);
147
- }
148
- return entries;
149
- }
150
-
151
- async getEntriesCount(requestOptions) {
152
- let requestObject = {
153
- locale: requestOptions.locale,
154
- limit: 1,
155
- include_count: true,
156
- include_publish_details: true,
157
- query: {
158
- locale: requestOptions.locale,
159
- },
160
- };
161
-
162
- const entriesSearchResponse = await this.stackAPIClient
163
- .contentType(requestOptions.content_type)
164
- .entry()
165
- .query(requestObject)
166
- .find();
167
-
168
- return entriesSearchResponse.count;
169
- }
170
-
171
- createRequestObjects(locales, contentTypes) {
172
- let requestObjects = [];
173
- contentTypes.forEach((contentType) => {
174
- if (Object.keys(locales).length !== 0) {
175
- for (let locale in locales) {
176
- requestObjects.push({
177
- content_type: contentType.uid,
178
- locale: locales[locale].code,
179
- });
180
- }
181
- }
182
- requestObjects.push({
183
- content_type: contentType.uid,
184
- locale: this.exportConfig.master_locale.code,
185
- });
186
- });
187
-
188
- return requestObjects;
189
- }
190
-
191
- sanitizeAttribs(entries, entriesList = {}) {
192
- entries.forEach((entry) => {
193
- this.entriesConfig.invalidKeys.forEach((key) => delete entry[key]);
194
- entriesList[entry.uid] = entry;
195
- });
196
- return entriesList;
197
- }
198
- }
199
-
200
- module.exports = EntriesExport;
@@ -1,74 +0,0 @@
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
-
11
- const helper = require('../util/helper');
12
- const { addlogs } = require('../util/log');
13
- const { formatError } = require('../util');
14
-
15
- module.exports = class ExportEnvironments {
16
- config = {};
17
- master = {};
18
- environments = {};
19
- requestOptions = {
20
- json: true,
21
- qs: {
22
- asc: 'updated_at',
23
- include_count: true,
24
- },
25
- };
26
-
27
- constructor(exportConfig, stackAPIClient) {
28
- this.config = merge(this.config, exportConfig);
29
- this.stackAPIClient = stackAPIClient;
30
- }
31
-
32
- start() {
33
- const self = this;
34
- const environmentConfig = self.config.modules.environments;
35
- const environmentsFolderPath = path.resolve(
36
- self.config.data,
37
- self.config.branchName || '',
38
- environmentConfig.dirName,
39
- );
40
-
41
- // Create folder for environments
42
- mkdirp.sync(environmentsFolderPath);
43
- addlogs(self.config, 'Starting environment export', 'success');
44
-
45
- return new Promise((resolve, reject) => {
46
- self.stackAPIClient
47
- .environment()
48
- .query(self.requestOptions.qs)
49
- .find()
50
- .then((environmentResponse) => {
51
- if (environmentResponse.items.length !== 0) {
52
- for (let i = 0, total = environmentResponse.count; i < total; i++) {
53
- const envUid = environmentResponse.items[i].uid;
54
- self.master[envUid] = '';
55
- self.environments[envUid] = environmentResponse.items[i];
56
- delete self.environments[envUid].uid;
57
- delete self.environments[envUid]['ACL'];
58
- }
59
- helper.writeFileSync(path.join(environmentsFolderPath, environmentConfig.fileName), self.environments);
60
- addlogs(self.config, chalk.green('All the environments have been exported successfully'), 'success');
61
- return resolve();
62
- }
63
- if (environmentResponse.items.length === 0) {
64
- addlogs(self.config, 'No environments found', 'success');
65
- resolve();
66
- }
67
- })
68
- .catch((error) => {
69
- addlogs(self.config, `Environments export failed. ${formatError(error)}`, 'error');
70
- reject(error);
71
- });
72
- });
73
- }
74
- };
@@ -1,69 +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 chalk = require('chalk');
10
- const { merge } = require('lodash');
11
-
12
- const helper = require('../util/helper');
13
- const { addlogs } = require('../util/log');
14
- const { formatError } = require('../util');
15
- let config = require('../../config/default');
16
-
17
- module.exports = class ExportExtensions {
18
- master = {};
19
- extensions = {};
20
- extensionConfig = config.modules.extensions;
21
- queryRequestOptions = {
22
- asc: 'updated_at',
23
- include_count: true,
24
- };
25
-
26
- constructor(exportConfig, stackAPIClient) {
27
- this.config = merge(config, exportConfig);
28
- this.stackAPIClient = stackAPIClient;
29
- }
30
-
31
- start() {
32
- addlogs(this.config, 'Starting extension export', 'success');
33
-
34
- const self = this;
35
- const extensionsFolderPath = path.resolve(
36
- this.config.data,
37
- this.config.branchName || '',
38
- this.extensionConfig.dirName,
39
- );
40
- // Create folder for extensions
41
- mkdirp.sync(extensionsFolderPath);
42
- return new Promise(function (resolve, reject) {
43
- self.stackAPIClient
44
- .extension()
45
- .query(self.queryRequestOptions)
46
- .find()
47
- .then((extension) => {
48
- if (extension.items.length !== 0) {
49
- for (let i = 0, total = extension.count; i < total; i++) {
50
- const extUid = extension.items[i].uid;
51
- self.master[extUid] = '';
52
- self.extensions[extUid] = extension.items[i];
53
- delete self.extensions[extUid].uid;
54
- delete self.extensions[extUid].SYS_ACL;
55
- }
56
- helper.writeFileSync(path.join(extensionsFolderPath, self.extensionConfig.fileName), self.extensions);
57
- addlogs(self.config, chalk.green('All the extensions have been exported successfully'), 'success');
58
- return resolve();
59
- }
60
- addlogs(self.config, 'No extensions found', 'success');
61
- resolve();
62
- })
63
- .catch((error) => {
64
- addlogs(self.config, `Failed to export extensions. ${formatError(error)}`, 'error');
65
- reject();
66
- });
67
- });
68
- }
69
- };
@@ -1,122 +0,0 @@
1
- /*!
2
- * Contentstack Export
3
- * Copyright (c) 2019 Contentstack LLC
4
- * MIT Licensed
5
- */
6
-
7
- const path = require('path');
8
- const chalk = require('chalk');
9
- const mkdirp = require('mkdirp');
10
- const { merge } = require('lodash');
11
-
12
- const helper = require('../util/helper');
13
- const { addlogs } = require('../util/log');
14
- const { formatError } = require('../util');
15
- let config = require('../../config/default');
16
-
17
- module.exports = class ExportGlobalFields {
18
- limit = 100;
19
- config = {};
20
- global_fields = [];
21
- master = {};
22
- globalfields = {};
23
- requestOptions = {};
24
- globalfieldsFolderPath;
25
- globalfieldsConfig = config.modules.globalfields;
26
- validKeys = config.modules.globalfields.validKeys;
27
-
28
- constructor(exportConfig, stackAPIClient) {
29
- this.requestOptions = {
30
- qs: {
31
- skip: 0,
32
- limit: this.limit,
33
- asc: 'updated_at',
34
- include_count: true,
35
- },
36
- };
37
- this.config = merge(config, exportConfig);
38
- this.stackAPIClient = stackAPIClient;
39
- this.globalfieldsFolderPath = path.resolve(
40
- this.config.data,
41
- this.config.branchName || '',
42
- this.globalfieldsConfig.dirName,
43
- );
44
- }
45
-
46
- start() {
47
- const self = this;
48
- // Create folder for Global Fields
49
- mkdirp.sync(self.globalfieldsFolderPath);
50
- addlogs(self.config, 'Starting Global Fields export', 'success');
51
-
52
- return new Promise(function (resolve, reject) {
53
- try {
54
- return self
55
- .getGlobalFields(0, self.config)
56
- .then(function (result) {
57
- if (!result) {
58
- return self.writeGlobalFields().then(resolve).catch(reject);
59
- }
60
- return resolve();
61
- })
62
- .catch(reject);
63
- } catch (error) {
64
- addlogs(self.config, error, 'error');
65
- return reject(error);
66
- }
67
- });
68
- }
69
-
70
- getGlobalFields(skip, globalFieldConfig) {
71
- const self = this;
72
- self.requestOptions.qs.skip = skip;
73
- return new Promise(function (resolve, reject) {
74
- self.stackAPIClient
75
- .globalField()
76
- .query(self.requestOptions.qs)
77
- .find()
78
- .then((globalFieldResponse) => {
79
- try {
80
- if (globalFieldResponse.items.length === 0) {
81
- addlogs(globalFieldConfig, 'No global fields found', 'success');
82
- return resolve('No Global Fields');
83
- }
84
- globalFieldResponse.items.forEach(function (globalField) {
85
- for (const key in globalField) {
86
- if (self.validKeys.indexOf(key) === -1) {
87
- delete globalField[key];
88
- }
89
- }
90
- self.global_fields.push(globalField);
91
- });
92
- skip += self.limit;
93
- if (skip >= globalFieldResponse.count) {
94
- return resolve();
95
- }
96
- return self.getGlobalFields(skip, globalFieldConfig).then(resolve).catch(reject);
97
- } catch (error) {
98
- addlogs(globalFieldConfig, `Failed to export global-fields. ${formatError(error)}`, 'error');
99
- reject(error);
100
- }
101
- })
102
- .catch(reject);
103
- });
104
- }
105
-
106
- writeGlobalFields() {
107
- const self = this;
108
- return new Promise(function (resolve, reject) {
109
- try {
110
- helper.writeFileSync(
111
- path.join(self.globalfieldsFolderPath, self.globalfieldsConfig.fileName),
112
- self.global_fields,
113
- );
114
- addlogs(self.config, chalk.green('Global Fields export completed successfully'), 'success');
115
- resolve();
116
- } catch (error) {
117
- addlogs(self.config, error, 'error');
118
- reject(error);
119
- }
120
- });
121
- }
122
- };
@@ -1,67 +0,0 @@
1
- /*!
2
- * Contentstack Export
3
- * Copyright (c) 2019 Contentstack LLC
4
- * MIT Licensed
5
- */
6
-
7
- const path = require('path');
8
- const chalk = require('chalk');
9
- const mkdirp = require('mkdirp');
10
- const { merge } = require('lodash');
11
-
12
- const helper = require('../util/helper');
13
- const { addlogs } = require('../util/log');
14
- const { formatError } = require('../util');
15
- const config = require('../../config/default');
16
-
17
- module.exports = class ExportLabels {
18
- labels = {};
19
- labelConfig = config.modules.labels;
20
-
21
- constructor(exportConfig, stackAPIClient) {
22
- this.config = merge(config, exportConfig);
23
- this.stackAPIClient = stackAPIClient;
24
- }
25
-
26
- start() {
27
- addlogs(this.config, 'Starting labels export', 'success');
28
- const self = this;
29
- const labelsFolderPath = path.resolve(config.data, this.config.branchName || '', self.labelConfig.dirName);
30
- // Create locale folder
31
- mkdirp.sync(labelsFolderPath);
32
- return new Promise(function (resolve, reject) {
33
- return self.stackAPIClient
34
- .label()
35
- .query()
36
- .find()
37
- .then((response) => {
38
- if (response.items.length !== 0) {
39
- response.items.forEach(function (label) {
40
- addlogs(self.config, `'${label.name}' label was exported successfully`, 'success');
41
- self.labels[label.uid] = label;
42
- const deleteItems = self.config.modules.labels.invalidKeys;
43
- deleteItems.forEach((e) => delete label[e]);
44
- });
45
- addlogs(self.config, chalk.green('All the labels have been exported successfully'), 'success');
46
- } else {
47
- addlogs(self.config, 'No labels found', 'success');
48
- }
49
- helper.writeFileSync(path.join(labelsFolderPath, self.labelConfig.fileName), self.labels);
50
- resolve();
51
- })
52
- .catch(function (error) {
53
- if (error.statusCode === 401) {
54
- addlogs(
55
- self.config,
56
- 'You are not allowed to export label, Unless you provide email and password in config',
57
- 'error'
58
- );
59
- return resolve();
60
- }
61
-
62
- addlogs(self.config, `Failed to export labels. ${formatError(error)}`, 'error');
63
- reject();
64
- });
65
- });
66
- }
67
- };
@@ -1,72 +0,0 @@
1
- const path = require('path');
2
- const chalk = require('chalk');
3
- const fileHelper = require('../util/helper');
4
- const { addlogs } = require('../util/log');
5
- const { formatError } = require('../util');
6
- class LocaleExport {
7
- constructor(exportConfig, stackAPIClient) {
8
- this.stackAPIClient = stackAPIClient;
9
- this.exportConfig = exportConfig;
10
- this.localeConfig = exportConfig.modules.locales;
11
- this.masterLocaleConfig = exportConfig.modules.masterLocale;
12
- this.qs = {
13
- include_count: true,
14
- asc: 'updated_at',
15
- only: {
16
- BASE: this.localeConfig.requiredKeys,
17
- },
18
- };
19
-
20
- this.localesPath = path.resolve(exportConfig.data, exportConfig.branchName || '', this.localeConfig.dirName);
21
- this.locales = {};
22
- this.masterLocale = {};
23
- this.fetchConcurrency = this.localeConfig.fetchConcurrency || this.exportConfig.fetchConcurrency;
24
- this.writeConcurrency = this.localeConfig.writeConcurrency || this.exportConfig.writeConcurrency;
25
- }
26
-
27
- async start() {
28
- try {
29
- addlogs(this.exportConfig, 'Starting locale export', 'success');
30
- await fileHelper.makeDirectory(this.localesPath);
31
- await this.getLocales();
32
- await fileHelper.writeFile(path.join(this.localesPath, this.localeConfig.fileName), this.locales);
33
- await fileHelper.writeFile(path.join(this.localesPath, this.masterLocaleConfig.fileName), this.masterLocale);
34
- addlogs(this.exportConfig, 'Completed locale export', 'success');
35
- } catch (error) {
36
- addlogs(this.exportConfig, `Failed to export locales. ${formatError(error)}`, 'error');
37
- throw new Error('Failed to export locales');
38
- }
39
- }
40
-
41
- async getLocales(skip = 0) {
42
- if (skip) {
43
- this.qs.skip = skip;
44
- }
45
- let localesFetchResponse = await this.stackAPIClient.locale().query(this.qs).find();
46
- if (Array.isArray(localesFetchResponse.items) && localesFetchResponse.items.length > 0) {
47
- this.sanitizeAttribs(localesFetchResponse.items);
48
- skip += this.localeConfig.limit || 100;
49
- if (skip > localesFetchResponse.count) {
50
- return;
51
- }
52
- return await this.getLocales(skip);
53
- }
54
- }
55
-
56
- sanitizeAttribs(locales) {
57
- locales.forEach((locale) => {
58
- for (let key in locale) {
59
- if (this.localeConfig.requiredKeys.indexOf(key) === -1) {
60
- delete locale[key];
61
- }
62
- }
63
- if(locale.code === this.exportConfig.master_locale.code){
64
- this.masterLocale[locale.uid] = locale;
65
- }else{
66
- this.locales[locale.uid] = locale;
67
- }
68
- });
69
- }
70
- }
71
-
72
- module.exports = LocaleExport;