Skip to content

Commit

Permalink
Use the import-local module (#1376)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus authored Jun 14, 2017
1 parent dfca2d9 commit 7c62b35
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 40 deletions.
14 changes: 3 additions & 11 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
#!/usr/bin/env node
'use strict';
const path = require('path');
const debug = require('debug')('ava');
const importLocal = require('import-local');

// Prefer the local installation of AVA.
const resolveCwd = require('resolve-cwd');

const localCLI = resolveCwd('ava/cli');

// Use `path.relative()` to detect local AVA installation,
// because __filename's case is inconsistent on Windows
// see https://github.com/nodejs/node/issues/6624
if (localCLI && path.relative(localCLI, __filename) !== '') {
// Prefer the local installation of AVA
if (importLocal(__filename)) {
debug('Using local install of AVA');
require(localCLI);
} else {
if (debug.enabled) {
require('time-require'); // eslint-disable-line import/no-unassigned-import
Expand Down
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"has-flag": "^2.0.0",
"hullabaloo-config-manager": "^1.1.0",
"ignore-by-default": "^1.0.0",
"import-local": "^0.1.1",
"indent-string": "^3.0.0",
"is-ci": "^1.0.7",
"is-generator-fn": "^1.0.0",
Expand Down
39 changes: 10 additions & 29 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const getStream = require('get-stream');
const figures = require('figures');
const makeDir = require('make-dir');
const touch = require('touch');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const uniqueTempDir = require('unique-temp-dir');
const execa = require('execa');
const stripAnsi = require('strip-ansi');
Expand All @@ -33,7 +31,7 @@ function execCli(args, opts, cb) {
let stderr;

const processPromise = new Promise(resolve => {
child = childProcess.spawn(process.execPath, [path.relative(dirname, cliPath)].concat(args), {
child = childProcess.spawn(process.execPath, [cliPath].concat(args), {
cwd: dirname,
env,
stdio: [null, 'pipe', 'pipe']
Expand Down Expand Up @@ -421,33 +419,16 @@ test('should warn ava is required without the cli', t => {
});

test('prefers local version of ava', t => {
t.plan(1);

const stubModulePath = path.join(__dirname, '/fixture/empty');
const debugSpy = sinon.spy();
const resolveCwdStub = () => stubModulePath;

function debugStub() {
return message => {
let result = {
enabled: false
};

if (message) {
result = debugSpy(message);
}

return result;
};
}

proxyquire('../cli', {
debug: debugStub,
'resolve-cwd': resolveCwdStub
execCli('', {
dirname: 'fixture/local-bin',
env: {
DEBUG: 'ava'
}
}, (err, stdout, stderr) => {
t.ifError(err);
t.match(stderr, 'Using local install of AVA');
t.end();
});

t.ok(debugSpy.calledWith('Using local install of AVA'));
t.end();
});

test('use current working directory if `package.json` is not found', () => {
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions test/fixture/local-bin/node_modules/ava/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7c62b35

Please sign in to comment.