Skip to content

Commit

Permalink
Export content to CSV format script (#4569)
Browse files Browse the repository at this point in the history
  • Loading branch information
gewfy authored Jan 11, 2024
2 parents e227933 + ff0117e commit 2dbd75a
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 10 deletions.
83 changes: 83 additions & 0 deletions content/exportToCsv.ts
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';

Check warning on line 4 in content/exportToCsv.ts

View workflow job for this annotation

GitHub Actions / Test / Content

'LANGUAGES' is defined but never used
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();
2 changes: 2 additions & 0 deletions content/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.23.3",
"@fast-csv/format": "^4.3.5",
"@types/jest": "^29.5.11",
"@types/node": "^20.10.7",
"@types/ramda": "^0.29.9",
Expand All @@ -30,6 +31,7 @@
"eslint-plugin-prettier": "^5.1.2",
"jest": "^29.7.0",
"prettier": "^3.1.1",
"safe-flat": "^2.1.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
Expand Down
15 changes: 5 additions & 10 deletions content/tsconfig.json
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"],
}

47 changes: 47 additions & 0 deletions content/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,18 @@
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.56.0.tgz#ef20350fec605a7f7035a01764731b2de0f3782b"
integrity sha512-gMsVel9D7f2HLkBma9VbtzZRehRogVRfbr++f06nL2vnCGCNlzOD+/MUov/F4p8myyAHspEhVobgjpX64q5m6A==

"@fast-csv/format@^4.3.5":
version "4.3.5"
resolved "https://registry.yarnpkg.com/@fast-csv/format/-/format-4.3.5.tgz#90d83d1b47b6aaf67be70d6118f84f3e12ee1ff3"
integrity sha512-8iRn6QF3I8Ak78lNAa+Gdl5MJJBM5vRHivFtMRUWINdevNo00K7OXxS2PshawLKTejVwieIlPmK5YlLu6w4u8A==
dependencies:
"@types/node" "^14.0.1"
lodash.escaperegexp "^4.1.2"
lodash.isboolean "^3.0.3"
lodash.isequal "^4.5.0"
lodash.isfunction "^3.0.9"
lodash.isnil "^4.0.0"

"@humanwhocodes/config-array@^0.11.13":
version "0.11.13"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.13.tgz#075dc9684f40a531d9b26b0822153c1e832ee297"
Expand Down Expand Up @@ -892,6 +904,11 @@
dependencies:
undici-types "~5.26.4"

"@types/node@^14.0.1":
version "14.18.63"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.18.63.tgz#1788fa8da838dbb5f9ea994b834278205db6ca2b"
integrity sha512-fAtCfv4jJg+ExtXhvCkCqUKZ+4ok/JQk01qDKhL5BDDoS3AxKXhV5/MAVUZyQnSEd2GT92fkgZl0pz0Q0AzcIQ==

"@types/ramda@^0.29.9":
version "0.29.9"
resolved "https://registry.yarnpkg.com/@types/ramda/-/ramda-0.29.9.tgz#a4c1a9d056249268ffe16c2f6d198e42c2fc994a"
Expand Down Expand Up @@ -2993,6 +3010,31 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

lodash.escaperegexp@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz#64762c48618082518ac3df4ccf5d5886dae20347"
integrity sha512-TM9YBvyC84ZxE3rgfefxUWiQKLilstD6k7PTGt6wfbtXF8ixIJLOL3VYyV/z+ZiPLsVxAsKAFVwWlWeb2Y8Yyw==

lodash.isboolean@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz#6c2e171db2a257cd96802fd43b01b20d5f5870f6"
integrity sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==

lodash.isequal@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
integrity sha512-pDo3lu8Jhfjqls6GkMgpahsF9kCyayhgykjyLMNFTKWrpVdAQtYyB4muAMWozBB4ig/dtWAmsMxLEI8wuz+DYQ==

lodash.isfunction@^3.0.9:
version "3.0.9"
resolved "https://registry.yarnpkg.com/lodash.isfunction/-/lodash.isfunction-3.0.9.tgz#06de25df4db327ac931981d1bdb067e5af68d051"
integrity sha512-AirXNj15uRIMMPihnkInB4i3NHeb4iBtNg9WRWuK2o31S+ePwwNmDPaTL3o7dTJ+VXNZim7rFs4rxN4YU1oUJw==

lodash.isnil@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/lodash.isnil/-/lodash.isnil-4.0.0.tgz#49e28cd559013458c814c5479d3c663a21bfaa6c"
integrity sha512-up2Mzq3545mwVnMhTDMdfoG1OurpA/s5t88JmQX809eH3C8491iu2sfKhTfhQtKY78oPNhiaHJUpT/dUDAAtng==

lodash.memoize@4.x:
version "4.1.2"
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
Expand Down Expand Up @@ -3500,6 +3542,11 @@ safe-array-concat@^1.0.1:
has-symbols "^1.0.3"
isarray "^2.0.5"

safe-flat@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/safe-flat/-/safe-flat-2.1.0.tgz#6feb2907683e04a67349b292d9ce88d4323aacf7"
integrity sha512-qr5iVWMYuN21dkijya23k6apc2BV1hiCG75vjToKDTzWlbR4SLbLbCnowPJ2pngnwGT2nMEeZKOglBE4pksj6g==

safe-regex-test@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/safe-regex-test/-/safe-regex-test-1.0.0.tgz#793b874d524eb3640d1873aad03596db2d4f2295"
Expand Down

0 comments on commit 2dbd75a

Please sign in to comment.