2024-10-28 13:41:37 -04:00
|
|
|
#!/usr/bin/env node
|
2019-02-11 11:27:32 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview
|
|
|
|
* Script to validate the www translation json
|
|
|
|
*/
|
|
|
|
|
|
|
|
const args = process.argv.slice(2);
|
|
|
|
const usage = `
|
|
|
|
Validate translation json. Usage:
|
2024-10-28 13:41:37 -04:00
|
|
|
node validate_www.mjs path
|
2019-02-11 11:27:32 -05:00
|
|
|
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';
|
2024-10-28 13:41:37 -04:00
|
|
|
import {validateTranslations} from '../lib/validate.mjs';
|
|
|
|
import locales from '../src/supported-locales.mjs';
|
2019-02-11 11:27:32 -05:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
});
|