Skip to content

Commit

Permalink
feat: support --cache-directory
Browse files Browse the repository at this point in the history
  • Loading branch information
plantain-00 committed Nov 20, 2022
1 parent cd60df0 commit f04a347
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -56,6 +56,7 @@ name | type | description
`--no-detail-when-failed` | boolean? | not show detail message when the CLI failed(Added in `v2.19`)(Use `--no-detail-when-failed=true` to enable it <https://github.com/plantain-00/type-coverage/issues/113>)
`--report-semantic-error` | boolean? | report typescript semantic error(Added in `v2.22`)
`-- file1.ts file2.ts ...` | string[]? | only checks these files, useful for usage with tools like `lint-staged`(Added in `v2.23`)
`--cache-directory` | string? | [set cache directory](#enable-cache)(Added in `v2.24`)

### strict mode

Expand All @@ -69,7 +70,7 @@ Also, future minor release may introduce stricter type check in this mode, which

### enable cache

save and reuse type check result of files that is unchanged and independent of changed files in `.type-coverage` directory, to improve speed
save and reuse type check result of files that is unchanged and independent of changed files in `.type-coverage` directory(or set by `--cache-directory`), to improve speed

### ignore catch

Expand Down Expand Up @@ -122,6 +123,7 @@ This tool will ignore the files, eg: `--ignore-files "demo1/*.ts" --ignore-files
"historyFile": "typecoverage.json", // same as --history-file (Added in `v2.18`)
"noDetailWhenFailed": true, // same as --no-detail-when-failed (Added in `v2.19`)
"reportSemanticError": true, // same as --report-semantic-error (Added in `v2.22`)
"cacheDirectory": "custom-directory", // same as --cache-directory (Added in `v2.24`)
},
```

Expand Down Expand Up @@ -204,6 +206,7 @@ export interface LintOptions {
ignoreObject: boolean // Added in v2.21
ignoreEmptyType: boolean // Added in v2.21
reportSemanticError: boolean // Added in v2.22
cacheDirectory: string // Added in v2.24
}

export interface FileTypeCheckResult {
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/index.ts
Expand Up @@ -42,6 +42,7 @@ function printHelp() {
--no-detail-when-failed boolean? not show detail message when the CLI failed
--report-semantic-error boolean? report typescript semantic error
-- file1.ts file2.ts ... string[]? only checks these files, useful for usage with tools like lint-staged
--cache-directory string? set cache directory
`)
}

Expand Down Expand Up @@ -81,6 +82,7 @@ interface CliArgs extends BaseArgs {
['update-if-higher']: boolean

['report-semantic-error']: boolean
['cache-directory']: string
}

interface PkgArgs extends BaseArgs {
Expand All @@ -101,6 +103,7 @@ interface PkgArgs extends BaseArgs {
noDetailWhenFailed: boolean
updateIfHigher: boolean
reportSemanticError: boolean
cacheDirectory: string
}

interface PackageJson {
Expand Down Expand Up @@ -146,6 +149,7 @@ async function executeCommandLine() {
historyFile,
noDetailWhenFailed,
reportSemanticError,
cacheDirectory,
} = await getTarget(argv);

const { correctCount, totalCount, anys } = await lint(project, {
Expand All @@ -162,6 +166,7 @@ async function executeCommandLine() {
ignoreObject,
ignoreEmptyType,
reportSemanticError,
cacheDirectory,
files: argv['--'].length > 0 ? argv['--'] : undefined,
});

Expand Down Expand Up @@ -247,6 +252,7 @@ async function getTarget(argv: CliArgs) {
const historyFile = getArgOrCfgVal(['history-file', 'historyFile'])
const noDetailWhenFailed = getArgOrCfgVal(['no-detail-when-failed', 'noDetailWhenFailed'])
const reportSemanticError = getArgOrCfgVal(['report-semantic-error', 'reportSemanticError'])
const cacheDirectory = getArgOrCfgVal(['cache-directory', 'cacheDirectory'])

return {
atLeast,
Expand All @@ -271,6 +277,7 @@ async function getTarget(argv: CliArgs) {
historyFile,
noDetailWhenFailed,
reportSemanticError,
cacheDirectory,
};
}

Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/cache.ts
Expand Up @@ -17,12 +17,12 @@ function calculateHash(str: string): string {
return createHash('sha1').update(str).digest('hex')
}

export async function saveCache(typeCheckResult: TypeCheckResult) {
await mkdirIfmissing()
export async function saveCache(typeCheckResult: TypeCheckResult, dirName = defaultDirName) {
await mkdirIfmissing(dirName)
await writeFileAsync(path.resolve(dirName, 'result.json'), JSON.stringify(typeCheckResult, null, 2))
}

const dirName = '.type-coverage'
const defaultDirName = '.type-coverage'

function statAsync(p: string) {
return new Promise<fs.Stats | undefined>((resolve) => {
Expand All @@ -36,14 +36,14 @@ function statAsync(p: string) {
})
}

async function mkdirIfmissing() {
async function mkdirIfmissing(dirName = defaultDirName) {
const stats = await statAsync(dirName)
if (!stats) {
await mkdirAsync(dirName)
}
}

export async function readCache(enableCache: boolean): Promise<TypeCheckResult> {
export async function readCache(enableCache: boolean, dirName = defaultDirName): Promise<TypeCheckResult> {
if (!enableCache) {
return {
cache: {}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/core.ts
Expand Up @@ -29,7 +29,7 @@ export async function lint(project: string, options?: Partial<LintOptions>) {

const allFiles = new Set<string>()
const sourceFileInfos: SourceFileInfo[] = []
const typeCheckResult = await readCache(lintOptions.enableCache)
const typeCheckResult = await readCache(lintOptions.enableCache, lintOptions.cacheDirectory)
const ignoreFileGlobs = lintOptions.ignoreFiles
? (typeof lintOptions.ignoreFiles === 'string'
? [lintOptions.ignoreFiles]
Expand Down Expand Up @@ -186,7 +186,7 @@ export async function lint(project: string, options?: Partial<LintOptions>) {
}

if (lintOptions.enableCache) {
await saveCache(typeCheckResult)
await saveCache(typeCheckResult, lintOptions.cacheDirectory)
}

return { correctCount, totalCount, anys, program, fileCounts }
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/interfaces.ts
Expand Up @@ -45,6 +45,7 @@ export interface LintOptions extends CommonOptions {
fileCounts: boolean,
absolutePath?: boolean,
reportSemanticError: boolean
cacheDirectory?: string
}

interface CommonOptions {
Expand Down

0 comments on commit f04a347

Please sign in to comment.