From 04cf87c03ef7a3a388f7574b658d732c24313b6a Mon Sep 17 00:00:00 2001 From: HF Date: Sun, 10 Mar 2024 22:22:21 +0100 Subject: [PATCH] add language file validation to build script --- scripts/build.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/scripts/build.js b/scripts/build.js index e4d7baa1..f499bb6b 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -8,6 +8,7 @@ const fs = require('fs'); const readline = require('readline'); const { spawn } = require('child_process'); const webpack = require('webpack'); +const validate = require("ttag-cli/dist/src/commands/validate").default; const minifyCss = require('./minifyCss'); const serverConfig = require('../webpack.config.server.js'); @@ -18,6 +19,7 @@ let doBuildServer = false; let doBuildClient = false; let parallel = false; let recursion = false; +let onlyValidate = false; for (let i = 0; i < process.argv.length; i += 1) { switch (process.argv[i]) { case '--langs': { @@ -37,6 +39,9 @@ for (let i = 0; i < process.argv.length; i += 1) { case '--recursion': recursion = true; break; + case '--validate': + onlyValidate = true; + break; default: // nothing } @@ -128,6 +133,36 @@ async function filterLackingLocals(langs, percentage) { }; } +/* + * check if language files contain errors + */ +function validateLangs(langs) { + console.log('Validating language files...'); + const langDir = path.resolve(__dirname, '..', 'i18n'); + const brokenLangs = []; + for (const lang of langs) { + const langFiles = [`${lang}.po`, `ssr-${lang}.po`]; + for (const langFile of langFiles) { + process.stdout.clearLine(0); + process.stdout.cursorTo(0); + process.stdout.write(`i18n/${langFile} `); + filePath = path.join(langDir, langFile); + if (!fs.existsSync(filePath)) { + continue; + } + try { + validate(filePath); + } catch { + brokenLangs.push(langFile); + } + } + } + process.stdout.clearLine(0); + process.stdout.cursorTo(0); + return brokenLangs; +} + + function compile(webpackConfig) { return new Promise((resolve, reject) => { webpack(webpackConfig, (err, stats) => { @@ -226,7 +261,7 @@ function buildClientsParallel(avlangs) { return Promise.all(promises); } -async function buildProduction() { +async function build() { const st = Date.now(); // cleanup old files if (!recursion) { @@ -257,6 +292,18 @@ async function buildProduction() { } console.log('Building', avlangs.length, 'locales:', avlangs); + const brokenLangs = validateLangs(avlangs); + if (brokenLangs.length) { + console.error('ERROR: Translation files', brokenLangs, 'contain errors.'); + process.exit(2); + return; + } + if (onlyValidate) { + console.log('Validation complete, everything is fine.'); + process.exit(0); + return; + } + const promises = []; if (doBuildServer) { @@ -296,4 +343,4 @@ async function buildProduction() { } } -buildProduction(); +build();