TT#107100 fix path issue in i18n:extract tool

Issue:
The tool cannot save updated language files on Linux\Mac platform, because of wrong path concatenation

Change-Id: I7920649cb0f1f643875a5cc40eabeb58160bd112
mr9.3
Sergii Leonenko 5 years ago
parent 9f1b25c5f9
commit 57c16b9c89

@ -132,7 +132,7 @@
return targetFiles.map(f => { return targetFiles.map(f => {
const langPath = path.resolve(process.cwd(), f); const langPath = path.resolve(process.cwd(), f);
const extension = langPath.substring(langPath.lastIndexOf('.')).toLowerCase(); const extension = path.extname(langPath).toLowerCase();
const isJSON = extension === '.json'; const isJSON = extension === '.json';
const isYAML = extension === '.yaml' || extension === '.yml'; const isYAML = extension === '.yaml' || extension === '.yml';
let langObj; let langObj;
@ -148,7 +148,7 @@
const fileName = f.replace(process.cwd(), ''); const fileName = f.replace(process.cwd(), '');
return { return {
fileName, fileName,
path: f, path: langPath,
content: JSON.stringify(langObj) content: JSON.stringify(langObj)
}; };
}); });
@ -203,16 +203,16 @@
} }
} }
}); });
const fileExtension = languageFile.fileName.substring(languageFile.fileName.lastIndexOf('.') + 1); const fileExtension = path.extname(languageFile.fileName).toLowerCase();
const filePath = path.resolve(process.cwd(), languageFile.fileName); const filePath = languageFile.path;
const stringifiedContent = outputOptions.sortKeys ? sortedJSONStringify(languageFileContent, outputOptions.indentationString) : JSON.stringify(languageFileContent, null, outputOptions.indentationString || 2); const stringifiedContent = outputOptions.sortKeys ? sortedJSONStringify(languageFileContent, outputOptions.indentationString) : JSON.stringify(languageFileContent, null, outputOptions.indentationString || 2);
if (fileExtension === 'json') { if (fileExtension === '.json') {
fs.writeFileSync(filePath, stringifiedContent); fs.writeFileSync(filePath, stringifiedContent);
} else if (fileExtension === 'js') { } else if (fileExtension === '.js') {
const jsFile = `export default ${stringifiedContent}; \n`; const jsFile = `export default ${stringifiedContent}; \n`;
fs.writeFileSync(filePath, jsFile); fs.writeFileSync(filePath, jsFile);
} else if (fileExtension === 'yaml' || fileExtension === 'yml') { } else if (fileExtension === '.yaml' || fileExtension === '.yml') {
const yamlFile = yaml.safeDump(languageFileContent); const yamlFile = yaml.safeDump(languageFileContent);
fs.writeFileSync(filePath, yamlFile); fs.writeFileSync(filePath, yamlFile);
} }
@ -343,6 +343,14 @@
}); });
} }
if (!Array.prototype.flatMap) {
function flatMap(f, ctx) {
// @ts-ignore
return this.reduce((r, x, i, a) => r.concat(f.call(ctx, x, i, a)), []);
}
Array.prototype.flatMap = flatMap;
}
function createI18NReport(vueFiles, languageFiles, command, missingKeysOptions) { function createI18NReport(vueFiles, languageFiles, command, missingKeysOptions) {
const resolvedVueFiles = path.resolve(process.cwd(), vueFiles); const resolvedVueFiles = path.resolve(process.cwd(), vueFiles);
const resolvedLanguageFiles = path.resolve(process.cwd(), languageFiles); const resolvedLanguageFiles = path.resolve(process.cwd(), languageFiles);
@ -408,9 +416,15 @@
const report = createI18NReport(vueFiles, languageFiles, command, missingKeysOptions); const report = createI18NReport(vueFiles, languageFiles, command, missingKeysOptions);
if (detailedReport) { if (detailedReport) {
if (report.missingKeys) console.info('missing keys: '), console.table(report.missingKeys); const normalizeKeysInfo = keyInfo => {
if (report.unusedKeys) console.info('unused keys: '), console.table(report.unusedKeys); delete keyInfo.translated;
if (report.dynamicKeys && dynamic && dynamic > 1) console.info('dynamic detected keys: '), console.table(report.dynamicKeys); if (keyInfo.file) keyInfo.file = path.relative(process.cwd(), keyInfo.file).replace(/\\/g, '/');
return keyInfo;
};
if (report.missingKeys) console.info('missing keys: '), console.table([...report.missingKeys].map(normalizeKeysInfo));
if (report.unusedKeys) console.info('unused keys: '), console.table([...report.unusedKeys].map(normalizeKeysInfo));
if (report.dynamicKeys && dynamic && dynamic > 1) console.info('dynamic detected keys: '), console.table([...report.dynamicKeys].map(normalizeKeysInfo));
} }
if (output) { if (output) {

Loading…
Cancel
Save