@contentstack/cli-cm-export-to-csv 0.1.0-beta → 0.1.0-beta.3
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/oclif.manifest.json +1 -1
- package/package.json +3 -3
- package/src/commands/cm/export-to-csv.js +8 -2
- package/src/util/index.js +43 -2
package/oclif.manifest.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"0.1.0-beta","commands":{"cm:export-to-csv":{"id":"cm:export-to-csv","description":"Export entries or organization users to csv using this command\n","pluginName":"@contentstack/cli-cm-export-to-csv","pluginType":"core","aliases":[],"flags":{},"args":[]}}}
|
|
1
|
+
{"version":"0.1.0-beta.3","commands":{"cm:export-to-csv":{"id":"cm:export-to-csv","description":"Export entries or organization users to csv using this command\n","pluginName":"@contentstack/cli-cm-export-to-csv","pluginType":"core","aliases":[],"flags":{},"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": "0.1.0-beta",
|
|
4
|
+
"version": "0.1.0-beta.3",
|
|
5
5
|
"author": "Abhinav Gupta @abhinav-from-contentstack",
|
|
6
6
|
"bugs": "https://github.com/contentstack/cli/issues",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@contentstack/cli-command": "^0.1.1-beta.
|
|
9
|
-
"@contentstack/management": "^1.
|
|
8
|
+
"@contentstack/cli-command": "^0.1.1-beta.6",
|
|
9
|
+
"@contentstack/management": "^1.3.0",
|
|
10
10
|
"@oclif/command": "^1.8.0",
|
|
11
11
|
"@oclif/config": "^1.17.0",
|
|
12
12
|
"axios": "^0.21.1",
|
|
@@ -24,8 +24,14 @@ class ExportToCsvCommand extends Command {
|
|
|
24
24
|
const environments = await util.getEnvironments(this.managementAPIClient, stack.apiKey) // fetch environments, because in publish details only env uid are available and we need env names
|
|
25
25
|
while(contentTypes.length > 0) {
|
|
26
26
|
let contentType = contentTypes.shift()
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
|
|
28
|
+
const entriesCount = await util.getEntriesCount(this.managementAPIClient, stack.apiKey, contentType, language.code);
|
|
29
|
+
let flatEntries = [];
|
|
30
|
+
for (let index = 0; index < entriesCount / 100; index++) {
|
|
31
|
+
const entriesResult = await util.getEntries(this.managementAPIClient, stack.apiKey, contentType, language.code, index);
|
|
32
|
+
const flatEntriesResult = util.cleanEntries(entriesResult.items, language.code, environments, contentType);
|
|
33
|
+
flatEntries = flatEntries.concat(flatEntriesResult);
|
|
34
|
+
}
|
|
29
35
|
// let dateTime = util.getDateTime()
|
|
30
36
|
// let fileName = `${contentType}_${language.code}_entries_export_${dateTime}.csv`
|
|
31
37
|
let fileName = `${stack.name}_${contentType}_${language.code}_entries_export.csv`
|
package/src/util/index.js
CHANGED
|
@@ -173,18 +173,30 @@ function getLanguages(managementAPIClient, stackApiKey) {
|
|
|
173
173
|
})
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
-
function getEntries(managementAPIClient, stackApiKey, contentType, language) {
|
|
176
|
+
function getEntries(managementAPIClient, stackApiKey, contentType, language, skip) {
|
|
177
177
|
return new Promise(resolve => {
|
|
178
178
|
managementAPIClient
|
|
179
179
|
.stack({api_key: stackApiKey})
|
|
180
180
|
.contentType(contentType)
|
|
181
181
|
.entry()
|
|
182
|
-
.query({include_publish_details: true, locale: language})
|
|
182
|
+
.query({include_publish_details: true, locale: language, skip: skip * 100})
|
|
183
183
|
.find()
|
|
184
184
|
.then(entries => resolve(entries))
|
|
185
185
|
})
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
+
function getEntriesCount(managementAPIClient, stackApiKey, contentType, language) {
|
|
189
|
+
return new Promise((resolve) => {
|
|
190
|
+
managementAPIClient
|
|
191
|
+
.stack({ api_key: stackApiKey })
|
|
192
|
+
.contentType(contentType)
|
|
193
|
+
.entry()
|
|
194
|
+
.query({ include_publish_details: true, locale: language })
|
|
195
|
+
.count()
|
|
196
|
+
.then((entriesData) => resolve(entriesData.entries));
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
188
200
|
function getEnvironments(managementAPIClient, stackApiKey) {
|
|
189
201
|
let result = {}
|
|
190
202
|
return managementAPIClient.stack({api_key: stackApiKey}).environment().query().find().then(environments => {
|
|
@@ -215,6 +227,7 @@ function cleanEntries(entries, language, environments, contentTypeUid) {
|
|
|
215
227
|
workflow = entry['_workflow']['name']
|
|
216
228
|
delete entry['_workflow']
|
|
217
229
|
}
|
|
230
|
+
entry = flatten(entry)
|
|
218
231
|
entry['publish_details'] = envArr
|
|
219
232
|
entry['_workflow'] = workflow
|
|
220
233
|
entry['ACL'] = JSON.stringify({}) // setting ACL to empty obj
|
|
@@ -227,6 +240,7 @@ function cleanEntries(entries, language, environments, contentTypeUid) {
|
|
|
227
240
|
delete entry.publish
|
|
228
241
|
delete entry.unpublish
|
|
229
242
|
delete entry.import
|
|
243
|
+
delete entry.publishRequest
|
|
230
244
|
return entry
|
|
231
245
|
})
|
|
232
246
|
|
|
@@ -366,6 +380,31 @@ function kebabize(str) {
|
|
|
366
380
|
return str.split(' ').map((word) => word.toLowerCase()).join('-')
|
|
367
381
|
}
|
|
368
382
|
|
|
383
|
+
// https://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects
|
|
384
|
+
function flatten(data) {
|
|
385
|
+
var result = {};
|
|
386
|
+
function recurse (cur, prop) {
|
|
387
|
+
if (Object(cur) !== cur) {
|
|
388
|
+
result[prop] = cur;
|
|
389
|
+
} else if (Array.isArray(cur)) {
|
|
390
|
+
for(var i=0, l=cur.length; i<l; i++)
|
|
391
|
+
recurse(cur[i], prop + "[" + i + "]");
|
|
392
|
+
if (l == 0)
|
|
393
|
+
result[prop] = [];
|
|
394
|
+
} else {
|
|
395
|
+
var isEmpty = true;
|
|
396
|
+
for (var p in cur) {
|
|
397
|
+
isEmpty = false;
|
|
398
|
+
recurse(cur[p], prop ? prop+"."+p : p);
|
|
399
|
+
}
|
|
400
|
+
if (isEmpty && prop)
|
|
401
|
+
result[prop] = {};
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
recurse(data, "");
|
|
405
|
+
return result;
|
|
406
|
+
}
|
|
407
|
+
|
|
369
408
|
module.exports = {
|
|
370
409
|
chooseOrganization: chooseOrganization,
|
|
371
410
|
chooseStack: chooseStack,
|
|
@@ -385,4 +424,6 @@ module.exports = {
|
|
|
385
424
|
determineUserOrgRole: determineUserOrgRole,
|
|
386
425
|
getOrganizationsWhereUserIsAdmin: getOrganizationsWhereUserIsAdmin,
|
|
387
426
|
kebabize: kebabize,
|
|
427
|
+
flatten: flatten,
|
|
428
|
+
getEntriesCount: getEntriesCount,
|
|
388
429
|
}
|