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

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.9",
2
+ "version": "1.3.11",
3
3
  "commands": {
4
4
  "cm:export-to-csv": {
5
5
  "id": "cm:export-to-csv",
@@ -58,6 +58,14 @@
58
58
  "required": false,
59
59
  "multiple": false
60
60
  },
61
+ "stack-api-key": {
62
+ "name": "stack-api-key",
63
+ "type": "option",
64
+ "char": "k",
65
+ "description": "API key of the source stack",
66
+ "required": false,
67
+ "multiple": false
68
+ },
61
69
  "org-name": {
62
70
  "name": "org-name",
63
71
  "type": "option",
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.9",
4
+ "version": "1.3.11",
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.9",
9
- "@contentstack/cli-utilities": "^1.4.5",
8
+ "@contentstack/cli-command": "^1.2.10",
9
+ "@contentstack/cli-utilities": "^1.5.0",
10
10
  "chalk": "^4.1.0",
11
11
  "fast-csv": "^4.3.6",
12
12
  "inquirer": "8.2.4",
@@ -17,7 +17,7 @@
17
17
  "@oclif/test": "^2.2.10",
18
18
  "chai": "^4.2.0",
19
19
  "debug": "^4.3.1",
20
- "eslint": "^8.18.0",
20
+ "eslint": "^7.32.0",
21
21
  "eslint-config-oclif": "^4.0.0",
22
22
  "globby": "^10.0.2",
23
23
  "mocha": "^10.0.0",
@@ -44,6 +44,7 @@
44
44
  "postpack": "rm -f oclif.manifest.json",
45
45
  "prepack": "oclif manifest && oclif readme",
46
46
  "test": "nyc mocha --forbid-only \"test/**/*.test.js\"",
47
+ "test:unit": "nyc mocha --timeout 10000 --forbid-only \"test/unit/**/*.test.js\"",
47
48
  "version": "oclif readme && git add README.md",
48
49
  "clean": "rm -rf ./node_modules tsconfig.build.tsbuildinfo"
49
50
  },
@@ -60,4 +61,4 @@
60
61
  }
61
62
  },
62
63
  "repository": "https://github.com/contentstack/cli"
63
- }
64
+ }
@@ -33,6 +33,12 @@ class ExportToCsvCommand extends Command {
33
33
  required: false,
34
34
  description: 'Name of the stack that needs to be created as csv filename.',
35
35
  }),
36
+ 'stack-api-key': flags.string({
37
+ char: 'k',
38
+ multiple: false,
39
+ required: false,
40
+ description: 'API key of the source stack',
41
+ }),
36
42
  'org-name': flags.string({
37
43
  multiple: false,
38
44
  required: false,
@@ -64,6 +70,7 @@ class ExportToCsvCommand extends Command {
64
70
  action: actionFlag,
65
71
  'org-name': orgName,
66
72
  'stack-name': stackName,
73
+ 'stack-api-key': stackAPIKey,
67
74
  locale: locale,
68
75
  'content-type': contentTypesFlag,
69
76
  alias: managementTokenAlias,
@@ -119,8 +126,11 @@ class ExportToCsvCommand extends Command {
119
126
  } else {
120
127
  organization = await util.chooseOrganization(managementAPIClient); // prompt for organization
121
128
  }
122
-
123
- stack = await util.chooseStack(managementAPIClient, organization.uid); // prompt for stack
129
+ if (!stackAPIKey) {
130
+ stack = await util.chooseStack(managementAPIClient, organization.uid); // prompt for stack
131
+ } else {
132
+ stack = await util.chooseStack(managementAPIClient, organization.uid, stackAPIKey);
133
+ }
124
134
  }
125
135
 
126
136
  stackAPIClient = this.getStackClient(managementAPIClient, stack);
@@ -156,6 +166,19 @@ class ExportToCsvCommand extends Command {
156
166
 
157
167
  if (contentTypesFlag) {
158
168
  contentTypes = contentTypesFlag.split(',').map(this.snakeCase);
169
+ const contentTypesArray = await stackAPIClient
170
+ .contentType()
171
+ .query()
172
+ .find()
173
+ .then((res) => res.items.map((contentType) => contentType.uid));
174
+
175
+ const doesContentTypeExist = contentTypesArray.includes(contentTypesFlag);
176
+
177
+ if (!doesContentTypeExist) {
178
+ throw new Error(
179
+ `The Content Type ${contentTypesFlag} was not found. Please try again. Content Type is not valid.`,
180
+ );
181
+ }
159
182
  } else {
160
183
  for (let index = 0; index <= contentTypeCount / 100; index++) {
161
184
  const contentTypesMap = await util.getContentTypes(stackAPIClient, index);
@@ -193,12 +216,11 @@ class ExportToCsvCommand extends Command {
193
216
  );
194
217
  flatEntries = flatEntries.concat(flatEntriesResult);
195
218
  }
196
- let fileName = `${stack.name}_${contentType}_${language.code}_entries_export.csv`;
197
-
219
+ let fileName = `${stackName ? stackName : stack.name}_${contentType}_${language.code}_entries_export.csv`;
198
220
  util.write(this, flatEntries, fileName, 'entries'); // write to file
199
221
  }
200
222
  } catch (error) {
201
- this.log(util.formatError(error));
223
+ cliux.error(util.formatError(error));
202
224
  }
203
225
  break;
204
226
  }
@@ -225,13 +247,13 @@ class ExportToCsvCommand extends Command {
225
247
  const mappedRoles = util.getMappedRoles(orgRoles);
226
248
  const listOfUsers = util.cleanOrgUsers(orgUsers, mappedUsers, mappedRoles);
227
249
  const fileName = `${util.kebabize(
228
- organization.name.replace(config.organizationNameRegex, ''),
250
+ (orgName ? orgName : organization.name).replace(config.organizationNameRegex, ''),
229
251
  )}_users_export.csv`;
230
252
 
231
253
  util.write(this, listOfUsers, fileName, 'organization details');
232
254
  } catch (error) {
233
255
  if (error.message || error.errorMessage) {
234
- this.log(util.formatError(error));
256
+ cliux.error(util.formatError(error));
235
257
  }
236
258
  }
237
259
  break;
@@ -239,7 +261,7 @@ class ExportToCsvCommand extends Command {
239
261
  }
240
262
  } catch (error) {
241
263
  if (error.message || error.errorMessage) {
242
- this.log(util.formatError(error));
264
+ cliux.error(util.formatError(error));
243
265
  }
244
266
  }
245
267
  }
package/src/util/index.js CHANGED
@@ -8,7 +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
+ const { cliux, configHandler } = require('@contentstack/cli-utilities');
12
12
 
13
13
  const directory = './data';
14
14
  const delimeter = os.platform() === 'win32' ? '\\' : '/';
@@ -59,8 +59,16 @@ async function getOrganizations(managementAPIClient) {
59
59
  }
60
60
 
61
61
  async function getOrganizationList(managementAPIClient, params, result = []) {
62
- const organizations = await managementAPIClient.organization().fetchAll(params);
63
- result = result.concat(organizations.items);
62
+ let organizations;
63
+ const configOrgUid = configHandler.get('oauthOrgUid');
64
+
65
+ if (configOrgUid) {
66
+ organizations = await managementAPIClient.organization(configOrgUid).fetch();
67
+ result = result.concat([organizations]);
68
+ } else {
69
+ organizations = await managementAPIClient.organization().fetchAll({ limit: 100 });
70
+ result = result.concat(organizations.items);
71
+ }
64
72
 
65
73
  if (!organizations.items || (organizations.items && organizations.items.length < params.limit)) {
66
74
  const orgMap = {};
@@ -76,35 +84,53 @@ async function getOrganizationList(managementAPIClient, params, result = []) {
76
84
  }
77
85
  }
78
86
 
79
- function getOrganizationsWhereUserIsAdmin(managementAPIClient) {
80
- return new Promise((resolve, reject) => {
87
+ async function getOrganizationsWhereUserIsAdmin(managementAPIClient) {
88
+ try {
81
89
  let result = {};
82
- managementAPIClient
83
- .getUser({ include_orgs_roles: true })
84
- .then((response) => {
85
- let organizations = response.organizations.filter((org) => {
86
- if (org.org_roles) {
87
- const org_role = org.org_roles.shift();
88
- return org_role.admin;
89
- }
90
- return org.is_owner === true;
91
- });
92
- organizations.forEach((org) => {
93
- result[org.name] = org.uid;
94
- });
95
- resolve(result);
96
- })
97
- .catch((error) => reject(error));
98
- });
90
+ const configOrgUid = configHandler.get('oauthOrgUid');
91
+
92
+ if (configOrgUid) {
93
+ const response = await managementAPIClient.organization(configOrgUid).fetch();
94
+ result[response.name] = response.uid;
95
+ } else {
96
+ const response = await managementAPIClient.getUser({ include_orgs_roles: true });
97
+ const organizations = response.organizations.filter((org) => {
98
+ if (org.org_roles) {
99
+ const org_role = org.org_roles.shift();
100
+ return org_role.admin;
101
+ }
102
+ return org.is_owner === true;
103
+ });
104
+
105
+ organizations.forEach((org) => {
106
+ result[org.name] = org.uid;
107
+ });
108
+ }
109
+
110
+ return result;
111
+ } catch (error) {
112
+ throw error;
113
+ }
99
114
  }
100
115
 
101
- function chooseStack(managementAPIClient, orgUid) {
116
+ function chooseStack(managementAPIClient, orgUid, stackApiKey) {
102
117
  return new Promise(async (resolve, reject) => {
103
118
  try {
104
119
  let stacks = await getStacks(managementAPIClient, orgUid);
120
+
121
+ if (stackApiKey) {
122
+ const stackName = Object.keys(stacks).find((key) => stacks[key] === stackApiKey);
123
+
124
+ if (stackName) {
125
+ resolve({ name: stackName, apiKey: stackApiKey });
126
+ } else {
127
+ throw new Error('Could not find stack');
128
+ }
129
+ return;
130
+ }
131
+
105
132
  let stackList = Object.keys(stacks);
106
133
  stackList.push(config.cancelString);
107
-
108
134
  let _chooseStack = [
109
135
  {
110
136
  type: 'list',
@@ -242,7 +268,9 @@ function getContentTypes(stackAPIClient, skip) {
242
268
  });
243
269
  resolve(result);
244
270
  })
245
- .catch((error) => reject(error));
271
+ .catch((error) => {
272
+ reject(error);
273
+ });
246
274
  });
247
275
  }
248
276
 
@@ -397,7 +425,7 @@ function write(command, entries, fileName, message) {
397
425
  process.chdir(directory);
398
426
  }
399
427
  // eslint-disable-next-line no-undef
400
- command.log(`Writing ${message} to file: ${process.cwd()}${delimeter}${fileName}`);
428
+ cliux.print(`Writing ${message} to file: ${process.cwd()}${delimeter}${fileName}`);
401
429
  fastcsv.writeToPath(fileName, entries, { headers: true });
402
430
  }
403
431