Skip to content

Instantly share code, notes, and snippets.

@szkrd
Created March 2, 2020 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szkrd/c825c72fe3afa94c5d9da41a7db62322 to your computer and use it in GitHub Desktop.
Save szkrd/c825c72fe3afa94c5d9da41a7db62322 to your computer and use it in GitHub Desktop.
check if locale json file is valid or not (parsable and has unique jeys only) - since git merge may cause headaches
const fs = require('fs');
const path = require('path');
const dir = path.join(__dirname, '/../locales');
fs.readdir(dir, (err, files) => {
files.filter(fn => /\.json$/.test(fn)).forEach(fn => {
fs.readFile(path.join(dir, fn), (err, s) => {
s = s + '';
let lang = {};
try {
lang = JSON.parse(s);
} catch (err) {
console.error(`JSON parse error in file "${fn}"`);
throw err;
}
const line = s.replace(/[\r\n]/g, '');
Object.keys(lang).forEach(key => {
if (line.split(`"${key}":`).length > 2) {
console.log(`Multiple keys in "${fn}": "${key}"`);
throw new Error('JSON key not unique.');
}
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment