Skip to content

Commit

Permalink
feat: support --update-if-higher
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Jan 25, 2022
1 parent 1d0d50d commit 68acc65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -41,6 +41,7 @@ name | type | description
`-h`, `--help` | boolean? | show help(Added in `v2.5`)
`--is` | number? | fail if coverage rate !== this value(Added in `v2.6`)
`--update` | boolean? | update "typeCoverage" in package.json to current result(Added in `v2.6`)
`--update-if-higher` | boolean? | update "typeCoverage" in package.json to current result if new type coverage is higher(Added in `v2.20`)
`--ignore-unread` | boolean? | allow writes to variables with implicit any types(Added in `v2.14`)
`--ignore-nested` | boolean? | ignore any in type arguments, eg: `Promise<any>`(Added in `v2.16`)
`--ignore-as-assertion` | boolean? | ignore as assertion, eg: `foo as string`(Added in `v2.16`)
Expand Down Expand Up @@ -101,6 +102,7 @@ This tool will ignore the files, eg: `--ignore-files "demo1/*.ts" --ignore-files
"strict": true, // same as --strict (Added in `v2.11`)
"suppressError": true, // same as --suppressError (Added in `v2.11`)
"update": true, // same as --update (Added in `v2.11`)
"updateIfHigher": true, // same as --update-if-higher (Added in `v2.20`)
"ignoreUnread": true, // same as --ignore-unread (Added in `v2.14`)
"ignoreNested": true, // same as --ignore-nested (Added in `v2.16`)
"ignoreAsAssertion": true, // same as --ignore-as-assertion (Added in `v2.16`)
Expand Down
18 changes: 15 additions & 3 deletions packages/cli/src/index.ts
Expand Up @@ -30,6 +30,7 @@ function printHelp() {
-h,--help boolean? show help
--is number? fail if coverage rate !== this value
--update boolean? update "typeCoverage" in package.json to current result
--update-if-higher boolean? update "typeCoverage" in package.json to current result if new type coverage is higher
--ignore-nested boolean? ignore any in type arguments, eg: Promise<any>
--ignore-as-assertion boolean? ignore as assertion, eg: foo as string
--ignore-type-assertion boolean? ignore type assertion, eg: <string>foo
Expand Down Expand Up @@ -70,6 +71,7 @@ interface CliArgs extends BaseArgs {

['history-file']: string
['no-detail-when-failed']: boolean
['update-if-higher']: boolean
}

interface PkgArgs extends BaseArgs {
Expand All @@ -86,6 +88,7 @@ interface PkgArgs extends BaseArgs {

historyFile: string
noDetailWhenFailed: boolean
updateIfHigher: boolean
}

interface PackageJson {
Expand Down Expand Up @@ -120,6 +123,7 @@ async function executeCommandLine() {
project,
strict,
update,
updateIfHigher,
ignoreNested,
ignoreAsAssertion,
ignoreTypeAssertion,
Expand Down Expand Up @@ -157,6 +161,8 @@ async function executeCommandLine() {

if (update) {
await saveTarget(+percentString)
} else if (updateIfHigher) {
await saveTarget(+percentString, true)
}
if (historyFile) {
await saveHistory(+percentString, historyFile)
Expand Down Expand Up @@ -211,6 +217,7 @@ async function getTarget(argv: CliArgs) {
const project = getArgOrCfgVal(['p', 'project']) || '.'
const strict = getArgOrCfgVal(['strict'])
const update = getArgOrCfgVal(['update'])
const updateIfHigher = getArgOrCfgVal(['update-if-higher', 'updateIfHigher'])
const ignoreNested = getArgOrCfgVal(['ignore-nested', 'ignoreNested'])
const ignoreAsAssertion = getArgOrCfgVal(['ignore-as-assertion', 'ignoreAsAssertion'])
const ignoreTypeAssertion = getArgOrCfgVal(['ignore-type-assertion', 'ignoreTypeAssertion'])
Expand All @@ -231,6 +238,7 @@ async function getTarget(argv: CliArgs) {
project,
strict,
update,
updateIfHigher,
ignoreNested,
ignoreAsAssertion,
ignoreTypeAssertion,
Expand All @@ -241,7 +249,7 @@ async function getTarget(argv: CliArgs) {
};
}

async function saveTarget(target: number) {
async function saveTarget(target: number, ifHigher?: boolean) {
const packageJsonPath = path.resolve(process.cwd(), 'package.json')
if (await existsAsync(packageJsonPath)) {
const currentPackageJson: {
Expand All @@ -252,9 +260,13 @@ async function saveTarget(target: number) {
} = JSON.parse((await readFileAsync(packageJsonPath)).toString())
if (currentPackageJson.typeCoverage) {
if (currentPackageJson.typeCoverage.atLeast) {
currentPackageJson.typeCoverage.atLeast = target
if (!ifHigher || target > currentPackageJson.typeCoverage.atLeast) {
currentPackageJson.typeCoverage.atLeast = target
}
} else if (currentPackageJson.typeCoverage.is) {
currentPackageJson.typeCoverage.is = target
if (!ifHigher || target > currentPackageJson.typeCoverage.is) {
currentPackageJson.typeCoverage.is = target
}
}
await writeFileAsync(packageJsonPath, JSON.stringify(currentPackageJson, null, 2) + '\n')
}
Expand Down

0 comments on commit 68acc65

Please sign in to comment.