Skip to content

Commit

Permalink
feat: Add version flag to the CLI
Browse files Browse the repository at this point in the history
Close #125
  • Loading branch information
romanrostislavovich committed Nov 2, 2022
1 parent a7ad38d commit c001035
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 29 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,46 @@ Options:
The path to languages folder
Possible Values: <relative path|absolute path>
(default: "./src/assets/i18n/*.json")
-v, --keysOnViews [enum]
-kv, --keysOnViews [enum]
Described how to handle the error of missing keys on view
Possible Values: <disable|warning|error>
(default: "error")
-z, --zombieKeys [enum]
-zk, --zombieKeys [enum]
Described how to handle the error of zombies keys
Possible Values: <disable|warning|error>
(default: "warning")
-e, --emptyKeys [enum]
-ek, --emptyKeys [enum]
Described how to handle empty value on translate keys
Possible Values: <disable|warning|error>
(default: "warning")
-i, --ignore [glob]
Ignore projects and languages files
Possible Values: <relative path|absolute path>
-m, --misprintKeys [enum]
--maxWarning [glob]
Max count of warnings in all files. If this value more that count of warnings, then an error is return
Possible Values: <number>
(default: "0")
-mk, --misprintKeys [enum]
Try to find matches with misprint keys on views and languages keys. CCan be longer process!!
Possible Values: <disable|warning|error>
(default: "disable")
-d, --deepSearch [enum]
-ds, --deepSearch [enum]
Add each translate key to global regexp end try to find them on project. Can be longer process!!
Possible Values: <disable|enable>
(default: "disable")
-mc, --misprintCoefficient [number]
Coefficient for misprint option can be from 0 to 1.0.
(default: "0.9")
(default: "0.9")
-c, --config [path]
Path to the config file.
--maxWarning [number]
Max count of warnings in all files. If this value more that count of warnings, then an error is return
Possible Values: <number>
(default: "0")
-t, --tsconfig [path] [BETA]
The beta argument which enables AST parsing for the angular project. Need to set up the path to the tsconfig file. Right now included only one rule: checked if `ngx-translate` imported for your project
Possible Values: <relative path|absolute path>
(default: "./")
-h, --help output usage information
-V, --version output the version number
-h, --help output usage information
Examples:
Expand Down
6 changes: 6 additions & 0 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: './package.json',
output: {
filename: '../dist/package.json',
},
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"lint:markdown": "markdownlint --config ./config/markdownlint.json ./docs/**/*.md",
"test:unit": "nyc mocha -r ts-node/register ./test/unit/*.test.ts",
"test:integration": "mocha -r ts-node/register ./test/integration/*.test.ts",
"build": "tsc -p .",
"build": "tsc -p . && webpack -c config/webpack.config.js",
"lint": "npm run lint:typescript && npm run lint:markdown",
"test": "npm run lint && npm run build && npm run test:integration",
"semantic-release": "env-cmd semantic-release --no-ci",
Expand Down Expand Up @@ -109,6 +109,8 @@
"ts-node": "^9.0.0",
"tslint": "^5.11.0",
"typedoc": "^0.15.0",
"typescript": "^4.1.2"
"typescript": "^4.1.2",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
}
}
15 changes: 14 additions & 1 deletion src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import {
} from "./../core";

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

const name: string = 'ngx-translate-lint';
const packageJsonPath: string = './package.json';

// tslint:disable-next-line:no-any
const docs: any = {
name,
Expand Down Expand Up @@ -59,6 +61,9 @@ class Cli {
this.cliClient.option(optionFlag, optionDescription, optionDefaultValue);
});

const packageJson: any = parseJsonFile(packageJsonPath);
this.cliClient.version(packageJson.version);

this.cliClient
.name(docs.name)
.usage(docs.usage)
Expand Down Expand Up @@ -119,6 +124,8 @@ class Cli {
}


this.printCurrentVersion();

if (options.project && options.languages) {
this.runLint(
projectPath, languagePath, optionZombiesRule,
Expand Down Expand Up @@ -203,6 +210,12 @@ class Cli {
throw new FatalErrorModel(chalk.red(resultModel.message));
}
}

private printCurrentVersion(): void {
const packageJson: any = parseJsonFile(packageJsonPath);
// tslint:disable-next-line:no-console
console.log(`Current version: ${packageJson.version}`);
}
}

export { Cli };
8 changes: 8 additions & 0 deletions src/cli/dictionaries/CliOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ const cliOptions: OptionModel[] = [
OptionsPath.absolute
]
}),
new OptionModel({
longName: OptionsLongNames.version,
shortName: OptionsShortNames.version,
required: false,
description: 'Print current version of ngx-translate-lint',
type: ArgumentTypes.undefined,
additionalDescription: '',
}),
];

export { cliOptions };
12 changes: 7 additions & 5 deletions src/cli/enums/OptionsNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ enum OptionsLongNames {
help = 'help',
ignore = 'ignore',
config = 'config',
version = 'version',
project = 'project',
tsconfig = 'tsconfig',
emptyKeys = 'emptyKeys',
Expand All @@ -19,13 +20,14 @@ enum OptionsShortNames {
ignore = 'i',
config = 'c',
project = 'p',
version = 'v',
tsconfig = 't',
languages = 'l',
emptyKeys = 'e',
deepSearch = 'd',
zombieKeys = 'z',
keysOnViews = 'v',
misprintKeys = 'm',
emptyKeys = 'ek',
deepSearch = 'ds',
zombieKeys = 'zk',
keysOnViews = 'kv',
misprintKeys = 'mk',
misprintCoefficient = 'mc',
}

Expand Down
4 changes: 2 additions & 2 deletions src/cli/models/Option.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Argument, IArgument } from 'conventional-cli';
import { Argument, ArgumentTypes, IArgument } from 'conventional-cli';

class OptionModel extends Argument {
constructor(
Expand All @@ -18,7 +18,7 @@ class OptionModel extends Argument {
}

public getFlag(): string {
const typeTemplate: string = `[${this.type}] `;
const typeTemplate: string = this.type !== ArgumentTypes.undefined ? `[${this.type}] ` : '';
const betaTemplate: string = this.beta ? '[BETA] ' : '';
const requiredTemplate: string = this.required ? '(required) ' : '';
const longNameTemplate: string = `--${this.longName} `;
Expand Down
8 changes: 4 additions & 4 deletions src/cli/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import chalk from 'chalk';
import { FatalErrorModel } from '../../core';

// tslint:disable-next-line:no-any
function parseJsonFile(configPath: string): any {
if (!fs.existsSync(configPath)) {
throw new FatalErrorModel(chalk.red(`Config file doesn't exists by path ${configPath}`));
function parseJsonFile(filePath: string): any {
if (!fs.existsSync(filePath)) {
throw new FatalErrorModel(chalk.red(`File doesn't exists by path ${filePath}`));
}
const configFile: Buffer = fs.readFileSync(configPath);
const configFile: Buffer = fs.readFileSync(filePath);
// tslint:disable-next-line:no-any
const result: any = JSON.parse(configFile.toString());
return result;
Expand Down
4 changes: 2 additions & 2 deletions src/core/models/files/FileLanguageModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class FileLanguageModel extends FileModel {
const objectKeys: string[] = Object.keys(obj);

objectKeys.forEach((key: string) => {
const keyValue: object = get(obj, key);
const keyValue: object = get(obj, key) || {};
const isObject: boolean = typeof keyValue === 'object';
const currentKey: string = cat === null ? key : cat.concat('.', key);

Expand All @@ -80,7 +80,7 @@ class FileLanguageModel extends FileModel {
const objectKeys: string[] = Object.keys(obj);

objectKeys.forEach((key: string) => {
const keyValue: object = get(obj, key);
const keyValue: object = get(obj, key) || {};
const isObject: boolean = typeof keyValue === 'object';
const currentKey: string = cat === null ? key : cat.concat('.', key);

Expand Down
4 changes: 2 additions & 2 deletions src/core/models/results/ResultCliModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ResultCliModel {
}

public isFullOfWarning(): boolean {
const result: boolean = this.maxCountWarning && this.maxCountWarning !== 0 && this.hasWarnings ? this.countWarnings() > this.maxCountWarning : false;
const result: boolean = this.maxCountWarning && this.maxCountWarning !== 0 && this.hasWarnings() ? this.countWarnings() > this.maxCountWarning : false;
return result;
}

Expand Down Expand Up @@ -78,7 +78,7 @@ class ResultCliModel {
.map((dictionary: ResultErrorModel[], key: string) => {
let clearDictionary: ResultErrorModel[] = dictionary;
const hasError: boolean = some<ResultErrorModel[]>(dictionary, { 'errorType': ErrorTypes.error });
const errorType: ErrorTypes = hasError || this.isFullOfWarning ? ErrorTypes.error : ErrorTypes.warning;
const errorType: ErrorTypes = hasError || this.isFullOfWarning() ? ErrorTypes.error : ErrorTypes.warning;
if (this.isFullOfWarning()) {
clearDictionary = dictionary.map((item: ResultErrorModel) => {
item.errorType = ErrorTypes.error;
Expand Down

0 comments on commit c001035

Please sign in to comment.