Commit d3809452 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov
Browse files

fixed passing params to i18nGenerate

parent f08ec6a5
......@@ -5,7 +5,7 @@
"scripts": {
"clean": "rimraf lib",
"build": "gg-i18n && tsc",
"i18nGenerate": "gg-i18n",
"i18nGenerate": "gg-i18n --moduleName=client",
"test": "jest"
},
"dependencies": {
......
......@@ -7,6 +7,7 @@ const md5 = require('js-md5');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const Path = require('path');
const minimist = require('minimist');
// console.log('Env', process.env);
// console.log('Args' , process.argv);
......@@ -21,6 +22,12 @@ const getPrefix = (path) => {
return path.substring(path.indexOf('/src/') + '/src/'.length, path.indexOf('/translations.json'));
};
const args = minimist(process.argv.slice(2));
const moduleName = args.moduleName;
const includedLocales = args.includedLocales;
// console.log(args);
// TODO fix generated paths
fg([`${rootDir}/src/**/translations.json`, `${rootDir}/rc/translations.json`])
.then((entries) => {
......@@ -39,24 +46,24 @@ fg([`${rootDir}/src/**/translations.json`, `${rootDir}/rc/translations.json`])
if (!fs.existsSync(`${rootDir}/locales/en`)) {
mkdirp.sync(`${rootDir}/locales/en`); // create dir if no exist
}
fs.writeFileSync(`${rootDir}/locales/en/${process.env.moduleName || 'translations'}.json`, JSON.stringify(content, null, 2))
fs.writeFileSync(`${rootDir}/locales/en/${moduleName || 'translations'}.json`, JSON.stringify(content, null, 2))
})
.then(() => {
const content = fs.readFileSync(`${rootDir}/locales/en/${process.env.moduleName || 'translations'}.json`);
const content = fs.readFileSync(`${rootDir}/locales/en/${moduleName || 'translations'}.json`);
JSON.parse(content);
}).catch((error) => {
console.log(`Error reading resulting locales/en/${process.env.moduleName || 'translations'}.json`, error);
console.log(`Error reading resulting locales/en/${moduleName || 'translations'}.json`, error);
process.exit(-1);
})
.then(() => generateHashedLocales());
const generateHashedLocales = () => {
const includedLocales = process.env.includedLocales
? process.env.includedLocales.replace(/[\[\]]/gi, '').split(',')
const included = includedLocales
? includedLocales.replace(/[\[\]]/gi, '').split(',')
: [];
rimraf(`${rootDir}/generated/locales`, (err) => { // delete old generated locales
fg([`${rootDir}/locales/**/${process.env.moduleName || 'translations'}.json`, ...includedLocales]) // scan for all translations files
fg([`${rootDir}/locales/**/${moduleName || 'translations'}.json`, ...included]) // scan for all translations files
.then((entries) => {
const localeHashMapping = {}; // map commonPath -> pathWithHash
mkdirp(`${rootDir}/generated/locales`, () => {
......@@ -73,9 +80,9 @@ const generateHashedLocales = () => {
fs.writeFileSync(`${rootDir}/${newFilePath}`, fileContent, { flag: 'wx' });
localeHashMapping[mappingPath] = newFilePath.replace('./generated', ''); // deleting '/generated' from new path for further loading
// console.log(`Generated ${ newFilePath } for ${ path }`);
if (mappingPath.endsWith(`/${process.env.moduleName}.json`)) {
if (mappingPath.endsWith(`/${moduleName}.json`)) {
console.log(`Registering 'translations.json' mapping for ${mappingPath}`);
localeHashMapping[mappingPath.replace(`/${process.env.moduleName}.json`, '/translations.json')] = localeHashMapping[mappingPath];
localeHashMapping[mappingPath.replace(`/${moduleName}.json`, '/translations.json')] = localeHashMapping[mappingPath];
}
});
console.log('Locale mappings', localeHashMapping);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment