@contentstack/cli-cm-export-to-csv 1.3.7 → 1.3.9

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.
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.3.7",
2
+ "version": "1.3.9",
3
3
  "commands": {
4
4
  "cm:export-to-csv": {
5
5
  "id": "cm:export-to-csv",
@@ -15,8 +15,8 @@
15
15
  "Exporting entries to csv",
16
16
  "csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type>",
17
17
  "",
18
- "Exporting entries to csv with stack name provided",
19
- "csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type> --stack-name <stack-name>",
18
+ "Exporting entries to csv with stack name provided and branch name provided",
19
+ "csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type> --stack-name <stack-name> --branch <branch-name>",
20
20
  "",
21
21
  "Exporting organization users to csv",
22
22
  "csdx cm:export-to-csv --action <users> --org <org-uid>",
@@ -78,6 +78,13 @@
78
78
  "description": "Content type for which entries needs to be exported",
79
79
  "required": false,
80
80
  "multiple": false
81
+ },
82
+ "branch": {
83
+ "name": "branch",
84
+ "type": "option",
85
+ "description": "Branch from which entries need to be exported",
86
+ "required": false,
87
+ "multiple": false
81
88
  }
82
89
  },
83
90
  "args": {}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@contentstack/cli-cm-export-to-csv",
3
3
  "description": "Export entities to csv",
4
- "version": "1.3.7",
4
+ "version": "1.3.9",
5
5
  "author": "Abhinav Gupta @abhinav-from-contentstack",
6
6
  "bugs": "https://github.com/contentstack/cli/issues",
7
7
  "dependencies": {
8
- "@contentstack/cli-command": "^1.2.7",
9
- "@contentstack/cli-utilities": "^1.4.3",
8
+ "@contentstack/cli-command": "^1.2.9",
9
+ "@contentstack/cli-utilities": "^1.4.5",
10
10
  "chalk": "^4.1.0",
11
11
  "fast-csv": "^4.3.6",
12
12
  "inquirer": "8.2.4",
@@ -60,4 +60,4 @@
60
60
  }
61
61
  },
62
62
  "repository": "https://github.com/contentstack/cli"
63
- }
63
+ }
@@ -1,5 +1,12 @@
1
1
  const { Command } = require('@contentstack/cli-command');
2
- const { configHandler, managementSDKClient, flags, isAuthenticated } = require('@contentstack/cli-utilities');
2
+ const {
3
+ configHandler,
4
+ managementSDKClient,
5
+ flags,
6
+ isAuthenticated,
7
+ cliux,
8
+ doesBranchExist,
9
+ } = require('@contentstack/cli-utilities');
3
10
  const util = require('../../util');
4
11
  const config = require('../../util/config');
5
12
 
@@ -41,6 +48,11 @@ class ExportToCsvCommand extends Command {
41
48
  required: false,
42
49
  multiple: false,
43
50
  }),
51
+ branch: flags.string({
52
+ description: 'Branch from which entries need to be exported',
53
+ multiple: false,
54
+ required: false,
55
+ }),
44
56
  };
45
57
 
46
58
  async run() {
@@ -55,6 +67,7 @@ class ExportToCsvCommand extends Command {
55
67
  locale: locale,
56
68
  'content-type': contentTypesFlag,
57
69
  alias: managementTokenAlias,
70
+ branch: branchUid,
58
71
  },
59
72
  } = await this.parse(ExportToCsvCommand);
60
73
 
@@ -76,6 +89,7 @@ class ExportToCsvCommand extends Command {
76
89
  let stackAPIClient;
77
90
  let language;
78
91
  let contentTypes = [];
92
+ let stackBranches;
79
93
  const listOfTokens = configHandler.get('tokens');
80
94
 
81
95
  if (managementTokenAlias && listOfTokens[managementTokenAlias]) {
@@ -110,7 +124,34 @@ class ExportToCsvCommand extends Command {
110
124
  }
111
125
 
112
126
  stackAPIClient = this.getStackClient(managementAPIClient, stack);
127
+
128
+ if (branchUid) {
129
+ try {
130
+ const branchExists = await doesBranchExist(stackAPIClient, branchUid);
131
+ if (branchExists?.errorCode) {
132
+ throw new Error(branchExists.errorMessage);
133
+ }
134
+ stack.branch_uid = branchUid;
135
+ stackAPIClient = this.getStackClient(managementAPIClient, stack);
136
+ } catch (error) {
137
+ if (error.message || error.errorMessage) {
138
+ cliux.error(util.formatError(error));
139
+ this.exit();
140
+ }
141
+ }
142
+ } else {
143
+ stackBranches = await this.getStackBranches(stackAPIClient);
144
+ if (stackBranches === undefined) {
145
+ stackAPIClient = this.getStackClient(managementAPIClient, stack);
146
+ } else {
147
+ const { branch } = await util.chooseBranch(stackBranches);
148
+ stack.branch_uid = branch;
149
+ stackAPIClient = this.getStackClient(managementAPIClient, stack);
150
+ }
151
+ }
152
+
113
153
  const contentTypeCount = await util.getContentTypeCount(stackAPIClient);
154
+
114
155
  const environments = await util.getEnvironments(stackAPIClient); // fetch environments, because in publish details only env uid are available and we need env names
115
156
 
116
157
  if (contentTypesFlag) {
@@ -189,7 +230,7 @@ class ExportToCsvCommand extends Command {
189
230
 
190
231
  util.write(this, listOfUsers, fileName, 'organization details');
191
232
  } catch (error) {
192
- if (error.message) {
233
+ if (error.message || error.errorMessage) {
193
234
  this.log(util.formatError(error));
194
235
  }
195
236
  }
@@ -197,7 +238,7 @@ class ExportToCsvCommand extends Command {
197
238
  }
198
239
  }
199
240
  } catch (error) {
200
- if (error.message) {
241
+ if (error.message || error.errorMessage) {
201
242
  this.log(util.formatError(error));
202
243
  }
203
244
  }
@@ -208,13 +249,26 @@ class ExportToCsvCommand extends Command {
208
249
  }
209
250
 
210
251
  getStackClient(managementAPIClient, stack) {
252
+ const stackInit = {
253
+ api_key: stack.apiKey,
254
+ branch_uid: stack.branch_uid,
255
+ };
211
256
  if (stack.token) {
212
257
  return managementAPIClient.stack({
213
- api_key: stack.apiKey,
258
+ ...stackInit,
214
259
  management_token: stack.token,
215
260
  });
216
261
  }
217
- return managementAPIClient.stack({ api_key: stack.apiKey });
262
+ return managementAPIClient.stack(stackInit);
263
+ }
264
+
265
+ getStackBranches(stackAPIClient) {
266
+ return stackAPIClient
267
+ .branch()
268
+ .query()
269
+ .find()
270
+ .then(({ items }) => (items !== undefined ? items : []))
271
+ .catch((_err) => {});
218
272
  }
219
273
  }
220
274
 
@@ -226,8 +280,8 @@ ExportToCsvCommand.examples = [
226
280
  'Exporting entries to csv',
227
281
  'csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type>',
228
282
  '',
229
- 'Exporting entries to csv with stack name provided',
230
- 'csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type> --stack-name <stack-name>',
283
+ 'Exporting entries to csv with stack name provided and branch name provided',
284
+ 'csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type> --stack-name <stack-name> --branch <branch-name>',
231
285
  '',
232
286
  'Exporting organization users to csv',
233
287
  'csdx cm:export-to-csv --action <users> --org <org-uid>',
package/src/util/index.js CHANGED
@@ -8,6 +8,7 @@ const debug = require('debug')('export-to-csv');
8
8
  const checkboxPlus = require('inquirer-checkbox-plus-prompt');
9
9
 
10
10
  const config = require('./config.js');
11
+ const { cliux } = require('@contentstack/cli-utilities');
11
12
 
12
13
  const directory = './data';
13
14
  const delimeter = os.platform() === 'win32' ? '\\' : '/';
@@ -126,6 +127,26 @@ function chooseStack(managementAPIClient, orgUid) {
126
127
  });
127
128
  }
128
129
 
130
+ async function chooseBranch(branchList) {
131
+ try {
132
+ const branches = await branchList;
133
+
134
+ const branchesArray = branches.map((branch) => branch.uid);
135
+
136
+ let _chooseBranch = [
137
+ {
138
+ type: 'list',
139
+ name: 'branch',
140
+ message: 'Choose a Branch',
141
+ choices: branchesArray,
142
+ },
143
+ ];
144
+ return await inquirer.prompt(_chooseBranch);
145
+ } catch (err) {
146
+ cliux.error(err);
147
+ }
148
+ }
149
+
129
150
  function getStacks(managementAPIClient, orgUid) {
130
151
  return new Promise((resolve, reject) => {
131
152
  let result = {};
@@ -167,7 +188,7 @@ function chooseContentType(stackAPIClient, skip) {
167
188
  }
168
189
 
169
190
  function chooseInMemContentTypes(contentTypesList) {
170
- return new Promise(async (resolve, reject) => {
191
+ return new Promise((resolve, reject) => {
171
192
  let _chooseContentType = [
172
193
  {
173
194
  type: 'checkbox-plus',
@@ -213,7 +234,7 @@ function getContentTypes(stackAPIClient, skip) {
213
234
  let result = {};
214
235
  stackAPIClient
215
236
  .contentType()
216
- .query({ skip: skip * 100 })
237
+ .query({ skip: skip * 100, include_branch: true })
217
238
  .find()
218
239
  .then((contentTypes) => {
219
240
  contentTypes.items.forEach((contentType) => {
@@ -400,7 +421,7 @@ function startupQuestions() {
400
421
  });
401
422
  }
402
423
 
403
- function getOrgUsers(managementAPIClient, orgUid, ecsv) {
424
+ function getOrgUsers(managementAPIClient, orgUid) {
404
425
  return new Promise((resolve, reject) => {
405
426
  managementAPIClient
406
427
  .getUser({ include_orgs_roles: true })
@@ -442,10 +463,7 @@ async function getUsers(managementAPIClient, organization, params, result = [])
442
463
  await wait(200);
443
464
  return getUsers(managementAPIClient, organization, params, result);
444
465
  }
445
- } catch (error) {
446
- console.error(error);
447
- throw error;
448
- }
466
+ } catch (error) {}
449
467
  }
450
468
 
451
469
  function getMappedUsers(users) {
@@ -612,6 +630,7 @@ function wait(time) {
612
630
  module.exports = {
613
631
  chooseOrganization: chooseOrganization,
614
632
  chooseStack: chooseStack,
633
+ chooseBranch: chooseBranch,
615
634
  chooseContentType: chooseContentType,
616
635
  chooseLanguage: chooseLanguage,
617
636
  getEntries: getEntries,