-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcb-content-export-translations
executable file
·69 lines (58 loc) · 1.75 KB
/
cb-content-export-translations
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
#!/usr/bin/env node
const array2csv = require('convert-array-to-csv');
const Papa = require('papaparse');
const contentful = require('contentful-management');
const { Command } = require('commander');
const main = async ({
accessToken,
spaceId,
environmentId = 'master',
languages,
}) => {
const contentfulClient = await contentful.createClient({ accessToken });
const client = await contentfulClient
.getSpace(spaceId)
.then(s => s.getEnvironment(environmentId))
.catch(e => console.log(e));
// Better logic here. Check total from response
const [entries1, entries2] = await Promise.all([
client.getEntries({
limit: 1000,
content_type: 'translation',
}),
client.getEntries({
limit: 1000,
skip: 1000,
content_type: 'translation',
}),
]);
const entries = [...entries1.items, ...entries2.items];
const headers = ['identifier', ...languages];
const csv = [headers];
entries.forEach(e => {
if (!e.fields.identifier) return;
const row = [];
row.push(e.fields.identifier[Object.keys(e.fields.identifier)[0]]);
languages.forEach(l => {
row.push(
e.fields.translation
? e.fields.translation[l]?.replace(/\n$/) // Replace new line if it's the last char...
: ''
);
});
csv.push(row);
});
console.log(Papa.unparse(csv));
};
const program = new Command();
program
.requiredOption('-t, --accessToken <accessToken>', 'Content Management Token')
.requiredOption('-s, --spaceId <spaceId>', 'Space Id')
.option('-e, --environmentId <environmentId>', 'Environment Id');
program.parseAsync(process.argv);
main({
accessToken: program.accessToken,
spaceId: program.spaceId,
environmentId: program.environmentId,
languages: program.args,
});