From ebb394886558c4c51194d20caa8024be9121c4d7 Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sat, 12 May 2018 17:40:57 +0100 Subject: [PATCH] Implement --reset-cache --- lib/cli.js | 22 +++++++++++++++++++++- test/cli.js | 14 ++++++++++++++ test/fixture/reset-cache/package.json | 1 + test/fixture/reset-cache/test.js | 3 +++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/fixture/reset-cache/package.json create mode 100644 test/fixture/reset-cache/test.js diff --git a/lib/cli.js b/lib/cli.js index 30fa3df8a..0ae9f9daa 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -1,5 +1,6 @@ 'use strict'; const path = require('path'); +const del = require('del'); const updateNotifier = require('update-notifier'); const figures = require('figures'); const arrify = require('arrify'); @@ -38,6 +39,7 @@ exports.run = () => { // eslint-disable-line complexity --tap, -t Generate TAP output --color Force color output --no-color Disable color output + --reset-cache Reset AVA's compilation cache and exit Examples ava @@ -95,6 +97,10 @@ exports.run = () => { // eslint-disable-line complexity type: 'boolean', default: 'color' in conf ? conf.color : require('supports-color').stdout !== false }, + 'reset-cache': { + type: 'boolean', + default: false + }, '--': { type: 'string' } @@ -102,7 +108,21 @@ exports.run = () => { // eslint-disable-line complexity }); updateNotifier({pkg: cli.pkg}).notify(); - require('./chalk').set({enabled: cli.flags.color}); + const chalk = require('./chalk').set({enabled: cli.flags.color}); + + if (cli.flags.resetCache) { + const cacheDir = path.join(projectDir, 'node_modules', '.cache', 'ava'); + del('*', { + cwd: cacheDir, + nodir: true + }).then(() => { + console.error(`\n${chalk.green(figures.tick)} Removed AVA cache files in ${cacheDir}`); + process.exit(0); // eslint-disable-line unicorn/no-process-exit + }, err => { + exit(`Error removing AVA cache files in ${cacheDir}\n\n${chalk.gray((err && err.stack) || err)}`); + }); + return; + } if (cli.flags.watch && cli.flags.tap && !conf.tap) { exit('The TAP reporter is not available when using watch mode.'); diff --git a/test/cli.js b/test/cli.js index b02bd5ab9..9efc8ef56 100644 --- a/test/cli.js +++ b/test/cli.js @@ -836,3 +836,17 @@ test('additional arguments are forwarded to the worker', t => { t.end(); }); }); + +test('--reset-cache resets cache', t => { + const cacheDir = path.join(__dirname, 'fixture', 'reset-cache', 'node_modules', '.cache', 'ava'); + execCli([], {dirname: 'fixture/reset-cache'}, err => { + t.ifError(err); + t.true(fs.readdirSync(cacheDir).length > 0); + + execCli(['--reset-cache'], {dirname: 'fixture/reset-cache'}, err => { + t.ifError(err); + t.true(fs.readdirSync(cacheDir).length === 0); + t.end(); + }); + }); +}); diff --git a/test/fixture/reset-cache/package.json b/test/fixture/reset-cache/package.json new file mode 100644 index 000000000..0967ef424 --- /dev/null +++ b/test/fixture/reset-cache/package.json @@ -0,0 +1 @@ +{} diff --git a/test/fixture/reset-cache/test.js b/test/fixture/reset-cache/test.js new file mode 100644 index 000000000..9d6912855 --- /dev/null +++ b/test/fixture/reset-cache/test.js @@ -0,0 +1,3 @@ +import test from '../../..'; + +test('pass', t => t.pass());