Skip to content

Commit

Permalink
Save coverage history on a target file
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-spielmann authored and plantain-00 committed Jun 23, 2021
1 parent a5382fb commit f4099f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ name | type | description
`--ignore-type-assertion` | boolean? | ignore type assertion, eg: `<string>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

Expand Down Expand Up @@ -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
},
```

Expand Down
27 changes: 27 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -78,6 +80,8 @@ interface PkgArgs extends BaseArgs {
ignoreAsAssertion: boolean
ignoreTypeAssertion: boolean
ignoreNonNullAssertion: boolean

historyFile: string
}

interface PackageJson {
Expand Down Expand Up @@ -117,6 +121,7 @@ async function executeCommandLine() {
ignoreTypeAssertion,
ignoreNonNullAssertion,
showRelativePath,
historyFile,
} = await getTarget(argv);

const { correctCount, totalCount, anys } = await lint(project, {
Expand Down Expand Up @@ -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}%).`)
Expand Down Expand Up @@ -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,
Expand All @@ -221,6 +230,7 @@ async function getTarget(argv: CliArgs) {
ignoreTypeAssertion,
ignoreNonNullAssertion,
showRelativePath,
historyFile,
};
}

Expand All @@ -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<string, number> = {}
historyFile[date] = percentage
await writeFileAsync(historyFilePath, JSON.stringify(historyFile, null, 2) + '\n');
}
}
}

executeCommandLine().then(() => {
console.log('type-coverage success.')
}, (error: Error | string) => {
Expand Down

0 comments on commit f4099f8

Please sign in to comment.