-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathexportToCsv.ts
93 lines (81 loc) · 2.23 KB
/
exportToCsv.ts
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
import {flatten} from 'safe-flat';
import * as csv from '@fast-csv/format';
import {getContentByType} from './src/utils/utils';
import {DEFAULT_LANGUAGE_TAG} from '../shared/src/i18n/constants';
import {createWriteStream} from 'fs';
const types = ['categories', 'collections', 'exercises', 'tags', 'ui', 'email'];
const FROM_LANGUAGE_TAG = DEFAULT_LANGUAGE_TAG;
const [, , TO_LANGUAGE_TAG] = process.argv;
if (!TO_LANGUAGE_TAG) {
throw new Error('Missing language argument');
}
const onlyFiles = {
ui: [],
exercises: [],
};
const skipKeys = {
categories: ['id', 'exercises', 'collections', 'lottie'],
collections: ['id', 'coCreators', 'card', 'tags', 'exercises'],
tags: ['id'],
exercises: [
'id',
'type',
'source',
'preview',
'image',
'card',
'coCreators',
'audio',
'subtitles',
'textColor',
'backgroundColor',
'published',
'tags',
'p5JsScript',
],
};
const main = async () => {
const data = await Promise.all(
types.map(async type => ({
type,
content: await getContentByType(type),
})),
);
data.forEach(({type, content}) => {
const csvStream = csv.format({headers: true});
csvStream.pipe(
createWriteStream(`./content-${type}-${TO_LANGUAGE_TAG}.csv`),
);
csvStream.write([
'Type',
'File',
'Key',
FROM_LANGUAGE_TAG,
TO_LANGUAGE_TAG,
]);
Object.entries(content)
.filter(
([file]) => !onlyFiles[type]?.length || onlyFiles[type].includes(file),
)
.forEach(([file, translations]) => {
const fromTranslations = flatten(translations[FROM_LANGUAGE_TAG]);
const toTranslations = flatten(translations[TO_LANGUAGE_TAG]);
Object.entries(fromTranslations)
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
.forEach(([key, value]) => {
if (
value &&
typeof value === 'string' &&
!value.startsWith('http') &&
!skipKeys[type]?.some(
skip => key.startsWith(skip) || key.endsWith(skip),
)
) {
csvStream.write([type, file, key, value, toTranslations[key]]);
}
});
});
csvStream.end();
});
};
main();