Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
realappie committed Mar 6, 2020
2 parents b42da7d + d661ca8 commit 6011a09
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 21 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ You will need a `ngx-translate-merge.json` in the root directory that looks a bi
{
"i18nFilesPath": "i18n",
"masterFileName": "en.json",
"autoFix": true,
"overwrite": false
"addMissing": true,
"overwrite": false,
"removeRedundant": false
}
```

Expand All @@ -26,7 +27,9 @@ Flags
Usage: node ngx-translate-tool [options]
Options:
--[no]autofix: Auto fixes translation files
--[no]addMissing: Adds missing translation keys
(default: false)
--[no]removeRedundant: Removes redundant translation keys
(default: false)
--[no]overwrite: Whether to overrwrite original files or not
(default: false)
Expand Down
2 changes: 1 addition & 1 deletion ngx-translate-merge.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"i18nFilesPath": "i18n",
"masterFileName": "en.json",
"autoFix": true,
"addMissing": true,
"overwrite": false
}
4 changes: 2 additions & 2 deletions src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export class Analyzer {
handleFeedback(messages: string[], config: IConfiguration) {
console.log(messages.join('\n'));

if (config.autoFix === false) {
console.log('\x1b[32m%s\x1b[0m', `To fix this, run the command with the --autofix flag or add autofix to true in the configuration file`);
if (config.addMissing === false || config.removeRedundant === false) {
console.log('\x1b[32m%s\x1b[0m', `To fix this, run the command with the --addMissing flag or -removeRedundant to true in the configuration file`);
return null;
} else {
return messages;
Expand Down
16 changes: 11 additions & 5 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FilesManager } from "./files-manager";

/**
* This class will be responsible for loading in the configuration for the tool
* It will also check if the autofix flag was passed or not
* It will also check if the removeRedundant or addMissing flag was passed or not
*/
export class Configuration {
private readonly configPath: string = process.cwd() + '/ngx-translate-merge.json';
Expand All @@ -16,17 +16,23 @@ export class Configuration {
if ( await this.filesManager.pathExists(this.configPath) ) {
const configuration = await this.filesManager.loadConfiguration(this.configPath);

flags.defineBoolean('autofix', false, 'Auto fixes translation files');
flags.defineBoolean('addMissing', false, 'Adds missing translation keys');
flags.defineBoolean('removeRedundant', false, 'Removes redundant translation keys');
flags.defineBoolean('overwrite', false, 'Whether to overrwrite original files or not');
flags.defineString('fileType', 'json', 'What type of translation files');

flags.parse();

if ( flags.isSet('autofix') ) {
configuration.autoFix = flags.get('autofix');
if (flags.isSet('addMissing') ) {
configuration.addMissing = flags.get('addMissing');
}

if ( flags.isSet('removeRedundant')) {
configuration.removeRedundant = flags.get('removeRedundant');
}

if ( flags.isSet('overwrite') ) {
configuration.autoFix = flags.get('overwrite');
configuration.overwrite = flags.get('overwrite');
}

configuration.fileType = flags.get('fileType');
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export class Main {

this.analyzer.handleFeedback(messages, configuration!);

if ( configuration!.autoFix ) {
if ( configuration!.addMissing || configuration!.removeRedundant ) {
const fixedFilesMap = this.translationsDifferenceHandler.handle(
translationsMap!,
differences,
configuration!.masterFileName
configuration!
);

this.translationWriter.write(
Expand Down
14 changes: 7 additions & 7 deletions src/translation-difference-handler.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { ITranslationFile, ITranslationDifference } from "../types";
import { IConfiguration, ITranslationDifference, ITranslationFile } from "../types";

export class TranslationsDifferenceHandler {

handle(originalFiles: Map<string, ITranslationFile>, differencesMap: Map<string, ITranslationDifference>, masterFileName: string): Map<string, ITranslationFile> {
handle(originalFiles: Map<string, ITranslationFile>, differencesMap: Map<string, ITranslationDifference>, configuration: IConfiguration): Map<string, ITranslationFile> {
const newFiles = new Map<string, ITranslationFile>(originalFiles);

const masterFileTranslations = originalFiles.get(masterFileName)!;
const masterFileTranslations = originalFiles.get(configuration.masterFileName)!;

for (const [fileName, value] of differencesMap.entries()) {
for (const fileName of differencesMap.keys()) {
if (differencesMap.get(fileName)!.missing.length || differencesMap.get(fileName)!.redundant.length) {
const newFileValue = newFiles.get(fileName)!;
if (differencesMap.get(fileName)!.redundant.length) {
if (differencesMap.get(fileName)!.redundant.length && configuration.removeRedundant) {
differencesMap.get(fileName)!.redundant.forEach(redundantKey => {
delete newFileValue[redundantKey];
});
}

if (differencesMap.get(fileName)!.missing.length) {
if (differencesMap.get(fileName)!.missing.length && configuration.addMissing) {
differencesMap.get(fileName)!.missing.forEach(missingKey => {
newFileValue[missingKey] = `⚠️ ${masterFileTranslations![missingKey]}`;
newFileValue[missingKey] = `${masterFileTranslations![missingKey]}`;
});
}
newFiles.set(fileName, newFileValue);
Expand Down
4 changes: 3 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ export type TSupportedFileTypes = 'json';
export interface IConfiguration {
i18nFilesPath: string;
masterFileName: string;
autoFix: boolean;
addMissing: boolean;
/** Whether to remove redundant translation keys or not */
removeRedundant: boolean;
/** What type of translations there are */
fileType: TSupportedFileTypes;
/** Whether to overrwrite original files or not */
Expand Down

0 comments on commit 6011a09

Please sign in to comment.