From bed0d4e2326940e035f1be2d8887c8110c4980a5 Mon Sep 17 00:00:00 2001 From: MattIPv4 Date: Fri, 4 Feb 2022 20:59:52 +0000 Subject: [PATCH] Don't rely on Webpack for available languages --- src/nginxconfig/i18n/verify.js | 16 +++++++++++++--- src/nginxconfig/util/language_packs.js | 21 ++++++++++++--------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/nginxconfig/i18n/verify.js b/src/nginxconfig/i18n/verify.js index ddc6e39..b5c386c 100644 --- a/src/nginxconfig/i18n/verify.js +++ b/src/nginxconfig/i18n/verify.js @@ -28,7 +28,7 @@ import { readdirSync, readFileSync } from 'fs'; import { join, sep } from 'path'; import { URL } from 'url'; import chalk from 'chalk'; -import { defaultPack, toSep, fromSep } from '../util/language_packs'; +import { defaultPack, availablePacks, toSep, fromSep } from '../util/language_packs'; import snakeToCamel from '../util/snake_to_camel'; // Recursively get all keys in a i18n pack object fragment @@ -150,10 +150,10 @@ const main = async () => { // Output the pack results if (warnings.length) for (const warning of warnings) - console.log(` ${chalk.yellow('warning')} ${warning}`); + console.warn(` ${chalk.yellow('warning')} ${warning}`); if (errors.length) for (const error of errors) - console.log(` ${chalk.red('error')} ${error}`); + console.error(` ${chalk.red('error')} ${error}`); if (!errors.length && !warnings.length) console.log(` ${chalk.green('No issues')}`); @@ -164,6 +164,16 @@ const main = async () => { console.log(chalk.reset()); } + // Check available language packs + const packKeys = Object.keys(packs); + const missingPacks = packKeys.filter(x => !availablePacks.includes(x)); + const extraPacks = availablePacks.filter(x => !packKeys.includes(x)); + + // Missing packs and extra packs are errors + missingPacks.forEach(pack => console.error(`${chalk.red('error')} Language pack \`${pack}\` not included in \`availablePacks\``)); + extraPacks.forEach(pack => console.error(`${chalk.red('error')} Language pack \`${pack}\` included in \`availablePacks\` but not found`)); + if (missingPacks.length || extraPacks.length) hadError = true; + // Exit 1 if we had errors if (hadError) process.exit(1); }; diff --git a/src/nginxconfig/util/language_packs.js b/src/nginxconfig/util/language_packs.js index 3ca3713..98ee8c7 100644 --- a/src/nginxconfig/util/language_packs.js +++ b/src/nginxconfig/util/language_packs.js @@ -37,12 +37,15 @@ export const toSep = (pack, sep) => pack export const fromSep = (pack, sep) => pack.split(sep, 2)[0].toLowerCase() + (pack.split(sep, 2)[1] || '').toUpperCase(); -// Use webpack magic to get all the language packs we've bundled -// If not in a webpack context, return no packs -/* global __webpack_modules__ */ -export const availablePacks = typeof __webpack_modules__ === 'undefined' - ? [] - : Object.keys(__webpack_modules__) - .map(pack => pack.match(/i18n\/([^/]+)\/languages\.js$/)) - .filter(pack => pack !== null) - .map(pack => fromSep(pack[1], '-')); +// Export a static array of all language packs +export const availablePacks = Object.freeze([ + 'de', + 'en', + 'es', + 'fr', + 'pl', + 'ptBR', + 'ru', + 'zhCN', + 'zhTW', +]);