From eb53eab36ea7cb23f16245ea6c39b6cdd4e6d9ab Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Mon, 9 Nov 2015 15:19:26 +0500 Subject: [PATCH] cache by test file content and ava version hash --- index.js | 13 +++++++++++++ lib/babel.js | 33 ++++++++++++++++++++++++--------- package.json | 7 ++++++- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 29469f6fd1..d9bd7f9e0b 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,24 @@ require('./lib/babel').avaRequired(); var setImmediate = require('set-immediate-shim'); var hasFlag = require('has-flag'); var chalk = require('chalk'); +var join = require('path').join; var relative = require('path').relative; var serializeError = require('destroy-circular'); var Runner = require('./lib/runner'); var log = require('./lib/logger'); var runner = new Runner(); +var xdgBasedir = require('xdg-basedir'); +var cache = require('cacha')(join(xdgBasedir.cache, 'ava')); +var Configstore = require('configstore'); +var config = new Configstore('ava', { + lastCacheClean: Date.now() +}); +var WEEK = 7 * 24 * 60 * 60 * 1000; + +if (Date.now() - config.get('lastCacheClean') > WEEK) { + config.set('lastCacheClean', Date.now()); + cache.clean(); +} // check if the test is being run without AVA cli var isForked = typeof process.send === 'function'; diff --git a/lib/babel.js b/lib/babel.js index 78957dae3a..4d9e8afc5e 100644 --- a/lib/babel.js +++ b/lib/babel.js @@ -2,6 +2,11 @@ var resolveFrom = require('resolve-from'); var createEspowerPlugin = require('babel-plugin-espower/create'); var requireFromString = require('require-from-string'); +var hasha = require('hasha'); +var hashObj = require('hash-obj'); +var xdgBasedir = require('xdg-basedir'); +var path = require('path'); +var cache = require('cacha')(path.join(xdgBasedir.cache, 'ava')); var hasGenerators = parseInt(process.version.slice(1), 10) > 0; var testPath = process.argv[2]; @@ -24,16 +29,32 @@ var options = { ] }; -var avaRequired; +process.on('message', function (message) { + if (message['ava-kill-command']) { + process.exit(0); + } +}); + +var hash = hashObj({ + code: hasha.fromFileSync(testPath), + avaVersion: require('../package.json').version +}); + +var code = cache.getSync(hash, 'utf8'); +if (code === undefined) { + code = babel.transformFileSync(testPath, options).code; + cache.setSync(hash, code, 'utf8'); +} + +var avaRequired; module.exports = { avaRequired: function () { avaRequired = true; } }; -var transpiled = babel.transformFileSync(testPath, options); -requireFromString(transpiled.code, testPath, { +requireFromString(code, testPath, { appendPaths: module.paths }); @@ -43,9 +64,3 @@ if (!avaRequired) { process.exit(1); }); } - -process.on('message', function (message) { - if (message['ava-kill-command']) { - process.exit(0); - } -}); diff --git a/package.json b/package.json index aae3f9f8e8..264853a25f 100644 --- a/package.json +++ b/package.json @@ -67,8 +67,10 @@ "babel-plugin-espower": "^1.1.0", "babel-runtime": "^5.8.29", "bluebird": "^3.0.0", + "cacha": "^1.0.3", "chalk": "^1.0.0", "co-with-promise": "^4.6.0", + "configstore": "^1.3.0", "core-assert": "^0.1.0", "destroy-circular": "jamestalmage/destroy-circular#feeb7d1", "empower": "^1.1.0", @@ -76,6 +78,8 @@ "fn-name": "^2.0.0", "globby": "^3.0.1", "has-flag": "^1.0.0", + "hash-obj": "^1.0.0", + "hasha": "^2.0.2", "is-generator": "^1.0.2", "meow": "^3.3.0", "plur": "^2.0.0", @@ -86,7 +90,8 @@ "resolve-from": "^1.0.0", "set-immediate-shim": "^1.0.1", "squeak": "^1.2.0", - "update-notifier": "^0.5.0" + "update-notifier": "^0.5.0", + "xdg-basedir": "^2.0.0" }, "devDependencies": { "coveralls": "^2.11.4",