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

fixed passing params to i18nGenerate

parent f08ec6a5
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
"scripts": { "scripts": {
"clean": "rimraf lib", "clean": "rimraf lib",
"build": "gg-i18n && tsc", "build": "gg-i18n && tsc",
"i18nGenerate": "gg-i18n", "i18nGenerate": "gg-i18n --moduleName=client",
"test": "jest" "test": "jest"
}, },
"dependencies": { "dependencies": {
......
...@@ -7,6 +7,7 @@ const md5 = require('js-md5'); ...@@ -7,6 +7,7 @@ const md5 = require('js-md5');
const mkdirp = require('mkdirp'); const mkdirp = require('mkdirp');
const rimraf = require('rimraf'); const rimraf = require('rimraf');
const Path = require('path'); const Path = require('path');
const minimist = require('minimist');
// console.log('Env', process.env); // console.log('Env', process.env);
// console.log('Args' , process.argv); // console.log('Args' , process.argv);
...@@ -21,6 +22,12 @@ const getPrefix = (path) => { ...@@ -21,6 +22,12 @@ const getPrefix = (path) => {
return path.substring(path.indexOf('/src/') + '/src/'.length, path.indexOf('/translations.json')); 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 // TODO fix generated paths
fg([`${rootDir}/src/**/translations.json`, `${rootDir}/rc/translations.json`]) fg([`${rootDir}/src/**/translations.json`, `${rootDir}/rc/translations.json`])
.then((entries) => { .then((entries) => {
...@@ -39,24 +46,24 @@ fg([`${rootDir}/src/**/translations.json`, `${rootDir}/rc/translations.json`]) ...@@ -39,24 +46,24 @@ fg([`${rootDir}/src/**/translations.json`, `${rootDir}/rc/translations.json`])
if (!fs.existsSync(`${rootDir}/locales/en`)) { if (!fs.existsSync(`${rootDir}/locales/en`)) {
mkdirp.sync(`${rootDir}/locales/en`); // create dir if no exist 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(() => { .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); JSON.parse(content);
}).catch((error) => { }).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); process.exit(-1);
}) })
.then(() => generateHashedLocales()); .then(() => generateHashedLocales());
const generateHashedLocales = () => { const generateHashedLocales = () => {
const includedLocales = process.env.includedLocales const included = includedLocales
? process.env.includedLocales.replace(/[\[\]]/gi, '').split(',') ? includedLocales.replace(/[\[\]]/gi, '').split(',')
: []; : [];
rimraf(`${rootDir}/generated/locales`, (err) => { // delete old generated locales 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) => { .then((entries) => {
const localeHashMapping = {}; // map commonPath -> pathWithHash const localeHashMapping = {}; // map commonPath -> pathWithHash
mkdirp(`${rootDir}/generated/locales`, () => { mkdirp(`${rootDir}/generated/locales`, () => {
...@@ -73,9 +80,9 @@ const generateHashedLocales = () => { ...@@ -73,9 +80,9 @@ const generateHashedLocales = () => {
fs.writeFileSync(`${rootDir}/${newFilePath}`, fileContent, { flag: 'wx' }); fs.writeFileSync(`${rootDir}/${newFilePath}`, fileContent, { flag: 'wx' });
localeHashMapping[mappingPath] = newFilePath.replace('./generated', ''); // deleting '/generated' from new path for further loading localeHashMapping[mappingPath] = newFilePath.replace('./generated', ''); // deleting '/generated' from new path for further loading
// console.log(`Generated ${ newFilePath } for ${ path }`); // 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}`); 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); 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