diff --git a/README.md b/README.md index 725fdae..fdc5be8 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ name | type | description `--ignore-type-assertion` | boolean? | ignore type assertion, eg: `foo`(Added in `v2.16`) `--ignore-non-null-assertion` | boolean? | ignore non-null assertion, eg: `foo!`(Added in `v2.16`) `--show-relative-path` | boolean? | show relative path in detail message(Added in `v2.17`) +`--history-file` | string? | file name where history is saved ### strict mode @@ -104,7 +105,8 @@ This tool will ignore the files, eg: `--ignore-files "demo1/*.ts" --ignore-files "ignoreAsAssertion": true, // same as --ignore-as-assertion (Added in `v2.16`) "ignoreTypeAssertion": true, // same as --ignore-type-assertion (Added in `v2.16`) "ignoreNonNullAssertion": true, // same as --ignore-non-null-assertion (Added in `v2.16`) - "showRelativePath": true // same as --show-relative-path (Added in `v2.17`) + "showRelativePath": true, // same as --show-relative-path (Added in `v2.17`) + "historyFile": "typecoverage.json" // same as --history-file }, ``` diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 88d7cde..e8fe6be 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -65,6 +65,8 @@ interface CliArgs extends BaseArgs { ['ignore-as-assertion']: boolean ['ignore-type-assertion']: boolean ['ignore-non-null-assertion']: boolean + + ['history-file']: string } interface PkgArgs extends BaseArgs { @@ -78,6 +80,8 @@ interface PkgArgs extends BaseArgs { ignoreAsAssertion: boolean ignoreTypeAssertion: boolean ignoreNonNullAssertion: boolean + + historyFile: string } interface PackageJson { @@ -117,6 +121,7 @@ async function executeCommandLine() { ignoreTypeAssertion, ignoreNonNullAssertion, showRelativePath, + historyFile, } = await getTarget(argv); const { correctCount, totalCount, anys } = await lint(project, { @@ -148,6 +153,9 @@ async function executeCommandLine() { if (update) { await saveTarget(+percentString) } + if (historyFile) { + await saveHistory(+percentString, historyFile) + } if (atLeastFailed) { throw new Error(`The type coverage rate(${percentString}%) is lower than the target(${atLeast}%).`) @@ -203,6 +211,7 @@ async function getTarget(argv: CliArgs) { const ignoreTypeAssertion = getArgOrCfgVal(['ignore-type-assertion', 'ignoreTypeAssertion']) const ignoreNonNullAssertion = getArgOrCfgVal(['ignore-non-null-assertion', 'ignoreNonNullAssertion']) const showRelativePath = getArgOrCfgVal(['show-relative-path', 'showRelativePath']) + const historyFile = getArgOrCfgVal(['history-file', 'historyFile']) return { atLeast, @@ -221,6 +230,7 @@ async function getTarget(argv: CliArgs) { ignoreTypeAssertion, ignoreNonNullAssertion, showRelativePath, + historyFile, }; } @@ -244,6 +254,23 @@ async function saveTarget(target: number) { } } +async function saveHistory(percentage: number, historyFile?:string) { + if (historyFile) { + const historyFilePath = path.resolve(process.cwd(), historyFile); + if (await existsAsync(historyFilePath)) { + const date = new Date().toISOString() + const historyFile = JSON.parse((await readFileAsync(historyFilePath)).toString()); + historyFile[date] = percentage + await writeFileAsync(historyFilePath, JSON.stringify(historyFile, null, 2) + '\n'); + } else { + const date = new Date().toISOString() + const historyFile: Record = {} + historyFile[date] = percentage + await writeFileAsync(historyFilePath, JSON.stringify(historyFile, null, 2) + '\n'); + } + } +} + executeCommandLine().then(() => { console.log('type-coverage success.') }, (error: Error | string) => {