From 2de06992570faaa7667ca86684c85154a8faf3cc Mon Sep 17 00:00:00 2001 From: Totto16 Date: Tue, 3 Sep 2024 18:33:28 +0200 Subject: [PATCH] ci: add option for the ci to check locales add support for a "dry-run" to "generateLocale.ts" , so that locales get checked in the ci --- .github/workflows/ci.yml | 3 ++ package.json | 1 + scripts/generateLocale.ts | 58 +++++++++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 72afa9bd..19746989 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,9 @@ jobs: - name: Validate Formatting run: yarn run prettier:check + - name: Validate Locale + run: yarn run check:locale + - name: Lint Extension run: yarn run lint diff --git a/package.json b/package.json index d84a6751..f975e29f 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "scripts": { "build": "yarn run build:ts && yarn run gen:locale && yarn run build:extension", "gen:locale": "yarn run ts-node scripts/generateLocale.ts", + "check:locale": "yarn run ts-node scripts/generateLocale.ts --dry-run", "build:ts": "yarn run clean:ts && rollup -c --failAfterWarnings && sed -i '/setTimeout/d' dist/thirdparty/prismjs.js && sed -i 's/var\\ /let\\ /g' dist/extension.js dist/prefs.js", "clean:ts": "rm -rf ./dist", "build:extension": "yarn run build:schema", diff --git a/scripts/generateLocale.ts b/scripts/generateLocale.ts index 8f0d0590..0aee4145 100644 --- a/scripts/generateLocale.ts +++ b/scripts/generateLocale.ts @@ -5,7 +5,7 @@ import * as gettextParser from 'gettext-parser'; import * as glob from 'glob'; import * as path from 'path'; -const extractStrings = () => { +const extractStrings = (dryRun: boolean) => { const extractor = new GettextExtractor(); extractor @@ -28,16 +28,18 @@ const extractStrings = () => { extractor.savePotFile(`${path.resolve(__dirname)}/../resources/po/pano@elhan.io.pot`); - extractor.printStats(); + if (!dryRun) { + extractor.printStats(); + } }; -const mergeStrings = () => { - fillPotPo.sync({ +const mergeStrings = (dryRun: boolean) => { + return fillPotPo.sync({ potSources: [`${path.resolve(__dirname)}/../resources/po/*.pot`], poSources: [`${path.resolve(__dirname)}/../resources/po/*.po`], - writeFiles: true, + writeFiles: !dryRun, destDir: `${path.resolve(__dirname)}/../resources/po/`, - logResults: true, + logResults: !dryRun, wrapLength: 1000, }); }; @@ -53,6 +55,44 @@ const compileStrings = () => { }); }; -extractStrings(); -mergeStrings(); -compileStrings(); +function main() { + let dryRun = false; + + const lastArg = process.argv.at(-1); + + if (lastArg && ['--dry-run', '--dryRun', '-d'].includes(lastArg)) { + dryRun = true; + } + + extractStrings(dryRun); + const result = mergeStrings(dryRun); + + if (dryRun) { + let errors = 0; + for (const vinyl of result) { + const file = `${path.resolve(__dirname)}/../resources/po/${vinyl.path}`; + + if (!fs.existsSync(file)) { + console.error(`FATAL ERROR: File ${file} doesn't exist, but was reported as existing, this is a bug!`); + process.exit(1); + } + + const actualContent = fs.readFileSync(file); + if (!actualContent.equals(vinyl.contents)) { + console.error(`File ${file} isn't valid, please update it!`); + errors++; + } + } + if (errors > 0) { + process.exit(1); + } else { + console.log('Checked every locales files: No updates necessary'); + } + } + + if (!dryRun) { + compileStrings(); + } +} + +main();