Skip to content

Commit

Permalink
Fix regression with preferring local install when invoked through… (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxmul authored and sindresorhus committed Jul 7, 2019
1 parent 77bc808 commit 47fedf4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
7 changes: 7 additions & 0 deletions fixtures/prefer-local/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node
'use strict';
const importLocal = require('../..');

if (importLocal(__filename)) {
console.log('local');
}
Empty file.

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

3 changes: 3 additions & 0 deletions fixtures/prefer-local/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "some-pkg"
}
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const path = require('path');
const findUp = require('find-up');
const resolveCwd = require('resolve-cwd');
const pkgDir = require('pkg-dir');

Expand All @@ -9,7 +8,8 @@ module.exports = filename => {
const relativePath = path.relative(globalDir, filename);
const pkg = require(path.join(globalDir, 'package.json'));
const localFile = resolveCwd.silent(path.join(pkg.name, relativePath));
const filenameIsLocal = findUp.sync(path.join(process.cwd(), 'node_modules'), {cwd: globalDir, type: 'directory'}) !== undefined;
const localNodeModules = path.join(process.cwd(), 'node_modules');
const filenameInLocalNodeModules = !path.relative(localNodeModules, filename).startsWith('..');

return localFile && !filenameIsLocal && require(localFile);
return !filenameInLocalNodeModules && localFile && require(localFile);
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"cli"
],
"dependencies": {
"find-up": "^4.1.0",
"pkg-dir": "^4.2.0",
"resolve-cwd": "^3.0.0"
},
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ test('local', async t => {
await del(path.join(__dirname, 'fixtures/local/node_modules'));
});

test('invoking global prefers local', async t => {
await cpy(
['package.json', 'index.js'],
path.join(__dirname, 'fixtures/prefer-local/node_modules/import-local'),
{parents: true}
);

const {stdout} = await execa(
'node',
[path.join(__dirname, 'fixtures/prefer-local/cli.js')],
{
preferLocal: false,
cwd: path.join(__dirname, 'fixtures/prefer-local/nested')
}
);
t.is(stdout, 'local');

await del(path.join(__dirname, 'fixtures/prefer-local/node_modules'));
});

test('global', async t => {
const {stdout} = await execa('import-local-fixture', {
preferLocal: false,
Expand Down

0 comments on commit 47fedf4

Please sign in to comment.