@contentstack/cli-cm-export-to-csv 1.10.2 → 2.0.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/bin/dev.cmd +2 -0
- package/bin/dev.js +7 -0
- package/bin/run.cmd +2 -0
- package/bin/run.js +7 -0
- package/package.json +41 -30
- package/src/commands/cm/export-to-csv.js +0 -523
- package/src/util/client.js +0 -0
- package/src/util/config.js +0 -13
- package/src/util/index.js +0 -1313
package/LICENSE
CHANGED
package/bin/dev.cmd
ADDED
package/bin/dev.js
ADDED
package/bin/run.cmd
ADDED
package/bin/run.js
ADDED
package/package.json
CHANGED
|
@@ -1,60 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentstack/cli-cm-export-to-csv",
|
|
3
|
-
"description": "Export
|
|
4
|
-
"version": "
|
|
5
|
-
"author": "
|
|
3
|
+
"description": "Export entries, taxonomies, terms, or organization users to CSV",
|
|
4
|
+
"version": "2.0.0-beta",
|
|
5
|
+
"author": "Contentstack",
|
|
6
6
|
"bugs": "https://github.com/contentstack/cli/issues",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@contentstack/cli-command": "~
|
|
9
|
-
"@contentstack/cli-utilities": "~
|
|
10
|
-
"@oclif/core": "^4.
|
|
8
|
+
"@contentstack/cli-command": "~2.0.0-beta",
|
|
9
|
+
"@contentstack/cli-utilities": "~2.0.0-beta",
|
|
10
|
+
"@oclif/core": "^4.8.0",
|
|
11
11
|
"@oclif/plugin-help": "^6.2.32",
|
|
12
12
|
"fast-csv": "^4.3.6",
|
|
13
13
|
"inquirer": "8.2.7",
|
|
14
|
-
"inquirer-checkbox-plus-prompt": "1.4.2"
|
|
15
|
-
"mkdirp": "^3.0.1"
|
|
14
|
+
"inquirer-checkbox-plus-prompt": "1.4.2"
|
|
16
15
|
},
|
|
17
16
|
"devDependencies": {
|
|
18
17
|
"@oclif/test": "^4.1.13",
|
|
19
18
|
"@types/chai": "^4.3.20",
|
|
19
|
+
"@types/inquirer": "^9.0.8",
|
|
20
20
|
"@types/mocha": "^10.0.10",
|
|
21
|
+
"@types/node": "^20.17.50",
|
|
21
22
|
"chai": "^4.5.0",
|
|
22
|
-
"
|
|
23
|
-
"eslint": "^
|
|
24
|
-
"eslint-config-oclif": "^
|
|
23
|
+
"eslint": "^8.57.1",
|
|
24
|
+
"eslint-config-oclif": "^6.0.62",
|
|
25
|
+
"eslint-config-oclif-typescript": "^3.1.14",
|
|
25
26
|
"mocha": "^10.8.2",
|
|
27
|
+
"nock": "^13.5.6",
|
|
26
28
|
"nyc": "^15.1.0",
|
|
27
|
-
"oclif": "^4.17.46"
|
|
29
|
+
"oclif": "^4.17.46",
|
|
30
|
+
"sinon": "^21.0.1",
|
|
31
|
+
"ts-node": "^10.9.2",
|
|
32
|
+
"typescript": "^5.8.3"
|
|
28
33
|
},
|
|
29
34
|
"engines": {
|
|
30
|
-
"node": ">=
|
|
35
|
+
"node": ">=18.0.0"
|
|
31
36
|
},
|
|
32
37
|
"files": [
|
|
38
|
+
"/bin",
|
|
39
|
+
"/lib",
|
|
33
40
|
"/npm-shrinkwrap.json",
|
|
34
|
-
"/oclif.manifest.json"
|
|
35
|
-
"/src",
|
|
36
|
-
"/yarn.lock"
|
|
41
|
+
"/oclif.manifest.json"
|
|
37
42
|
],
|
|
38
43
|
"homepage": "https://github.com/contentstack/cli",
|
|
39
44
|
"keywords": [
|
|
40
45
|
"contentstack",
|
|
41
46
|
"cli",
|
|
42
|
-
"plugin"
|
|
47
|
+
"plugin",
|
|
48
|
+
"export",
|
|
49
|
+
"csv"
|
|
43
50
|
],
|
|
44
|
-
"scripts": {
|
|
45
|
-
"pack": "npm pack && mv *.tgz ../../build",
|
|
46
|
-
"postpack": "rm -f oclif.manifest.json",
|
|
47
|
-
"prepack": "oclif manifest && oclif readme",
|
|
48
|
-
"test": "nyc mocha --forbid-only \"test/**/*.test.js\"",
|
|
49
|
-
"test:unit": "mocha --timeout 10000 --forbid-only \"test/unit/**/*.test.js\" \"test/util/*.test.js\"",
|
|
50
|
-
"test:unit:report": "nyc --extension .js mocha --forbid-only \"test/unit/**/*.test.js\" \"test/util/*.test.js\"",
|
|
51
|
-
"version": "oclif readme && git add README.md",
|
|
52
|
-
"clean": "rm -rf ./node_modules tsconfig.build.tsbuildinfo"
|
|
53
|
-
},
|
|
54
|
-
"main": "./src/commands/cm/export-to-csv.js",
|
|
55
51
|
"license": "MIT",
|
|
52
|
+
"main": "./lib/index.js",
|
|
53
|
+
"types": "./lib/index.d.ts",
|
|
56
54
|
"oclif": {
|
|
57
|
-
"commands": "./
|
|
55
|
+
"commands": "./lib/commands",
|
|
58
56
|
"bin": "csdx",
|
|
59
57
|
"repositoryPrefix": "<%- repo %>/blob/main/packages/contentstack-export-to-csv/<%- commandPath %>"
|
|
60
58
|
},
|
|
@@ -63,5 +61,18 @@
|
|
|
63
61
|
"cm:export-to-csv": "EXPRTCSV"
|
|
64
62
|
}
|
|
65
63
|
},
|
|
66
|
-
"repository": "https://github.com/contentstack/cli"
|
|
64
|
+
"repository": "https://github.com/contentstack/cli",
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "npm run clean && npm run compile",
|
|
67
|
+
"clean": "rm -rf ./lib ./node_modules tsconfig.tsbuildinfo oclif.manifest.json",
|
|
68
|
+
"compile": "tsc -b tsconfig.json",
|
|
69
|
+
"lint": "eslint src/**/*.ts",
|
|
70
|
+
"lint:fix": "eslint src/**/*.ts --fix",
|
|
71
|
+
"postpack": "rm -f oclif.manifest.json",
|
|
72
|
+
"prepack": "pnpm compile && oclif manifest && oclif readme",
|
|
73
|
+
"test": "nyc mocha --forbid-only \"test/**/*.test.ts\"",
|
|
74
|
+
"test:unit": "mocha --timeout 10000 --forbid-only \"test/unit/**/*.test.ts\"",
|
|
75
|
+
"test:unit:report": "nyc --extension .ts mocha --forbid-only \"test/unit/**/*.test.ts\"",
|
|
76
|
+
"version": "oclif readme && git add README.md"
|
|
77
|
+
}
|
|
67
78
|
}
|
|
@@ -1,523 +0,0 @@
|
|
|
1
|
-
const { Command } = require('@contentstack/cli-command');
|
|
2
|
-
const {
|
|
3
|
-
configHandler,
|
|
4
|
-
managementSDKClient,
|
|
5
|
-
flags,
|
|
6
|
-
isAuthenticated,
|
|
7
|
-
cliux,
|
|
8
|
-
doesBranchExist,
|
|
9
|
-
isManagementTokenValid,
|
|
10
|
-
} = require('@contentstack/cli-utilities');
|
|
11
|
-
const util = require('../../util');
|
|
12
|
-
const config = require('../../util/config');
|
|
13
|
-
|
|
14
|
-
class ExportToCsvCommand extends Command {
|
|
15
|
-
static flags = {
|
|
16
|
-
action: flags.string({
|
|
17
|
-
required: false,
|
|
18
|
-
multiple: false,
|
|
19
|
-
options: ['entries', 'users', 'teams', 'taxonomies'],
|
|
20
|
-
description:
|
|
21
|
-
'Option to export data (entries, users, teams, taxonomies). <options: entries|users|teams|taxonomies>',
|
|
22
|
-
}),
|
|
23
|
-
alias: flags.string({
|
|
24
|
-
char: 'a',
|
|
25
|
-
description: 'Alias of the management token.',
|
|
26
|
-
}),
|
|
27
|
-
org: flags.string({
|
|
28
|
-
multiple: false,
|
|
29
|
-
required: false,
|
|
30
|
-
description: 'Provide organization UID to clone org users.',
|
|
31
|
-
}),
|
|
32
|
-
'stack-name': flags.string({
|
|
33
|
-
char: 'n',
|
|
34
|
-
multiple: false,
|
|
35
|
-
required: false,
|
|
36
|
-
description: 'Name of the stack that needs to be created as CSV filename.',
|
|
37
|
-
}),
|
|
38
|
-
'stack-api-key': flags.string({
|
|
39
|
-
char: 'k',
|
|
40
|
-
multiple: false,
|
|
41
|
-
required: false,
|
|
42
|
-
description: 'API Key of the source stack.',
|
|
43
|
-
}),
|
|
44
|
-
'org-name': flags.string({
|
|
45
|
-
multiple: false,
|
|
46
|
-
required: false,
|
|
47
|
-
description: 'Name of the organization that needs to be created as CSV filename.',
|
|
48
|
-
}),
|
|
49
|
-
locale: flags.string({
|
|
50
|
-
required: false,
|
|
51
|
-
multiple: false,
|
|
52
|
-
description: 'Locale of entries that will be exported.',
|
|
53
|
-
}),
|
|
54
|
-
'content-type': flags.string({
|
|
55
|
-
description: 'Content type of entries that will be exported.',
|
|
56
|
-
required: false,
|
|
57
|
-
multiple: false,
|
|
58
|
-
}),
|
|
59
|
-
branch: flags.string({
|
|
60
|
-
description: 'Branch from which entries will be exported.',
|
|
61
|
-
multiple: false,
|
|
62
|
-
required: false,
|
|
63
|
-
}),
|
|
64
|
-
'team-uid': flags.string({
|
|
65
|
-
description: 'Provide the UID of a specific team in an organization.',
|
|
66
|
-
}),
|
|
67
|
-
'taxonomy-uid': flags.string({
|
|
68
|
-
description: 'Provide the taxonomy UID of the related terms you want to export.',
|
|
69
|
-
}),
|
|
70
|
-
'include-fallback': flags.boolean({
|
|
71
|
-
description:
|
|
72
|
-
"[Optional] Include fallback locale data when exporting taxonomies. When enabled, if a taxonomy term doesn't exist in the specified locale, it will fallback to the hierarchy defined in the branch settings.",
|
|
73
|
-
default: false,
|
|
74
|
-
}),
|
|
75
|
-
'fallback-locale': flags.string({
|
|
76
|
-
description:
|
|
77
|
-
"[Optional] Specify a specific fallback locale for taxonomy export. This locale will be used when a taxonomy term doesn't exist in the primary locale. Takes priority over branch fallback hierarchy when both are specified.",
|
|
78
|
-
required: false,
|
|
79
|
-
}),
|
|
80
|
-
delimiter: flags.string({
|
|
81
|
-
description:
|
|
82
|
-
"[optional] Provide a delimiter to separate individual data fields within the CSV file. For example: cm:export-to-csv --delimiter '|'",
|
|
83
|
-
default: ',',
|
|
84
|
-
}),
|
|
85
|
-
};
|
|
86
|
-
async run() {
|
|
87
|
-
try {
|
|
88
|
-
let action, managementAPIClient;
|
|
89
|
-
const {
|
|
90
|
-
flags: {
|
|
91
|
-
org,
|
|
92
|
-
action: actionFlag,
|
|
93
|
-
'org-name': orgName,
|
|
94
|
-
'stack-name': stackName,
|
|
95
|
-
'stack-api-key': stackAPIKey,
|
|
96
|
-
locale: locale,
|
|
97
|
-
'content-type': contentTypesFlag,
|
|
98
|
-
alias: managementTokenAlias,
|
|
99
|
-
branch: branchUid,
|
|
100
|
-
'team-uid': teamUid,
|
|
101
|
-
'taxonomy-uid': taxonomyUID,
|
|
102
|
-
'include-fallback': includeFallback,
|
|
103
|
-
'fallback-locale': fallbackLocale,
|
|
104
|
-
delimiter,
|
|
105
|
-
},
|
|
106
|
-
} = await this.parse(ExportToCsvCommand);
|
|
107
|
-
|
|
108
|
-
if (!managementTokenAlias) {
|
|
109
|
-
managementAPIClient = await managementSDKClient({ host: this.cmaHost });
|
|
110
|
-
if (!isAuthenticated()) {
|
|
111
|
-
this.error(config.CLI_EXPORT_CSV_ENTRIES_ERROR, {
|
|
112
|
-
exit: 2,
|
|
113
|
-
suggestions: ['https://www.contentstack.com/docs/developers/cli/authentication/'],
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (actionFlag) {
|
|
119
|
-
action = actionFlag;
|
|
120
|
-
} else {
|
|
121
|
-
action = await util.startupQuestions();
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
switch (action) {
|
|
125
|
-
case config.exportEntries:
|
|
126
|
-
case 'entries': {
|
|
127
|
-
try {
|
|
128
|
-
let stack;
|
|
129
|
-
let stackAPIClient;
|
|
130
|
-
let language;
|
|
131
|
-
let contentTypes = [];
|
|
132
|
-
|
|
133
|
-
if (managementTokenAlias) {
|
|
134
|
-
const { stackDetails, apiClient } = await this.getAliasDetails(managementTokenAlias, stackName);
|
|
135
|
-
managementAPIClient = apiClient;
|
|
136
|
-
stack = stackDetails;
|
|
137
|
-
} else {
|
|
138
|
-
stack = await this.getStackDetails(managementAPIClient, stackAPIKey, org);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
stackAPIClient = this.getStackClient(managementAPIClient, stack);
|
|
142
|
-
stackAPIClient = await this.checkAndUpdateBranchDetail(
|
|
143
|
-
branchUid,
|
|
144
|
-
stack,
|
|
145
|
-
stackAPIClient,
|
|
146
|
-
managementAPIClient,
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
const contentTypeCount = await util.getContentTypeCount(stackAPIClient);
|
|
150
|
-
|
|
151
|
-
const environments = await util.getEnvironments(stackAPIClient); // fetch environments, because in publish details only env uid are available and we need env names
|
|
152
|
-
|
|
153
|
-
if (contentTypesFlag) {
|
|
154
|
-
contentTypes = contentTypesFlag.split(',').map(this.snakeCase);
|
|
155
|
-
const contentTypesArray = await stackAPIClient
|
|
156
|
-
.contentType()
|
|
157
|
-
.query()
|
|
158
|
-
.find()
|
|
159
|
-
.then((res) => res.items.map((contentType) => contentType.uid));
|
|
160
|
-
|
|
161
|
-
const doesContentTypeExist = contentTypesArray.includes(contentTypesFlag);
|
|
162
|
-
|
|
163
|
-
if (!doesContentTypeExist) {
|
|
164
|
-
throw new Error(
|
|
165
|
-
`The Content Type ${contentTypesFlag} was not found. Please try again. Content Type is not valid.`,
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
} else {
|
|
169
|
-
for (let index = 0; index <= contentTypeCount / 100; index++) {
|
|
170
|
-
const contentTypesMap = await util.getContentTypes(stackAPIClient, index);
|
|
171
|
-
contentTypes = contentTypes.concat(Object.values(contentTypesMap)); // prompt for content Type
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
if (contentTypes.length <= 0) {
|
|
176
|
-
this.log('No content types found for the given stack');
|
|
177
|
-
this.exit();
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (!contentTypesFlag) {
|
|
181
|
-
contentTypes = await util.chooseInMemContentTypes(contentTypes);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (locale) {
|
|
185
|
-
language = { code: locale };
|
|
186
|
-
} else {
|
|
187
|
-
language = await util.chooseLanguage(stackAPIClient); // prompt for language
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
while (contentTypes.length > 0) {
|
|
191
|
-
let contentType = contentTypes.pop();
|
|
192
|
-
|
|
193
|
-
const entriesCount = await util.getEntriesCount(stackAPIClient, contentType, language.code);
|
|
194
|
-
let flatEntries = [];
|
|
195
|
-
for (let index = 0; index < entriesCount / 100; index++) {
|
|
196
|
-
const entriesResult = await util.getEntries(stackAPIClient, contentType, language.code, index, 100);
|
|
197
|
-
const flatEntriesResult = util.cleanEntries(
|
|
198
|
-
entriesResult.items,
|
|
199
|
-
language.code,
|
|
200
|
-
environments,
|
|
201
|
-
contentType,
|
|
202
|
-
);
|
|
203
|
-
flatEntries = flatEntries.concat(flatEntriesResult);
|
|
204
|
-
}
|
|
205
|
-
let fileName = `${stackName ? stackName : stack.name}_${contentType}_${language.code}_entries_export.csv`;
|
|
206
|
-
util.write(this, flatEntries, fileName, 'entries', delimiter); // write to file
|
|
207
|
-
}
|
|
208
|
-
} catch (error) {
|
|
209
|
-
cliux.error(util.formatError(error));
|
|
210
|
-
}
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
case config.exportUsers:
|
|
214
|
-
case 'users': {
|
|
215
|
-
try {
|
|
216
|
-
let organization;
|
|
217
|
-
|
|
218
|
-
if (org) {
|
|
219
|
-
organization = { uid: org, name: orgName || org };
|
|
220
|
-
} else {
|
|
221
|
-
organization = await util.chooseOrganization(managementAPIClient, action); // prompt for organization
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const orgUsers = await util.getOrgUsers(managementAPIClient, organization.uid, this);
|
|
225
|
-
const orgRoles = await util.getOrgRoles(managementAPIClient, organization.uid, this);
|
|
226
|
-
const mappedUsers = util.getMappedUsers(orgUsers);
|
|
227
|
-
const mappedRoles = util.getMappedRoles(orgRoles);
|
|
228
|
-
const listOfUsers = util.cleanOrgUsers(orgUsers, mappedUsers, mappedRoles);
|
|
229
|
-
const fileName = `${util.kebabize(
|
|
230
|
-
(orgName ? orgName : organization.name).replace(config.organizationNameRegex, ''),
|
|
231
|
-
)}_users_export.csv`;
|
|
232
|
-
|
|
233
|
-
util.write(this, listOfUsers, fileName, 'organization details', delimiter);
|
|
234
|
-
} catch (error) {
|
|
235
|
-
if (error.message || error.errorMessage) {
|
|
236
|
-
cliux.error(util.formatError(error));
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
break;
|
|
240
|
-
}
|
|
241
|
-
case config.exportTeams:
|
|
242
|
-
case 'teams': {
|
|
243
|
-
try {
|
|
244
|
-
let organization;
|
|
245
|
-
if (org) {
|
|
246
|
-
organization = { uid: org, name: orgName || org };
|
|
247
|
-
} else {
|
|
248
|
-
organization = await util.chooseOrganization(managementAPIClient, action); // prompt for organization
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
await util.exportTeams(managementAPIClient, organization, teamUid, delimiter);
|
|
252
|
-
} catch (error) {
|
|
253
|
-
if (error.message || error.errorMessage) {
|
|
254
|
-
cliux.error(util.formatError(error));
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
break;
|
|
258
|
-
}
|
|
259
|
-
case config.exportTaxonomies:
|
|
260
|
-
case 'taxonomies': {
|
|
261
|
-
let stack;
|
|
262
|
-
let language;
|
|
263
|
-
let stackAPIClient;
|
|
264
|
-
let finalIncludeFallback = includeFallback;
|
|
265
|
-
let finalFallbackLocale = fallbackLocale;
|
|
266
|
-
|
|
267
|
-
if (managementTokenAlias) {
|
|
268
|
-
const { stackDetails, apiClient } = await this.getAliasDetails(managementTokenAlias, stackName);
|
|
269
|
-
managementAPIClient = apiClient;
|
|
270
|
-
stack = stackDetails;
|
|
271
|
-
} else {
|
|
272
|
-
stack = await this.getStackDetails(managementAPIClient, stackAPIKey, org);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
stackAPIClient = this.getStackClient(managementAPIClient, stack);
|
|
276
|
-
if (locale) {
|
|
277
|
-
language = { code: locale };
|
|
278
|
-
} else {
|
|
279
|
-
language = await util.chooseLanguage(stackAPIClient);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// if (includeFallback === undefined || fallbackLocale === undefined) {
|
|
283
|
-
// const fallbackOptions = await util.chooseFallbackOptions(stackAPIClient);
|
|
284
|
-
// }
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
if (fallbackLocale !== undefined) {
|
|
288
|
-
finalFallbackLocale = fallbackLocale;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
await this.createTaxonomyAndTermCsvFile(stackAPIClient, stackName, stack, taxonomyUID, delimiter, {
|
|
292
|
-
locale: language.code,
|
|
293
|
-
branch: branchUid,
|
|
294
|
-
include_fallback: finalIncludeFallback,
|
|
295
|
-
fallback_locale: finalFallbackLocale,
|
|
296
|
-
});
|
|
297
|
-
break;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
} catch (error) {
|
|
301
|
-
if (error.message || error.errorMessage) {
|
|
302
|
-
cliux.error(util.formatError(error));
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
snakeCase(string) {
|
|
308
|
-
return (string || '').split(' ').join('_').toLowerCase();
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
getStackClient(managementAPIClient, stack) {
|
|
312
|
-
const stackInit = {
|
|
313
|
-
api_key: stack.apiKey,
|
|
314
|
-
};
|
|
315
|
-
if (stack?.branch_uid) stackInit['branch_uid'] = stack.branch_uid;
|
|
316
|
-
if (stack.token) {
|
|
317
|
-
return managementAPIClient.stack({
|
|
318
|
-
...stackInit,
|
|
319
|
-
management_token: stack.token,
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
return managementAPIClient.stack(stackInit);
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
getStackBranches(stackAPIClient) {
|
|
326
|
-
return stackAPIClient
|
|
327
|
-
.branch()
|
|
328
|
-
.query()
|
|
329
|
-
.find()
|
|
330
|
-
.then(({ items }) => (items !== undefined ? items : []))
|
|
331
|
-
.catch(() => {});
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* check whether branch enabled org or not and update branch details
|
|
336
|
-
* @param {string} branchUid
|
|
337
|
-
* @param {object} stack
|
|
338
|
-
* @param {*} stackAPIClient
|
|
339
|
-
* @param {*} managementAPIClient
|
|
340
|
-
*/
|
|
341
|
-
async checkAndUpdateBranchDetail(branchUid, stack, stackAPIClient, managementAPIClient) {
|
|
342
|
-
if (branchUid) {
|
|
343
|
-
try {
|
|
344
|
-
const branchExists = await doesBranchExist(stackAPIClient, branchUid);
|
|
345
|
-
if (branchExists?.errorCode) {
|
|
346
|
-
throw new Error(branchExists.errorMessage);
|
|
347
|
-
}
|
|
348
|
-
stack.branch_uid = branchUid;
|
|
349
|
-
stackAPIClient = this.getStackClient(managementAPIClient, stack);
|
|
350
|
-
} catch (error) {
|
|
351
|
-
if (error?.message || error?.errorMessage) {
|
|
352
|
-
cliux.error(util.formatError(error));
|
|
353
|
-
this.exit();
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
} else {
|
|
357
|
-
const stackBranches = await this.getStackBranches(stackAPIClient);
|
|
358
|
-
if (stackBranches === undefined) {
|
|
359
|
-
stackAPIClient = this.getStackClient(managementAPIClient, stack);
|
|
360
|
-
} else {
|
|
361
|
-
const { branch } = await util.chooseBranch(stackBranches);
|
|
362
|
-
stack.branch_uid = branch;
|
|
363
|
-
stackAPIClient = this.getStackClient(managementAPIClient, stack);
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
return stackAPIClient;
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* fetch stack details from alias token
|
|
371
|
-
* @param {string} managementTokenAlias
|
|
372
|
-
* @param {string} stackName
|
|
373
|
-
* @returns
|
|
374
|
-
*/
|
|
375
|
-
async getAliasDetails(managementTokenAlias, stackName) {
|
|
376
|
-
let apiClient, stackDetails;
|
|
377
|
-
const listOfTokens = configHandler.get('tokens');
|
|
378
|
-
if (managementTokenAlias && listOfTokens[managementTokenAlias]) {
|
|
379
|
-
const checkManagementTokenValidity = await isManagementTokenValid(
|
|
380
|
-
listOfTokens[managementTokenAlias].apiKey,
|
|
381
|
-
listOfTokens[managementTokenAlias].token,
|
|
382
|
-
);
|
|
383
|
-
if (Object.prototype.hasOwnProperty.call(checkManagementTokenValidity, 'message')) {
|
|
384
|
-
throw checkManagementTokenValidity.valid === 'failedToCheck'
|
|
385
|
-
? checkManagementTokenValidity.message
|
|
386
|
-
: `error: Management token or stack API key is invalid. ${checkManagementTokenValidity.message}`;
|
|
387
|
-
}
|
|
388
|
-
apiClient = await managementSDKClient({
|
|
389
|
-
host: this.cmaHost,
|
|
390
|
-
management_token: listOfTokens[managementTokenAlias].token,
|
|
391
|
-
});
|
|
392
|
-
stackDetails = {
|
|
393
|
-
name: stackName || managementTokenAlias,
|
|
394
|
-
apiKey: listOfTokens[managementTokenAlias].apiKey,
|
|
395
|
-
token: listOfTokens[managementTokenAlias].token,
|
|
396
|
-
};
|
|
397
|
-
} else if (managementTokenAlias) {
|
|
398
|
-
this.error('The provided management token alias was not found in your config.');
|
|
399
|
-
}
|
|
400
|
-
return {
|
|
401
|
-
apiClient,
|
|
402
|
-
stackDetails,
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* fetch stack details on basis of the selected org and stack
|
|
408
|
-
* @param {*} managementAPIClient
|
|
409
|
-
* @param {string} stackAPIKey
|
|
410
|
-
* @param {string} org
|
|
411
|
-
* @returns
|
|
412
|
-
*/
|
|
413
|
-
async getStackDetails(managementAPIClient, stackAPIKey, org) {
|
|
414
|
-
let organization, stackDetails;
|
|
415
|
-
|
|
416
|
-
if (!isAuthenticated()) {
|
|
417
|
-
this.error(config.CLI_EXPORT_CSV_ENTRIES_ERROR, {
|
|
418
|
-
exit: 2,
|
|
419
|
-
suggestions: ['https://www.contentstack.com/docs/developers/cli/authentication/'],
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
if (org) {
|
|
424
|
-
organization = { uid: org };
|
|
425
|
-
} else {
|
|
426
|
-
organization = await util.chooseOrganization(managementAPIClient); // prompt for organization
|
|
427
|
-
}
|
|
428
|
-
if (!stackAPIKey) {
|
|
429
|
-
stackDetails = await util.chooseStack(managementAPIClient, organization.uid); // prompt for stack
|
|
430
|
-
} else {
|
|
431
|
-
stackDetails = await util.chooseStack(managementAPIClient, organization.uid, stackAPIKey);
|
|
432
|
-
}
|
|
433
|
-
return stackDetails;
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* Create a taxonomies csv file for stack and a terms csv file for associated taxonomies
|
|
438
|
-
* @param {string} stackName
|
|
439
|
-
* @param {object} stack
|
|
440
|
-
* @param {string} taxUID
|
|
441
|
-
*/
|
|
442
|
-
async createTaxonomyAndTermCsvFile(stackAPIClient, stackName, stack, taxUID, delimiter, localeOptions = {}) {
|
|
443
|
-
const payload = {
|
|
444
|
-
stackAPIClient,
|
|
445
|
-
type: '',
|
|
446
|
-
limit: config.limit || 100,
|
|
447
|
-
...localeOptions, // Spread locale, branch, include_fallback, fallback_locale
|
|
448
|
-
};
|
|
449
|
-
//check whether the taxonomy is valid or not
|
|
450
|
-
let taxonomies = [];
|
|
451
|
-
if (taxUID) {
|
|
452
|
-
payload['taxonomyUID'] = taxUID;
|
|
453
|
-
const taxonomy = await util.getTaxonomy(payload);
|
|
454
|
-
taxonomies.push(taxonomy);
|
|
455
|
-
} else {
|
|
456
|
-
taxonomies = await util.getAllTaxonomies(payload);
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
if (!taxonomies?.length) {
|
|
460
|
-
cliux.print('info: No taxonomies found!', { color: 'blue' });
|
|
461
|
-
} else {
|
|
462
|
-
const fileName = `${stackName ?? stack.name}_taxonomies.csv`;
|
|
463
|
-
const { taxonomiesData, headers } = await util.createImportableCSV(payload, taxonomies);
|
|
464
|
-
if (taxonomiesData?.length) {
|
|
465
|
-
util.write(this, taxonomiesData, fileName, 'taxonomies', delimiter, headers);
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
ExportToCsvCommand.description = `Export entries, taxonomies, terms or organization users to csv using this command`;
|
|
472
|
-
|
|
473
|
-
ExportToCsvCommand.examples = [
|
|
474
|
-
'csdx cm:export-to-csv',
|
|
475
|
-
'',
|
|
476
|
-
'Exporting entries to CSV',
|
|
477
|
-
'csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type>',
|
|
478
|
-
'',
|
|
479
|
-
'Exporting entries to CSV with stack name provided and branch name provided',
|
|
480
|
-
'csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type> --stack-name <stack-name> --branch <branch-name>',
|
|
481
|
-
'',
|
|
482
|
-
'Exporting organization users to CSV',
|
|
483
|
-
'csdx cm:export-to-csv --action <users> --org <org-uid>',
|
|
484
|
-
'',
|
|
485
|
-
'Exporting organization users to CSV with organization name provided',
|
|
486
|
-
'csdx cm:export-to-csv --action <users> --org <org-uid> --org-name <org-name>',
|
|
487
|
-
'',
|
|
488
|
-
'Exporting organization teams to CSV',
|
|
489
|
-
'csdx cm:export-to-csv --action <teams>',
|
|
490
|
-
'',
|
|
491
|
-
'Exporting organization teams to CSV with org UID',
|
|
492
|
-
'csdx cm:export-to-csv --action <teams> --org <org-uid>',
|
|
493
|
-
'',
|
|
494
|
-
'Exporting organization teams to CSV with team UID',
|
|
495
|
-
'csdx cm:export-to-csv --action <teams> --team-uid <team-uid>',
|
|
496
|
-
'',
|
|
497
|
-
'Exporting organization teams to CSV with org UID and team UID',
|
|
498
|
-
'csdx cm:export-to-csv --action <teams> --org <org-uid> --team-uid <team-uid>',
|
|
499
|
-
'',
|
|
500
|
-
'Exporting organization teams to CSV with org UID and team UID',
|
|
501
|
-
'csdx cm:export-to-csv --action <teams> --org <org-uid> --team-uid <team-uid> --org-name <org-name>',
|
|
502
|
-
'',
|
|
503
|
-
'Exporting taxonomies and related terms to a .CSV file with the provided taxonomy UID',
|
|
504
|
-
'csdx cm:export-to-csv --action <taxonomies> --alias <management-token-alias> --taxonomy-uid <taxonomy-uid>',
|
|
505
|
-
'',
|
|
506
|
-
'Exporting taxonomies and respective terms to a .CSV file',
|
|
507
|
-
'csdx cm:export-to-csv --action <taxonomies> --alias <management-token-alias>',
|
|
508
|
-
'',
|
|
509
|
-
'Exporting taxonomies and respective terms to a .CSV file with a delimiter',
|
|
510
|
-
'csdx cm:export-to-csv --action <taxonomies> --alias <management-token-alias> --delimiter <delimiter>',
|
|
511
|
-
'',
|
|
512
|
-
'Exporting taxonomies with specific locale',
|
|
513
|
-
'csdx cm:export-to-csv --action <taxonomies> --alias <management-token-alias> --locale <locale>',
|
|
514
|
-
'',
|
|
515
|
-
'Exporting taxonomies with fallback locale support',
|
|
516
|
-
'csdx cm:export-to-csv --action <taxonomies> --alias <management-token-alias> --locale <locale> --include-fallback',
|
|
517
|
-
'',
|
|
518
|
-
'Exporting taxonomies with custom fallback locale',
|
|
519
|
-
'csdx cm:export-to-csv --action <taxonomies> --alias <management-token-alias> --locale <locale> --include-fallback --fallback-locale <fallback-locale>',
|
|
520
|
-
'',
|
|
521
|
-
];
|
|
522
|
-
|
|
523
|
-
module.exports = ExportToCsvCommand;
|
package/src/util/client.js
DELETED
|
File without changes
|
package/src/util/config.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
limit:100,
|
|
3
|
-
cancelString: 'Cancel and Exit',
|
|
4
|
-
exportEntries: 'Export entries to a .CSV file',
|
|
5
|
-
exportUsers: "Export organization users' data to a .CSV file",
|
|
6
|
-
exportTeams: "Export organization teams' data to a .CSV file",
|
|
7
|
-
exportTaxonomies: 'Export taxonomies to a .CSV file',
|
|
8
|
-
adminError: "Unable to export data. Make sure you're an admin or owner of this organization",
|
|
9
|
-
organizationNameRegex: /\'/,
|
|
10
|
-
CLI_EXPORT_CSV_LOGIN_FAILED: "You need to login to execute this command. See: auth:login --help",
|
|
11
|
-
CLI_EXPORT_CSV_ENTRIES_ERROR: "You need to either login or provide a management token to execute this command",
|
|
12
|
-
CLI_EXPORT_CSV_API_FAILED: 'Something went wrong! Please try again'
|
|
13
|
-
};
|