diff --git a/src/index.ts b/src/index.ts index 20c0e981..dccd1332 100644 --- a/src/index.ts +++ b/src/index.ts @@ -128,9 +128,6 @@ const typescript: PluginImpl = (options) => noErrors = false; } - if (pluginOptions.clean) - cache().clean(); - return config; }, diff --git a/src/tscache.ts b/src/tscache.ts index 49dc7b27..a9724d49 100644 --- a/src/tscache.ts +++ b/src/tscache.ts @@ -1,5 +1,5 @@ import * as tsTypes from "typescript"; -import { emptyDirSync, pathExistsSync, readdirSync, removeSync, statSync } from "fs-extra"; +import * as fs from "fs-extra"; import * as _ from "lodash"; import { Graph, alg } from "graphlib"; import objHash from "object-hash"; @@ -127,6 +127,8 @@ export class TsCache }, this.hashOptions, )}`; + } else { + this.clean(); } this.dependencyTree = new Graph({ directed: true }); @@ -146,26 +148,33 @@ export class TsCache this.checkAmbientTypes(); } - public clean() + private clean() { - if (pathExistsSync(this.cacheRoot)) + if (!fs.pathExistsSync(this.cacheRoot)) + return; + + const entries = fs.readdirSync(this.cacheRoot); + entries.forEach((e) => { - const entries = readdirSync(this.cacheRoot); - entries.forEach((e) => + const dir = `${this.cacheRoot}/${e}`; + + /* istanbul ignore if -- this is a safety check, but shouldn't happen when using a dedicated cache dir */ + if (!e.startsWith(this.cachePrefix)) { - const dir = `${this.cacheRoot}/${e}`; - if (e.startsWith(this.cachePrefix) && statSync(dir).isDirectory) - { - this.context.info(blue(`cleaning cache: ${dir}`)); - emptyDirSync(`${dir}`); - removeSync(`${dir}`); - } - else - this.context.debug(`not cleaning ${dir}`); - }); - } + this.context.debug(`skipping cleaning ${dir} as it does not have prefix ${this.cachePrefix}`); + return; + } - this.init(); + /* istanbul ignore if -- this is a safety check, but should never happen in normal usage */ + if (!fs.statSync(dir).isDirectory) + { + this.context.debug(`skipping cleaning ${dir} as it is not a directory`); + return; + } + + this.context.info(blue(`cleaning cache: ${dir}`)); + fs.removeSync(`${dir}`); + }); } public setDependency(importee: string, importer: string): void