Created
March 2, 2020 14:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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