diff --git a/fixtures/prefer-local/cli.js b/fixtures/prefer-local/cli.js new file mode 100755 index 0000000..23cfdbc --- /dev/null +++ b/fixtures/prefer-local/cli.js @@ -0,0 +1,7 @@ +#!/usr/bin/env node +'use strict'; +const importLocal = require('../..'); + +if (importLocal(__filename)) { + console.log('local'); +} diff --git a/fixtures/prefer-local/nested/node_modules/some-pkg/cli.js b/fixtures/prefer-local/nested/node_modules/some-pkg/cli.js new file mode 100644 index 0000000..e69de29 diff --git a/fixtures/prefer-local/nested/node_modules/some-pkg/package.json b/fixtures/prefer-local/nested/node_modules/some-pkg/package.json new file mode 100644 index 0000000..31017d5 --- /dev/null +++ b/fixtures/prefer-local/nested/node_modules/some-pkg/package.json @@ -0,0 +1,3 @@ +{ + "name": "some-pkg" +} diff --git a/fixtures/prefer-local/package.json b/fixtures/prefer-local/package.json new file mode 100644 index 0000000..31017d5 --- /dev/null +++ b/fixtures/prefer-local/package.json @@ -0,0 +1,3 @@ +{ + "name": "some-pkg" +} diff --git a/index.js b/index.js index b119e45..0bf36cf 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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); }; diff --git a/package.json b/package.json index a52e4e2..f3afb02 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,6 @@ "cli" ], "dependencies": { - "find-up": "^4.1.0", "pkg-dir": "^4.2.0", "resolve-cwd": "^3.0.0" }, diff --git a/test.js b/test.js index b13c693..d278ebb 100644 --- a/test.js +++ b/test.js @@ -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,