/
export-to-csv.js
489 lines (453 loc) · 17.7 KB
/
export-to-csv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
const { Command } = require('@contentstack/cli-command');
const {
configHandler,
managementSDKClient,
flags,
isAuthenticated,
cliux,
doesBranchExist,
isManagementTokenValid
} = require('@contentstack/cli-utilities');
const util = require('../../util');
const config = require('../../util/config');
class ExportToCsvCommand extends Command {
static flags = {
action: flags.string({
required: false,
multiple: false,
options: ['entries', 'users', 'teams', 'taxonomies'],
description: `Option to export data (entries, users, teams, taxonomies)`,
}),
alias: flags.string({
char: 'a',
description: 'Alias of the management token',
}),
org: flags.string({
multiple: false,
required: false,
description: 'Provide organization UID to clone org users',
}),
'stack-name': flags.string({
char: 'n',
multiple: false,
required: false,
description: 'Name of the stack that needs to be created as csv filename.',
}),
'stack-api-key': flags.string({
char: 'k',
multiple: false,
required: false,
description: 'API key of the source stack',
}),
'org-name': flags.string({
multiple: false,
required: false,
description: 'Name of the organization that needs to be created as csv filename.',
}),
locale: flags.string({
required: false,
multiple: false,
description: 'Locale for which entries need to be exported',
}),
'content-type': flags.string({
description: 'Content type for which entries needs to be exported',
required: false,
multiple: false,
}),
branch: flags.string({
description: 'Branch from which entries need to be exported',
multiple: false,
required: false,
}),
"team-uid": flags.string({
description: 'Uid of the team whose user data and stack roles are required'
}),
'taxonomy-uid': flags.string({
description: 'Provide the taxonomy UID of the related terms you want to export',
}),
delimiter: flags.string({
description: '[optional] Provide a delimiter to separate individual data fields within the CSV file.',
default: ',',
}),
};
async run() {
try {
let action, managementAPIClient;
const {
flags: {
org,
action: actionFlag,
'org-name': orgName,
'stack-name': stackName,
'stack-api-key': stackAPIKey,
locale: locale,
'content-type': contentTypesFlag,
alias: managementTokenAlias,
branch: branchUid,
"team-uid": teamUid,
'taxonomy-uid': taxonomyUID,
delimiter
},
} = await this.parse(ExportToCsvCommand);
if (!managementTokenAlias) {
managementAPIClient = await managementSDKClient({ host: this.cmaHost });
if (!isAuthenticated()) {
this.error(config.CLI_EXPORT_CSV_ENTRIES_ERROR, {
exit: 2,
suggestions: ['https://www.contentstack.com/docs/developers/cli/authentication/'],
});
}
}
if (actionFlag) {
action = actionFlag;
} else {
action = await util.startupQuestions();
}
switch (action) {
case config.exportEntries:
case 'entries': {
try {
let stack;
let stackAPIClient;
let language;
let contentTypes = [];
if (managementTokenAlias) {
const { stackDetails, apiClient } = await this.getAliasDetails(managementTokenAlias, stackName);
managementAPIClient = apiClient;
stack = stackDetails;
} else {
stack = await this.getStackDetails(managementAPIClient, stackAPIKey, org);
}
stackAPIClient = this.getStackClient(managementAPIClient, stack);
await this.checkAndUpdateBranchDetail(branchUid, stack, stackAPIClient, managementAPIClient);
const contentTypeCount = await util.getContentTypeCount(stackAPIClient);
const environments = await util.getEnvironments(stackAPIClient); // fetch environments, because in publish details only env uid are available and we need env names
if (contentTypesFlag) {
contentTypes = contentTypesFlag.split(',').map(this.snakeCase);
const contentTypesArray = await stackAPIClient
.contentType()
.query()
.find()
.then((res) => res.items.map((contentType) => contentType.uid));
const doesContentTypeExist = contentTypesArray.includes(contentTypesFlag);
if (!doesContentTypeExist) {
throw new Error(
`The Content Type ${contentTypesFlag} was not found. Please try again. Content Type is not valid.`,
);
}
} else {
for (let index = 0; index <= contentTypeCount / 100; index++) {
const contentTypesMap = await util.getContentTypes(stackAPIClient, index);
contentTypes = contentTypes.concat(Object.values(contentTypesMap)); // prompt for content Type
}
}
if (contentTypes.length <= 0) {
this.log('No content types found for the given stack');
this.exit();
}
if (!contentTypesFlag) {
contentTypes = await util.chooseInMemContentTypes(contentTypes);
}
if (locale) {
language = { code: locale };
} else {
language = await util.chooseLanguage(stackAPIClient); // prompt for language
}
while (contentTypes.length > 0) {
let contentType = contentTypes.pop();
const entriesCount = await util.getEntriesCount(stackAPIClient, contentType, language.code);
let flatEntries = [];
for (let index = 0; index < entriesCount / 100; index++) {
const entriesResult = await util.getEntries(stackAPIClient, contentType, language.code, index, 100);
const flatEntriesResult = util.cleanEntries(
entriesResult.items,
language.code,
environments,
contentType,
);
flatEntries = flatEntries.concat(flatEntriesResult);
}
let fileName = `${stackName ? stackName : stack.name}_${contentType}_${language.code}_entries_export.csv`;
util.write(this, flatEntries, fileName, 'entries', delimiter); // write to file
}
} catch (error) {
cliux.error(util.formatError(error));
}
break;
}
case config.exportUsers:
case 'users': {
try {
let organization;
if (org) {
organization = { uid: org, name: orgName || org };
} else {
organization = await util.chooseOrganization(managementAPIClient, action); // prompt for organization
}
const orgUsers = await util.getOrgUsers(managementAPIClient, organization.uid, this);
const orgRoles = await util.getOrgRoles(managementAPIClient, organization.uid, this);
const mappedUsers = util.getMappedUsers(orgUsers);
const mappedRoles = util.getMappedRoles(orgRoles);
const listOfUsers = util.cleanOrgUsers(orgUsers, mappedUsers, mappedRoles);
const fileName = `${util.kebabize(
(orgName ? orgName : organization.name).replace(config.organizationNameRegex, ''),
)}_users_export.csv`;
util.write(this, listOfUsers, fileName, 'organization details', delimiter);
} catch (error) {
if (error.message || error.errorMessage) {
cliux.error(util.formatError(error));
}
}
break;
}
case config.exportTeams:
case 'teams': {
try{
let organization;
if (org) {
organization = { uid: org, name: orgName || org };
} else {
organization = await util.chooseOrganization(managementAPIClient, action); // prompt for organization
}
await util.exportTeams(managementAPIClient,organization,teamUid, delimiter);
} catch (error) {
if (error.message || error.errorMessage) {
cliux.error(util.formatError(error));
}
}
break;
}
case config.exportTaxonomies:
case 'taxonomies': {
let stack;
let stackAPIClient;
if (managementTokenAlias) {
const { stackDetails, apiClient } = await this.getAliasDetails(managementTokenAlias, stackName);
managementAPIClient = apiClient;
stack = stackDetails;
} else {
stack = await this.getStackDetails(managementAPIClient, stackAPIKey, org);
}
stackAPIClient = this.getStackClient(managementAPIClient, stack);
await this.createTaxonomyAndTermCsvFile(stackAPIClient, stackName, stack, taxonomyUID, delimiter);
break;
}
}
} catch (error) {
if (error.message || error.errorMessage) {
cliux.error(util.formatError(error));
}
}
}
snakeCase(string) {
return (string || '').split(' ').join('_').toLowerCase();
}
getStackClient(managementAPIClient, stack) {
const stackInit = {
api_key: stack.apiKey,
};
if (stack?.branch_uid) stackInit['branch_uid'] = stack.branch_uid;
if (stack.token) {
return managementAPIClient.stack({
...stackInit,
management_token: stack.token,
});
}
return managementAPIClient.stack(stackInit);
}
getStackBranches(stackAPIClient) {
return stackAPIClient
.branch()
.query()
.find()
.then(({ items }) => (items !== undefined ? items : []))
.catch((_err) => {});
}
/**
* check whether branch enabled org or not and update branch details
* @param {string} branchUid
* @param {object} stack
* @param {*} stackAPIClient
* @param {*} managementAPIClient
*/
async checkAndUpdateBranchDetail(branchUid, stack, stackAPIClient, managementAPIClient) {
if (branchUid) {
try {
const branchExists = await doesBranchExist(stackAPIClient, branchUid);
if (branchExists?.errorCode) {
throw new Error(branchExists.errorMessage);
}
stack.branch_uid = branchUid;
stackAPIClient = this.getStackClient(managementAPIClient, stack);
} catch (error) {
if (error?.message || error?.errorMessage) {
cliux.error(util.formatError(error));
this.exit();
}
}
} else {
const stackBranches = await this.getStackBranches(stackAPIClient);
if (stackBranches === undefined) {
stackAPIClient = this.getStackClient(managementAPIClient, stack);
} else {
const { branch } = await util.chooseBranch(stackBranches);
stack.branch_uid = branch;
stackAPIClient = this.getStackClient(managementAPIClient, stack);
}
}
}
/**
* fetch stack details from alias token
* @param {string} managementTokenAlias
* @param {string} stackName
* @returns
*/
async getAliasDetails(managementTokenAlias, stackName) {
let apiClient, stackDetails;
const listOfTokens = configHandler.get('tokens');
if (managementTokenAlias && listOfTokens[managementTokenAlias]) {
const checkManagementTokenValidity = await isManagementTokenValid((listOfTokens[managementTokenAlias].apiKey) ,listOfTokens[managementTokenAlias].token);
if(checkManagementTokenValidity.hasOwnProperty('message')) {
throw checkManagementTokenValidity.valid==='failedToCheck'?checkManagementTokenValidity.message:(`error: Management token or stack API key is invalid. ${checkManagementTokenValidity.message}`);
}
apiClient = await managementSDKClient({
host: this.cmaHost,
management_token: listOfTokens[managementTokenAlias].token,
});
stackDetails = {
name: stackName || managementTokenAlias,
apiKey: listOfTokens[managementTokenAlias].apiKey,
token: listOfTokens[managementTokenAlias].token,
};
} else if (managementTokenAlias) {
this.error('Provided management token alias not found in your config.!');
}
return {
apiClient,
stackDetails,
};
}
/**
* fetch stack details on basis of the selected org and stack
* @param {*} managementAPIClient
* @param {string} stackAPIKey
* @param {string} org
* @returns
*/
async getStackDetails(managementAPIClient, stackAPIKey, org) {
let organization, stackDetails;
if (!isAuthenticated()) {
this.error(config.CLI_EXPORT_CSV_ENTRIES_ERROR, {
exit: 2,
suggestions: ['https://www.contentstack.com/docs/developers/cli/authentication/'],
});
}
if (org) {
organization = { uid: org };
} else {
organization = await util.chooseOrganization(managementAPIClient); // prompt for organization
}
if (!stackAPIKey) {
stackDetails = await util.chooseStack(managementAPIClient, organization.uid); // prompt for stack
} else {
stackDetails = await util.chooseStack(managementAPIClient, organization.uid, stackAPIKey);
}
return stackDetails;
}
/**
* Create a taxonomies csv file for stack and a terms csv file for associated taxonomies
* @param {string} stackName
* @param {object} stack
* @param {string} taxUID
*/
async createTaxonomyAndTermCsvFile(stackAPIClient, stackName, stack, taxUID, delimiter) {
//TODO: Temp variable to export taxonomies in importable format will replaced with flag once decided
const importableCSV = true;
const payload = {
stackAPIClient,
type: '',
limit: config.limit || 100,
};
//check whether the taxonomy is valid or not
let taxonomies = [];
if (taxUID) {
payload['taxonomyUID'] = taxUID;
const taxonomy = await util.getTaxonomy(payload);
taxonomies.push(taxonomy);
} else {
taxonomies = await util.getAllTaxonomies(payload);
}
if (!importableCSV) {
const formattedTaxonomiesData = util.formatTaxonomiesData(taxonomies);
if (formattedTaxonomiesData?.length) {
const fileName = `${stackName ? stackName : stack.name}_taxonomies.csv`;
util.write(this, formattedTaxonomiesData, fileName, 'taxonomies', delimiter);
} else {
cliux.print('info: No taxonomies found! Please provide a valid stack.', { color: 'blue' });
}
for (let index = 0; index < taxonomies?.length; index++) {
const taxonomy = taxonomies[index];
const taxonomyUID = taxonomy?.uid;
if (taxonomyUID) {
payload['taxonomyUID'] = taxonomyUID;
const terms = await util.getAllTermsOfTaxonomy(payload);
const formattedTermsData = util.formatTermsOfTaxonomyData(terms, taxonomyUID);
const taxonomyName = taxonomy?.name ?? '';
const termFileName = `${stackName ?? stack.name}_${taxonomyName}_${taxonomyUID}_terms.csv`;
if (formattedTermsData?.length) {
util.write(this, formattedTermsData, termFileName, 'terms', delimiter);
} else {
cliux.print(`info: No terms found for the taxonomy UID - '${taxonomyUID}'!`, { color: 'blue' });
}
}
}
} else {
const fileName = `${stackName ?? stack.name}_taxonomies.csv`;
const { taxonomiesData, headers } = await util.createImportableCSV(payload, taxonomies);
if (taxonomiesData?.length) {
util.write(this, taxonomiesData, fileName, 'taxonomies',delimiter, headers);
}
}
}
}
ExportToCsvCommand.description = `Export entries, taxonomies, terms or organization users to csv using this command`;
ExportToCsvCommand.examples = [
'csdx cm:export-to-csv',
'',
'Exporting entries to CSV',
'csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type>',
'',
'Exporting entries to CSV with stack name provided and branch name provided',
'csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type> --stack-name <stack-name> --branch <branch-name>',
'',
'Exporting organization users to CSV',
'csdx cm:export-to-csv --action <users> --org <org-uid>',
'',
'Exporting organization users to CSV with organization name provided',
'csdx cm:export-to-csv --action <users> --org <org-uid> --org-name <org-name>',
'',
'Exporting organization teams to CSV',
'csdx cm:export-to-csv --action <teams>',
'',
'Exporting organization teams to CSV with org UID',
'csdx cm:export-to-csv --action <teams> --org <org-uid>',
'',
'Exporting organization teams to CSV with team UID',
'csdx cm:export-to-csv --action <teams> --team-uid <team-uid>',
'',
'Exporting organization teams to CSV with org UID and team UID',
'csdx cm:export-to-csv --action <teams> --org <org-uid> --team-uid <team-uid>',
'',
'Exporting organization teams to CSV with org UID and team UID',
'csdx cm:export-to-csv --action <teams> --org <org-uid> --team-uid <team-uid> --org-name <org-name>',
'',
'Exporting taxonomies and related terms to a .CSV file with the provided taxonomy UID',
'csdx cm:export-to-csv --action <taxonomies> --alias <management-token-alias> --taxonomy-uid <taxonomy-uid>',
'',
'Exporting taxonomies and respective terms to a .CSV file',
'csdx cm:export-to-csv --action <taxonomies> --alias <management-token-alias>',
'',
'Exporting taxonomies and respective terms to a .CSV file with a delimiter',
'csdx cm:export-to-csv --action <taxonomies> --alias <management-token-alias> --delimiter <delimiter>',
];
module.exports = ExportToCsvCommand;