diff --git a/src/cache.ts b/src/cache.ts index 6200291..bd6fdc9 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -2,6 +2,7 @@ import * as ts from "typescript"; import * as graph from "graphlib"; import * as hash from "object-hash"; import * as _ from "lodash"; +import * as colors from "colors/safe"; import { RollingCache } from "./rollingcache"; export interface ICode @@ -158,7 +159,7 @@ export class Cache let dirty = l === undefined ? true : l.dirty; if (dirty) - console.log(`dirty: ${id} -> ${node}`.gray); + console.log(colors.gray(`dirty: ${id} -> ${node}`)); return dirty; }); @@ -176,13 +177,13 @@ export class Cache { let entry: IDiagnostics = { - flatMessage: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n").yellow, + flatMessage: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"), }; if (diagnostic.file) { let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); - entry.fileLine = `${diagnostic.file.fileName} (${line + 1},${character + 1})`.white; + entry.fileLine = `${diagnostic.file.fileName} (${line + 1},${character + 1})`; } return entry; diff --git a/src/index.ts b/src/index.ts index defa952..363e62c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { createFilter } from "rollup-pluginutils"; import * as fs from "fs-extra"; import * as path from "path"; import * as _ from "lodash"; -import * as colors from "colors"; +import * as colors from "colors/safe"; function getOptionsOverrides(): ts.CompilerOptions { @@ -87,9 +87,9 @@ function printDiagnostics(diagnostics: IDiagnostics[]) _.each(diagnostics, (diagnostic) => { if (diagnostic.fileLine) - console.log(`${diagnostic.fileLine}: ${diagnostic.flatMessage}`); + console.log(`${diagnostic.fileLine}: ${colors.yellow(diagnostic.flatMessage)}`); else - console.log(diagnostic.flatMessage); + console.log(colors.yellow(diagnostic.flatMessage)); }); }; @@ -160,7 +160,7 @@ export default function typescript (options: IOptions) const output = services.getEmitOutput(id); if (output.emitSkipped) - this.error({ message: `failed to transpile ${id}`}); + this.error({ message: colors.red(`failed to transpile ${id}`)}); const transpiled = _.find(output.outputFiles, (entry: ts.OutputFile) => _.endsWith(entry.name, ".js") ); const map = _.find(output.outputFiles, (entry: ts.OutputFile) => _.endsWith(entry.name, ".map") ); @@ -184,7 +184,7 @@ export default function typescript (options: IOptions) if (!snapshot) { - console.log(`failed lo load snapshot for ${id}`); + console.log(colors.red(`failed lo load snapshot for ${id}`)); return; } diff --git a/src/rollingcache.ts b/src/rollingcache.ts index 638ee8d..aab4340 100644 --- a/src/rollingcache.ts +++ b/src/rollingcache.ts @@ -1,11 +1,19 @@ import * as fs from "fs-extra"; import * as _ from "lodash"; +/** + * Saves data in new cache folder or reads it from old one. + * Avoids perpetually growing cache and situations when things need to consider changed and then reverted data to be changed. + */ export class RollingCache { private oldCacheRoot: string; private newCacheRoot: string; + /** + * @param cacheRoot: root folder for the cache + * @param checkNewCache: whether to also look in new cache when reading from cache + */ constructor(private cacheRoot: string, private checkNewCache: boolean) { this.oldCacheRoot = `${this.cacheRoot}/cache`; @@ -14,6 +22,9 @@ export class RollingCache fs.emptyDirSync(this.newCacheRoot); } + /** + * @returns true if name exist in old cache (or either old of new cache if checkNewCache is true) + */ public exists(name: string): boolean { if (this.checkNewCache && fs.existsSync(`${this.newCacheRoot}/${name}`)) @@ -22,14 +33,20 @@ export class RollingCache return fs.existsSync(`${this.oldCacheRoot}/${name}`); } + /** + * @returns true if old cache contains all names and nothing more + */ public match(names: string[]): boolean { if (!fs.existsSync(this.oldCacheRoot)) - return false; + return names.length === 0; // empty folder matches return _.isEqual(fs.readdirSync(this.oldCacheRoot).sort(), names.sort()); } + /** + * @returns data for name, must exist in old cache (or either old of new cache if checkNewCache is true) + */ public read(name: string): DataType { if (this.checkNewCache && fs.existsSync(`${this.newCacheRoot}/${name}`)) @@ -43,19 +60,28 @@ export class RollingCache if (data === undefined) return; - fs.writeJson(`${this.newCacheRoot}/${name}`, data, { encoding: "utf8" }, () => {}); + if (this.checkNewCache) + fs.writeJsonSync(`${this.newCacheRoot}/${name}`, data); + else // won't be reading it this run + fs.writeJson(`${this.newCacheRoot}/${name}`, data, { encoding: "utf8" }, () => { ; }); } public touch(name: string) { - fs.ensureFile(`${this.newCacheRoot}/${name}`, () => {}); + if (this.checkNewCache) + fs.ensureFileSync(`${this.newCacheRoot}/${name}`); + else // won't be reading it this run + fs.ensureFile(`${this.newCacheRoot}/${name}`, () => { ; }); } + /** + * clears old cache and moves new in its place + */ public roll() { fs.remove(this.oldCacheRoot, () => { - fs.move(this.newCacheRoot, this.oldCacheRoot, () => {} ); + fs.move(this.newCacheRoot, this.oldCacheRoot, () => { ; }); }); } }