UNPKG

coffee-coverage

Version:

Istanbul and JSCoverage-style instrumentation for CoffeeScript files.

70 lines (58 loc) 1.98 kB
// Generated by CoffeeScript 2.3.2 (function() { var CompiledCache, EXTENSIONS, _, fs, getRelativeFilename, mkdirs, path; fs = require('fs'); path = require('path'); _ = require('lodash'); ({EXTENSIONS} = require('./constants')); ({mkdirs, getRelativeFilename} = require('./utils/helpers')); module.exports = CompiledCache = class CompiledCache { constructor(basePath, cacheDir1, ext = '_covered') { this.basePath = basePath; this.cacheDir = cacheDir1; this.ext = ext; } _getCacheFileName(fileName, options = {}) { var cacheFile, newExt, ref, relativeFile; newExt = (ref = options.ext) != null ? ref : this.ext; relativeFile = getRelativeFilename(this.basePath, fileName); cacheFile = path.resolve(this.cacheDir, relativeFile); cacheFile += newExt; return cacheFile; } get(fileName, compileFn = null) { var answer, cacheFileName, cacheStat, fileStat; if (!this.cacheDir) { return typeof compileFn === "function" ? compileFn() : void 0; } cacheFileName = this._getCacheFileName(fileName); answer = null; if (fs.existsSync(cacheFileName)) { fileStat = fs.statSync(fileName); cacheStat = fs.statSync(cacheFileName); if (cacheStat.ctime > fileStat.mtime) { answer = fs.readFileSync(cacheFileName, { encoding: 'utf8' }); } } if ((answer == null) && (compileFn != null)) { answer = compileFn(); this.put(fileName, answer); } return answer; } put(fileName, contents, options = {}) { var cacheDir, cacheFileName; if (!this.cacheDir || !contents) { return; } cacheFileName = this._getCacheFileName(fileName, options); cacheDir = path.dirname(cacheFileName); mkdirs(cacheDir); return fs.writeFileSync(cacheFileName, contents, { encoding: 'utf8' }); } }; }).call(this);