scratch-l10n/scripts/validate-www.mjs

64 lines
1.7 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
/**
* @fileoverview
* Script to validate the www translation json
*/
const args = process.argv.slice(2);
const usage = `
Validate translation json. Usage:
node validate_www.mjs path
path: root folder for all the www resource folders
`;
if (args.length < 1) {
process.stdout.write(usage);
process.exit(1);
}
import fs from 'fs';
import path from 'path';
import glob from 'glob';
import async from 'async';
import {validateTranslations} from '../lib/validate.mjs';
import locales from '../src/supported-locales.mjs';
// Globals
const WWW_DIR = path.resolve(args[0]);
const RESOURCES = glob.sync(`${path.resolve(WWW_DIR)}/*`);
const validate = (localeData, callback) => {
fs.readFile(localeData.localeFileName, function (err, data) {
if (err) callback(err);
// let this throw an error if invalid json
data = JSON.parse(data);
const translations = {
locale: localeData.locale,
translations: data
};
validateTranslations(translations, localeData.sourceData);
});
};
const validateResource = (resource, callback) => {
const source = JSON.parse(fs.readFileSync(`${resource}/en.json`));
const allLocales = Object.keys(locales).map(loc => {
return {
locale: loc,
localeFileName: `${resource}/${loc}.json`,
sourceData: source
};
});
async.each(allLocales, validate, function (err) {
if (err) {
callback(err);
}
});
};
async.each(RESOURCES, validateResource, function (err) {
if (err) {
console.error(err); // eslint-disable-line no-console
process.exit(1);
}
});