Skip to content

Commit

Permalink
Merge pull request #159 from romanrostislavovich/feat/153
Browse files Browse the repository at this point in the history
feat: Fix Option for Removing Zombies
  • Loading branch information
romanrostislavovich authored Oct 30, 2024
2 parents 762e192 + 223f256 commit 6b02fdc
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 18 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ Options:
(default: "0.9")
-c, --config [path]
Path to the config file.
-fz, --fixZombiesKeys [boolean]
Auto fix zombies keys on languages files
(default: "false")
-V, --version output the version number
Expand Down Expand Up @@ -127,6 +130,7 @@ Default Config is:
"ignoredMisprintKeys": [],
"customRegExpToFindKeys": [ "(?<=marker\\(['\"])([A-Za-z0-9_\\-.]+)(?=['\"]\\))"], // to find: marker('TRSNLATE.KEY');
},
"fixZombiesKeys": false,
"project": "./src/app/**/*.{html,ts}",
"languages": "./src/assets/i18n/*.json"
}
Expand Down Expand Up @@ -180,6 +184,7 @@ const ruleConfig: IRulesConfig = {
emptyKeys: ErrorTypes.warning,
maxWarning: 0,
misprintCoefficient: 0.9,
fixZombiesKeys: false,
ignoredKeys: [ 'EXAMPLE.KEY', 'IGNORED.KEY.(.*)' ], // can be string or RegExp
ignoredMisprintKeys: [],
customRegExpToFindKeys: [ "(?<=marker\\(['\"])([A-Za-z0-9_\\-.]+)(?=['\"]\\))" ] // to find: marker('TRSNLATE.KEY');
Expand Down
7 changes: 5 additions & 2 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
ResultCliModel,
ResultModel,
StatusCodes,
ToggleRule
ToggleRule,
parseJsonFile,
getPackageJsonPath,
} from "./../core";

import { config } from './../core/config';
import { OptionsLongNames, OptionsShortNames } from './enums';
import chalk from 'chalk';
import { parseJsonFile, getPackageJsonPath } from './utils';
import { defaultConfig } from 'sinon';

const name: string = 'ngx-translate-lint';
Expand Down Expand Up @@ -81,6 +82,7 @@ class Cli {

const projectPath: string = this.cliClient.project !== config.defaultValues.projectPath ? this.cliClient.project : options.project;
const languagePath: string = this.cliClient.languages !== config.defaultValues.languagesPath? this.cliClient.languages : options.languages;
const fixZombiesKeys: boolean = this.cliClient.fixZombiesKeys !== config.defaultValues.fixZombiesKeys ? this.cliClient.fixZombiesKeys : options.fixZombiesKeys;

const tsConfigPath: string = options.tsConfigPath;

Expand Down Expand Up @@ -180,6 +182,7 @@ class Cli {
ignoredMisprintKeys: string[] = [],
customRegExpToFindKeys: string[] | RegExp[] = [],
tsConfigPath?: string,
fixZombiesKeys?: boolean,
): void {
const errorConfig: IRulesConfig = {
misprintKeys: misprint || ErrorTypes.disable,
Expand Down
9 changes: 9 additions & 0 deletions src/cli/dictionaries/CliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ const cliOptions: OptionModel[] = [
OptionsPath.absolute
]
}),
new OptionModel({
longName: OptionsLongNames.fixZombiesKeys,
shortName: OptionsShortNames.fixZombiesKeys,
required: false,
type: ArgumentTypes.boolean,
description: `Auto fix zombies keys on languages files`,
additionalDescription: ``,
default: config.defaultValues.fixZombiesKeys?.toString() || 'false',
}),
new OptionModel({
longName: OptionsLongNames.version,
shortName: OptionsShortNames.version,
Expand Down
2 changes: 2 additions & 0 deletions src/cli/enums/OptionsNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum OptionsLongNames {
zombieKeys = 'zombieKeys',
keysOnViews = 'keysOnViews',
misprintKeys = 'misprintKeys',
fixZombiesKeys = 'fixZombiesKeys',
misprintCoefficient = 'misprintCoefficient',
}

Expand All @@ -25,6 +26,7 @@ enum OptionsShortNames {
zombieKeys = 'zk',
keysOnViews = 'kv',
misprintKeys = 'mk',
fixZombiesKeys = 'fz',
misprintCoefficient = 'mc',
}

Expand Down
1 change: 0 additions & 1 deletion src/cli/utils/index.ts

This file was deleted.

48 changes: 38 additions & 10 deletions src/core/client.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import { flatMap } from 'lodash';
import { flatMap, omit } from 'lodash';
import * as path from 'path';
import { config } from './config';
import { ErrorTypes } from './enums';
import { ErrorFlow, ErrorTypes } from './enums';
import { IRulesConfig } from './interface';
import { KeysUtils } from './utils';
import { getPackageJsonPath, KeysUtils, parseJsonFile, saveJsonFile } from './utils';
import { FileLanguageModel, FileViewModel, KeyModel, LanguagesModel, ResultCliModel, ResultErrorModel } from './models';
import { AbsentViewKeysRule, MisprintRule, ZombieRule, EmptyKeysRule } from './rules';
import { AbsentViewKeysRule, EmptyKeysRule, MisprintRule, ZombieRule } from './rules';
import { KeyModelWithLanguages, LanguagesModelWithKey, ViewModelWithKey } from './models/KeyModelWithLanguages';



class NgxTranslateLint {
public rules: IRulesConfig;
public ignore?: string;
public projectPath: string;
public languagesPath: string;
public tsConfigPath: string | undefined;

public ignore?: string;
public languagesPath: string;
public fixZombiesKeys: boolean | undefined;

constructor (
projectPath: string = config.defaultValues.projectPath,
languagesPath: string = config.defaultValues.languagesPath,
ignore?: string,
rulesConfig: IRulesConfig = config.defaultValues.rules,
tsConfigPath?: string,
fixZombiesKeys?: boolean,
) {
this.languagesPath = languagesPath;
this.projectPath = projectPath;
this.ignore = ignore;
this.rules = rulesConfig;
this.projectPath = projectPath;
this.tsConfigPath = tsConfigPath;
this.languagesPath = languagesPath;
this.fixZombiesKeys = fixZombiesKeys;
}

public lint(maxWarning?: number): ResultCliModel {
Expand Down Expand Up @@ -189,9 +192,34 @@ class NgxTranslateLint {
result.push(...ruleResult);
}

if (!!this.fixZombiesKeys) {
const allZombiesKeys: ResultErrorModel[] = result.filter((error) => {
return error.errorFlow === ErrorFlow.zombieKeys;
});

const filesAndKeys: FileLanguageModel[] = allZombiesKeys.reduce((acum: FileLanguageModel[], item: ResultErrorModel) => {
const existingFileLanguage: FileLanguageModel | undefined = acum.find((x) => x.path === item.currentPath) ;
if (!!existingFileLanguage) {
existingFileLanguage.keys.push(new KeyModel(item.value));
} else {
const newFileLanguage: FileLanguageModel = new FileLanguageModel(item.currentPath, [], [new KeyModel(item.value)]);
acum.push(newFileLanguage);
}
return acum;
}, []);

filesAndKeys.forEach((languageFile: FileLanguageModel) => {
// tslint:disable-next-line:no-any
const jsonData: any = parseJsonFile(languageFile.path);
const keysArray: string[] = languageFile.keys.map((x) => x.name);
// tslint:disable-next-line:no-any
const resultData: any = omit(jsonData, keysArray);
saveJsonFile(resultData, languageFile.path);
});
}

return result;
}
}


export { NgxTranslateLint };
3 changes: 2 additions & 1 deletion src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const config: IAppConfig = {
customRegExpToFindKeys: []
},
projectPath: './src/app/**/*.{html,ts,resx}',
languagesPath: './src/assets/i18n/*.json'
languagesPath: './src/assets/i18n/*.json',
fixZombiesKeys: false,
}
};

Expand Down
1 change: 1 addition & 0 deletions src/core/interface/IAppConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface IDefaultValues {
rules: IRulesConfig;
projectPath: string;
languagesPath: string;
fixZombiesKeys?: boolean;
}
interface IAppConfig {
defaultValues: IDefaultValues;
Expand Down
12 changes: 9 additions & 3 deletions src/cli/utils/file.ts → src/core/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';

import chalk from 'chalk';

import { FatalErrorModel } from '../../core';
import { FatalErrorModel } from './../models';

const packageJsonPath: string = './package.json';

Expand All @@ -23,7 +22,14 @@ function getPackageJsonPath(): string {
return result;
}

// tslint:disable-next-line:typedef no-any
function saveJsonFile(data: any, path: string) {
const jsonSpaces: number = 4;
fs.writeFileSync(path, JSON.stringify(data, null, jsonSpaces));
}

export {
getPackageJsonPath,
parseJsonFile
parseJsonFile,
saveJsonFile,
};
1 change: 1 addition & 0 deletions src/core/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './keys';
export * from './file';
export * from './path';
export * from './logger';
export * from './shared';
2 changes: 1 addition & 1 deletion test/integration/config/custom.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"rules": {
"keysOnViews": "warning",
"keysOnViews": "warning ",
"zombieKeys": "disable",
"misprintKeys": "disable",
"deepSearch": "disable",
Expand Down

0 comments on commit 6b02fdc

Please sign in to comment.