From 81db62814335ebafaf2cb27ec194796353b7febe Mon Sep 17 00:00:00 2001 From: James Talmage Date: Fri, 27 Nov 2015 18:21:52 -0500 Subject: [PATCH] Prefer the locally installed version of AVA. This hunts for AVA in the locally installed project, and runs that CLI instead of the globally installed one. Fixes #157. --- cli.js | 18 ++++++++++++++++++ readme.md | 3 +++ 2 files changed, 21 insertions(+) diff --git a/cli.js b/cli.js index 6ed48633a9..52d1df63a7 100755 --- a/cli.js +++ b/cli.js @@ -1,6 +1,24 @@ #!/usr/bin/env node 'use strict'; +// Prefer the local installation of AVA. +var resolveFrom = require('resolve-from'); +var localCLI; +try { + localCLI = resolveFrom('.', 'ava/cli'); +} catch (e) {} + +if (localCLI && localCLI !== __filename) { + var thisVersion = require('./package.json').version; + var localVersion = require(localCLI.replace(/cli\.js$/, 'package.json')).version; + var warningMessage = 'Using local install of AVA (v%s), which '; + warningMessage += thisVersion === localVersion ? 'matches' : 'differs from'; + warningMessage += ' global (v%s)'; + console.warn(require('chalk').yellow(warningMessage), localVersion, thisVersion); + require(localCLI); + return; +} + var debug = require('debug')('ava'); if (debug.enabled) { diff --git a/readme.md b/readme.md index d35b35f7d3..ee7065e982 100644 --- a/readme.md +++ b/readme.md @@ -116,6 +116,9 @@ $ ava --help Files starting with `_` are ignored. This can be useful for having helpers in the same directory as your test files. +*WARNING: NON-STANDARD BEHAVIOR:* The AVA CLI will always try to find and use your projects local install of AVA. + This is true even when you run the global `ava` command. This non-standard behavior solves an important [issue](https://github.com/sindresorhus/ava/issues/157), + and should have no impact on everyday use. ## Documentation