Skip to content

Commit

Permalink
refactor(Printers): Cleanup of printers logic, change for noFile prin…
Browse files Browse the repository at this point in the history
…ters

BREAKING CHANGE: printLanguageReport, printNoLanguageFile and printNoLanguageWhitelistFile now
receive the full languageReport, instead of a few paramters. The parameters from the previous
version are available on this languageReport.
  • Loading branch information
Gertjan Reynaert committed Dec 16, 2016
1 parent 2b5360f commit 2216c42
Showing 1 changed file with 52 additions and 38 deletions.
90 changes: 52 additions & 38 deletions src/manageTranslations.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,51 @@ export default ({
singleMessagesFile = false,
detectDuplicateIds = true,
sortKeys = true,
printers = {},
jsonOptions = {},
overridePrinters = {},
}) => {
if (!messagesDirectory || !translationsDirectory) {
throw new Error('messagesDirectory and translationsDirectory are required');
}

const defaultPrinters = {
printDuplicateIds(duplicateIds) {
header('Duplicate ids:');
if (duplicateIds.length) {
duplicateIds.forEach(id => {
console.log(' ', `Duplicate message id: ${red(id)}`);
});
} else {
console.log(green(' No duplicate ids found, great!'));
}
footer();
},

printLanguageReport(langResults) {
header(`Maintaining ${yellow(langResults.languageFilename)}:`);
printResults({ ...langResults.report, sortKeys });
}

printNoLanguageFile(langResults) {
subheader(`
No existing ${langResults.languageFilename} translation file found.
A new one is created.
`);
}

printNoLanguageWhitelistFile(langResults) {
subheader(```
No existing ${langResults} file found.
A new one is created.
```);
}
};

const printers = {
...defaultPrinters,
...overridePrinters,
}

const stringifyOpts = {
...defaultJSONOptions,
...jsonOptions,
Expand All @@ -41,6 +79,7 @@ export default ({

core(languages, {
provideExtractedMessages: () => readMessageFiles(messagesDirectory),

outputSingleFile: combinedFiles => {
if (singleMessagesFile) {
createSingleMessagesFile({
Expand All @@ -50,27 +89,18 @@ export default ({
});
}
},

outputDuplicateKeys: duplicateIds => {
if (detectDuplicateIds) {
if (typeof printers.printDuplicateIds === 'function') {
printers.printDuplicateIds(duplicateIds);
} else {
header('Duplicate ids:');
if (duplicateIds.length) {
duplicateIds.forEach(id => {
console.log(' ', `Duplicate message id: ${red(id)}`);
});
} else {
console.log(green(' No duplicate ids found, great!'));
}
footer();
}
}
if (detectDuplicateIds) return;

printers.printDuplicateIds(duplicateIds);
},

beforeReporting: () => {
mkdirpSync(translationsDirectory);
mkdirpSync(whitelistsDirectory);
},

provideLangTemplate: lang => {
const languageFilename = `${lang}.json`;
const languageFilepath = Path.join(translationsDirectory, languageFilename);
Expand All @@ -85,24 +115,22 @@ export default ({
whitelistFilepath,
};
},

provideTranslationsFile: lang => {
const filePath = Path.join(translationsDirectory, `${lang}.json`);
const jsonFile = readFile(filePath);
return jsonFile ? JSON.parse(jsonFile) : undefined;
},

provideWhitelistFile: lang => {
const filePath = Path.join(whitelistsDirectory, `whitelist_${lang}.json`);
const jsonFile = readFile(filePath);
return jsonFile ? JSON.parse(jsonFile) : undefined;
},

reportLanguage: langResults => {
if (!langResults.report.noTranslationFile && !langResults.report.noWhitelistFile) {
if (typeof printers.printLanguageReport === 'function') {
printers.printLanguageReport(langResults.languageFilename, langResults.report);
} else {
header(`Maintaining ${yellow(langResults.languageFilename)}:`);
printResults({ ...langResults.report, sortKeys });
}
printers.printLanguageReport(langResults);

writeFileSync(
langResults.languageFilepath,
Expand All @@ -114,26 +142,12 @@ export default ({
);
} else {
if (langResults.report.noTranslationFile) {
if (typeof printers.printNoLanguageFile === 'function') {
printers.printNoLanguageFile(langResults.lang);
} else {
subheader(```
No existing ${langResults.languageFilename} translation file found.
A new one is created.
```);
}
printers.printNoLanguageFile(langResults);
writeFileSync(langResults, stringify(langResults.report.fileOutput, stringifyOpts));
}

if (langResults.report.noWhitelistFile) {
if (typeof printers.printNoLanguageWhitelistFile === 'function') {
printers.printNoLanguageWhitelistFile(langResults.lang);
} else {
subheader(```
No existing ${langResults.whitelistFilename} file found.
A new one is created.
```);
}
printers.printNoLanguageWhitelistFile(langResults);
writeFileSync(langResults.whitelistFilepath, stringify([], stringifyOpts));
}
}
Expand Down

0 comments on commit 2216c42

Please sign in to comment.