-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export content to CSV format script (#4569)
- Loading branch information
Showing
4 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import {flatten} from 'safe-flat'; | ||
import * as csv from '@fast-csv/format'; | ||
import {getContentByType} from './src/utils/utils'; | ||
import {DEFAULT_LANGUAGE_TAG, LANGUAGES} 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 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', | ||
], | ||
}; | ||
|
||
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).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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES2017", | ||
"useDefineForClassFields": true, | ||
"lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
"target": "es2020", | ||
"module": "commonjs", | ||
"allowJs": true, // until content is converted into ts | ||
"skipLibCheck": true, | ||
"esModuleInterop": false, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"allowImportingTsExtensions": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "CommonJS", | ||
"moduleResolution": "Node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true, | ||
"jsx": "react-jsx", | ||
"typeRoots": ["./types", "./node_modules/@types"], | ||
}, | ||
"include": ["src", "types", "buildContent.ts", "jest.config.js"] | ||
"include": ["src", "types", "buildContent.ts", "exportToCsv.ts", "jest.config.js"], | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters