@enplug/scripts
Version:
Enplug scripts
53 lines (43 loc) • 2.03 kB
JavaScript
const shell = require('shelljs');
const chalk = require('chalk');
const fs = require('fs');
const { getCrowdinConfig } = require('./crowdin');
async function checkKeys() {
console.log(`\n${chalk.bold('Looking for new translation keys...')}`);
const result = shell.exec('npm run --silent i18n:find -- --emit-error-on-extra-keys', { silent: true });
if (result.code !== 0) {
console.warn(`${chalk.bold.yellow(`Missing or extra keys might have been found.`)}\nRun ${chalk.yellow('npm run i18n:find')} for details.`);
throw new Error('i18n:find script error');
}
console.log(chalk.bold.green('Extracted translation file is up to date.'));
}
async function validateTranslationFile() {
const config = getCrowdinConfig();
// Load file with extracted translations
let translationJson;
try {
translationJson = await fs.promises.readFile(config.localPath, 'utf8');
} catch (error) {
console.error(chalk.bold.red(`Could not load the ${chalk.bold.yellow(`[${config.localPath}]`)} file`));
console.log(`Make sure the ${chalk.yellow(`config.crowdin.localPath`)} in ${chalk.yellow(`package.json`)} points to the correct file.`)
throw new Error(error);
}
// Validate JSON
let translations;
try {
translations = JSON.parse(translationJson);
} catch (error) {
console.error(chalk.bold.red(`Could not parse the ${chalk.bold.yellow(`[${config.localPath}]`)} file as JSON.`));
throw new Error(error);
}
// Validate missing translations
const keysWithMissingTranslation = Object.entries(translations)
.filter(([, value]) => value.includes('Missing value for'))
.map(([key, value]) => key);
if (keysWithMissingTranslation.length > 0) {
console.error(chalk.bold.red(`Missing translations ${chalk.bold.yellow(`[${config.localPath}]`)} for keys:`));
console.log(`${chalk.bold.yellow('[')} '${keysWithMissingTranslation.join(`', '`)}' ${chalk.bold.yellow(']')}`);
throw new Error(error);
}
}
module.exports = { checkKeys, validateTranslationFile };