This repository has been archived by the owner on May 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #367 from pdehaan/issue-364
Generate production locales using 'compare-locales'
- Loading branch information
Showing
4 changed files
with
120 additions
and
12 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
rules: | ||
node/shebang: off | ||
security/detect-child-process: off | ||
|
||
no-console: off | ||
no-process-exit: off |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/usr/bin/env node | ||
|
||
const cp = require('child_process'); | ||
const { promisify } = require('util'); | ||
const fs = require('fs'); | ||
const pkg = require('../package.json'); | ||
|
||
const availableLanguages = pkg.availableLanguages.sort(); | ||
const exec = promisify(cp.exec); | ||
|
||
const arrayDiff = (current, package) => | ||
current.filter(locale => !package.includes(locale)); | ||
|
||
const cmd = 'compare-locales l10n.toml . `ls public/locales` --data=json'; | ||
|
||
exec(cmd) | ||
.then(({ stdout }) => JSON.parse(stdout)) | ||
.then(({ summary }) => { | ||
const locales = Object.keys(summary) | ||
.filter(locale => { | ||
const loc = summary[locale]; | ||
const hasMissing = loc.hasOwnProperty('missing'); | ||
const hasErrors = loc.hasOwnProperty('errors'); | ||
return !hasMissing && !hasErrors; | ||
}) | ||
.sort(); | ||
|
||
if (locales.join(',') !== availableLanguages.join(',')) { | ||
const missingLanguages = arrayDiff(locales, availableLanguages); | ||
|
||
console.log('current 100%:', JSON.stringify(locales)); | ||
console.log('package.json:', JSON.stringify(availableLanguages)); | ||
console.log('missing prod:', JSON.stringify(missingLanguages)); | ||
|
||
if (process.argv.includes('--write')) { | ||
const pkgPath = require.resolve('../package.json'); | ||
pkg.availableLanguages = locales; | ||
const str = JSON.stringify(pkg, null, 2) + '\n'; | ||
console.log('Updating /package.json availableLanguages'); | ||
fs.writeFileSync(pkgPath, str, 'utf-8'); | ||
} | ||
} else { | ||
console.log('Production locales are up to date!'); | ||
} | ||
}) | ||
.catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env node | ||
|
||
const cp = require('child_process'); | ||
const { promisify } = require('util'); | ||
const pkg = require('../package.json'); | ||
const conf = require('../server/config'); | ||
|
||
const exec = promisify(cp.exec); | ||
const cmd = `compare-locales l10n.toml . ${getLocales()} --data=json`; | ||
|
||
console.log(cmd); | ||
|
||
exec(cmd) | ||
.then(({ stdout }) => JSON.parse(stdout)) | ||
.then(({ details }) => filterErrors(details)) | ||
.then(results => { | ||
if (results.length) { | ||
results.forEach(({ locale, data }) => { | ||
console.log(locale); | ||
data.forEach(msg => console.log(`- ${msg}`)); | ||
console.log(''); | ||
}); | ||
process.exit(2); | ||
} | ||
}) | ||
.catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
|
||
function filterErrors(details) { | ||
return Object.keys(details) | ||
.sort() | ||
.map(locale => { | ||
const data = details[locale] | ||
.filter(item => item.hasOwnProperty('error')) | ||
.map(({ error }) => error); | ||
return { locale, data }; | ||
}) | ||
.filter(({ data }) => data.length); | ||
} | ||
|
||
function getLocales() { | ||
// If we're in a "production" env (or passed the `--production` flag), only | ||
// check the locales from the package.json file's `availableLanguages` array. | ||
if (conf.env === 'production' || process.argv.includes('--production')) { | ||
return pkg.availableLanguages.sort().join(' '); | ||
} | ||
// Lint all the locales. | ||
return '`ls public/locales`'; | ||
} |