From ba305c65c8d840e9a5e4cbde0748c6c0c6b1d3d6 Mon Sep 17 00:00:00 2001 From: zyf722 Date: Thu, 12 Dec 2024 00:18:25 +0800 Subject: [PATCH] feat(ci): update .lokalise.json after pulling from Lokalise --- .github/workflows/i18n-update-pull.yml | 3 ++ scripts/i18n-lokalise-json.mjs | 50 +++++++++++++++++++------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/.github/workflows/i18n-update-pull.yml b/.github/workflows/i18n-update-pull.yml index d18da9732..8022b2dfe 100644 --- a/.github/workflows/i18n-update-pull.yml +++ b/.github/workflows/i18n-update-pull.yml @@ -102,6 +102,9 @@ jobs: mkdir -p $LOKALISE_TEMP && touch $LOKALISE_TEMP/locales.zip && npm run i18n-update-pull -- $PR_BRANCH && rm -rf $LOKALISE_TEMP env: LOKALISE_TEMP: lokalise_tmp + + - name: Generate Lokalise JSON files + run: npm run i18n-lokalise-json all - name: Linting and fixing run: npm run fix diff --git a/scripts/i18n-lokalise-json.mjs b/scripts/i18n-lokalise-json.mjs index 4612796c0..09ced9fb1 100644 --- a/scripts/i18n-lokalise-json.mjs +++ b/scripts/i18n-lokalise-json.mjs @@ -25,31 +25,34 @@ const parseObject = (node) => { result[key] = parseValue(prop.value); }); return result; -} +}; const parseValue = (node) => { switch (node.type) { - case "ObjectExpression": + case 'ObjectExpression': return parseObject(node); - case "ArrayExpression": + case 'ArrayExpression': return node.elements.map(parseValue); - case "StringLiteral": - case "NumericLiteral": - case "BooleanLiteral": + case 'StringLiteral': + case 'NumericLiteral': + case 'BooleanLiteral': return node.value; - case "NullLiteral": + case 'NullLiteral': return null; default: throw new Error(`Unsupported node type: ${node.type}`); } -} +}; -const generateLokaliseJSON = async () => { - const lang = process.argv[2]; +/** @param {string} lang */ +const generateLokaliseJSON = async (lang) => { const srcDir = path.resolve('src/livecodes/i18n/locales/' + lang); if (!fs.existsSync(srcDir)) { console.error(`Language ${srcDir} does not exist.`); return; + } else if (lang === 'en') { + console.warn('This script is not intended to be run for English language.\nPlease use `npm run i18n-export` instead.'); + return } const files = fs @@ -57,7 +60,7 @@ const generateLokaliseJSON = async () => { .filter((file) => file.endsWith('.ts')) .map((file) => path.resolve(srcDir, file)); - await Promise.all( + return Promise.all( files.map(async (file) => { try { console.log(`Generating Lokalise JSON for ${file} in language ${lang}...`); @@ -83,7 +86,10 @@ const generateLokaliseJSON = async () => { } const outFile = path.resolve(srcDir, file.replace('.ts', '.lokalise.json')); - await fs.promises.writeFile(outFile, sortedJSONify(result).replace(/<(\/?)(\d+)>/g, '<$1tag-$2>')); + await fs.promises.writeFile( + outFile, + sortedJSONify(result).replace(/<(\/?)(\d+)>/g, '<$1tag-$2>'), + ); } catch (err) { console.error(err); } @@ -91,4 +97,22 @@ const generateLokaliseJSON = async () => { ); }; -generateLokaliseJSON(); +const main = async () => { + const langs = new Set(process.argv.slice(2)); + + if (langs.has('all')) { + langs.delete('all'); + const srcDir = path.resolve('src/livecodes/i18n/locales'); + const files = fs + .readdirSync(srcDir) + .filter( + (file) => + fs.statSync(path.resolve(srcDir, file)).isDirectory() && file !== 'en' && file !== 'tmp', + ); + files.forEach((file) => langs.add(file)); + } + + await Promise.all([...langs].map(generateLokaliseJSON)); +}; + +main();