Skip to content

Commit ec1a98f

Browse files
committed
[Fix] node-modules-paths: opts should be optional, and opts.paths should not be concatenated when omitted.
Fixes #96.
1 parent 8280c53 commit ec1a98f

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

lib/node-modules-paths.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var path = require('path');
22

3-
module.exports = function (start, opts) {
4-
var modules = opts.moduleDirectory
3+
module.exports = function nodeModulesPaths(start, opts) {
4+
var modules = opts && opts.moduleDirectory
55
? [].concat(opts.moduleDirectory)
66
: ['node_modules']
77
;
@@ -34,5 +34,5 @@ module.exports = function (start, opts) {
3434
if (process.platform === 'win32') {
3535
dirs[dirs.length - 1] = dirs[dirs.length - 1].replace(':', ':\\');
3636
}
37-
return dirs.concat(opts.paths);
37+
return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
3838
};

test/node-modules-paths.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var test = require('tape');
2+
var path = require('path');
3+
4+
var getAllNodeModulePathsUpwards = function (start, moduleDirectory) {
5+
if (!moduleDirectory) { moduleDirectory = 'node_modules'; }
6+
var currentPath = start;
7+
var expectedDirs = ['/' + path.join(currentPath, moduleDirectory)];
8+
while (currentPath && currentPath !== '/') {
9+
currentPath = path.join(currentPath, '../');
10+
expectedDirs.push('/' + path.join(currentPath, moduleDirectory));
11+
}
12+
return expectedDirs;
13+
}
14+
15+
var nodeModulesPaths = require('../lib/node-modules-paths');
16+
17+
test('node-modules-paths', function (t) {
18+
t.test('no options', function (t) {
19+
var start = path.join(__dirname, 'resolver');
20+
var dirs = nodeModulesPaths(start);
21+
22+
var expectedDirs = getAllNodeModulePathsUpwards(start);
23+
24+
t.deepEqual(dirs, expectedDirs);
25+
26+
t.end();
27+
});
28+
29+
t.test('empty options', function (t) {
30+
var start = path.join(__dirname, 'resolver');
31+
var dirs = nodeModulesPaths(start, {});
32+
33+
var expectedDirs = getAllNodeModulePathsUpwards(start);
34+
35+
t.deepEqual(dirs, expectedDirs);
36+
37+
t.end();
38+
});
39+
40+
t.test('with paths option', function (t) {
41+
var start = path.join(__dirname, 'resolver');
42+
var paths = ['a', 'b'];
43+
var dirs = nodeModulesPaths(start, { paths: paths });
44+
45+
var expectedDirs = getAllNodeModulePathsUpwards(start);
46+
47+
t.deepEqual(dirs, expectedDirs.concat(paths));
48+
49+
t.end();
50+
});
51+
52+
t.test('with moduleDirectory option', function (t) {
53+
var start = path.join(__dirname, 'resolver');
54+
var paths = ['a', 'b'];
55+
var dirs = nodeModulesPaths(start, { paths: paths });
56+
57+
var expectedDirs = getAllNodeModulePathsUpwards(start);
58+
59+
t.deepEqual(dirs, expectedDirs.concat(paths));
60+
61+
t.end();
62+
});
63+
64+
t.test('with moduleDirectory and paths options', function (t) {
65+
var start = path.join(__dirname, 'resolver');
66+
var paths = ['a', 'b'];
67+
var moduleDirectory = 'not node modules';
68+
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectory });
69+
70+
var expectedDirs = getAllNodeModulePathsUpwards(start, moduleDirectory);
71+
72+
t.deepEqual(dirs, expectedDirs.concat(paths));
73+
74+
t.end();
75+
});
76+
});

0 commit comments

Comments
 (0)