Skip to content

Commit

Permalink
feat: Add check-locales script
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Aug 31, 2020
1 parent 41a0860 commit 9a09d5f
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions packages/cozy-scripts/scripts/check-locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const fs = require('fs')
const { ArgumentParser } = require('argparse')

const setDiff = (set1, set2) => {
const res = []
for (const v of set1) {
if (!set2.has(v)) {
res.push(v)
}
}
return res
}

const diff = (obj1, obj2) => {
const paths1 = new Set(paths(obj1))
const paths2 = new Set(paths(obj2))
const removed = Array.from(setDiff(paths1, paths2))
const added = Array.from(setDiff(paths2, paths1))
return [
...added.map(x => ({ type: 'add', value: x })),
...removed.map(x => ({ type: 'remove', value: x }))
]
}

/** Outputs all the dotted paths of an object */
const paths = (obj, current = '') => {
let res = []
for (let [k, v] of Object.entries(obj)) {
if (typeof v === 'object') {
for (const leaf of paths(v, current ? `${current}.${k}` : k)) {
res.push(`${leaf}`)
}
} else {
res.push(`${current}.${k}`)
}
}
return res
}

const main = () => {
const parser = new ArgumentParser({
description: `This scripts checks for differing keys between two locale files
It is used to check if a given locale file (file2) is up to date
with respect to another locale file (file1).
Usage: check-locales.js en.json fr.json`
})
parser.addArgument('file1', {
nargs: '?',
defaultValue: 'src/locales/en.json'
})
parser.addArgument('file2', {
nargs: '?',
defaultValue: 'src/locales/fr.json'
})
const args = parser.parseArgs()

const { file1, file2 } = args
const diffs = diff(
JSON.parse(fs.readFileSync(file1)),
JSON.parse(fs.readFileSync(file2))
)

if (diffs.length > 0) {
for (let d of diffs) {
console.log(d.type === 'add' ? '+' : '-', d.value)
}
console.warn(`Locales ${file1} and ${file2} mismatch, see diff above`)
process.exit(1)
} else {
console.log('Locales have the same keys, everything OK')
process.exit(0)
}
}

if (require.main === module) {
main()
}

0 comments on commit 9a09d5f

Please sign in to comment.