-
-
Notifications
You must be signed in to change notification settings - Fork 191
Implements useProcessResolution
#170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
e011edc
5f11711
109f275
5797755
5655d0a
c9faab9
9daf6b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ var path = require('path'); | |
| var fs = require('fs'); | ||
| var parse = path.parse || require('path-parse'); | ||
|
|
||
| module.exports = function nodeModulesPaths(start, opts) { | ||
| module.exports = function nodeModulesPaths(x, start, opts) { | ||
| var modules = opts && opts.moduleDirectory | ||
| ? [].concat(opts.moduleDirectory) | ||
| : ['node_modules']; | ||
|
|
@@ -20,25 +20,37 @@ module.exports = function nodeModulesPaths(start, opts) { | |
| } | ||
| } | ||
|
|
||
| var prefix = '/'; | ||
| if ((/^([A-Za-z]:)/).test(absoluteStart)) { | ||
| prefix = ''; | ||
| } else if ((/^\\\\/).test(absoluteStart)) { | ||
| prefix = '\\\\'; | ||
| } | ||
| var dirs = []; | ||
|
|
||
| if (!opts || typeof opts.useNodeModules === 'undefined' || opts.useNodeModules) { | ||
|
||
| var prefix = '/'; | ||
| if ((/^([A-Za-z]:)/).test(absoluteStart)) { | ||
| prefix = ''; | ||
| } else if ((/^\\\\/).test(absoluteStart)) { | ||
| prefix = '\\\\'; | ||
| } | ||
|
|
||
| var paths = [absoluteStart]; | ||
| var parsed = parse(absoluteStart); | ||
| while (parsed.dir !== paths[paths.length - 1]) { | ||
| paths.push(parsed.dir); | ||
| parsed = parse(parsed.dir); | ||
| var paths = [absoluteStart]; | ||
| var parsed = parse(absoluteStart); | ||
| while (parsed.dir !== paths[paths.length - 1]) { | ||
| paths.push(parsed.dir); | ||
| parsed = parse(parsed.dir); | ||
| } | ||
|
|
||
| dirs = paths.reduce(function (dirs, aPath) { | ||
| return dirs.concat(modules.map(function (moduleDir) { | ||
| return path.join(prefix, aPath, moduleDir); | ||
| })); | ||
| }, []); | ||
| } | ||
|
|
||
| var dirs = paths.reduce(function (dirs, aPath) { | ||
| return dirs.concat(modules.map(function (moduleDir) { | ||
| return path.join(prefix, aPath, moduleDir); | ||
| })); | ||
| }, []); | ||
| if (opts && opts.paths) { | ||
| if (typeof opts.paths === 'function') { | ||
| dirs = dirs.concat(opts.paths(x, absoluteStart)); | ||
| } else { | ||
| dirs = dirs.concat(opts.paths); | ||
| } | ||
| } | ||
|
|
||
| return opts && opts.paths ? dirs.concat(opts.paths) : dirs; | ||
| return dirs; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| var path = require('path'); | ||
|
|
||
| module.exports = function processResolution(opts) { | ||
| if (process.versions.pnp) { | ||
| var pnp = require('pnpapi'); | ||
|
|
||
| opts.preserveSymlinks = true; | ||
| opts.useNodeModules = false; | ||
| opts.paths = function (request, basedir) { | ||
| // Extract the name of the package being requested (1=full name, 2=scope name, 3=local name) | ||
| var parts = request.match(/^((?:(@[^\/]+)\/)?([^\/]+))/); | ||
|
|
||
| // This is guaranteed to return the path to the "package.json" file from the given package | ||
| var manifestPath = pnp.resolveToUnqualified(parts[1] + '/package.json', basedir); | ||
|
|
||
| // The first dirname strips the package.json, the second strips the local named folder | ||
| var nodeModules = path.dirname(path.dirname(manifestPath)); | ||
|
|
||
| // Strips the scope named folder if needed | ||
| if (parts[2]) { | ||
| nodeModules = path.dirname(nodeModules); | ||
| } | ||
|
|
||
| return [nodeModules]; | ||
| }; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,11 @@ var nodeModulesPaths = require('../lib/node-modules-paths'); | |
|
|
||
| var verifyDirs = function verifyDirs(t, start, dirs, moduleDirectories, paths) { | ||
| var moduleDirs = [].concat(moduleDirectories || 'node_modules'); | ||
| if (paths) { | ||
| for (var k = 0; k < paths.length; ++k) { | ||
| moduleDirs.push(path.basename(paths[k])); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems like a behavior change - the test is now using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only one test was using the path parameter previously and it was providing straight folder names (
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair, but why is the basename change needed? Was it incorrect for the test to omit it previously (and there weren't enough test cases to demonstrate that)?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, missed the comment, my bad! The basename change was needed because the previous testcases were only using |
||
| } | ||
| } | ||
|
|
||
| var foundModuleDirs = {}; | ||
| var uniqueDirs = {}; | ||
|
|
@@ -20,7 +25,7 @@ var verifyDirs = function verifyDirs(t, start, dirs, moduleDirectories, paths) { | |
| } | ||
| t.equal(keys(parsedDirs).length >= start.split(path.sep).length, true, 'there are >= dirs than "start" has'); | ||
| var foundModuleDirNames = keys(foundModuleDirs); | ||
| t.deepEqual(foundModuleDirNames, moduleDirs.concat(paths || []), 'all desired module dirs were found'); | ||
| t.deepEqual(foundModuleDirNames, moduleDirs, 'all desired module dirs were found'); | ||
| t.equal(keys(uniqueDirs).length, dirs.length, 'all dirs provided were unique'); | ||
|
|
||
| var counts = {}; | ||
|
|
@@ -33,7 +38,7 @@ var verifyDirs = function verifyDirs(t, start, dirs, moduleDirectories, paths) { | |
| test('node-modules-paths', function (t) { | ||
| t.test('no options', function (t) { | ||
| var start = path.join(__dirname, 'resolver'); | ||
| var dirs = nodeModulesPaths(start); | ||
| var dirs = nodeModulesPaths('pkg', start); | ||
|
|
||
| verifyDirs(t, start, dirs); | ||
|
|
||
|
|
@@ -42,27 +47,51 @@ test('node-modules-paths', function (t) { | |
|
|
||
| t.test('empty options', function (t) { | ||
| var start = path.join(__dirname, 'resolver'); | ||
| var dirs = nodeModulesPaths(start, {}); | ||
| var dirs = nodeModulesPaths('pkg', start, {}); | ||
|
|
||
| verifyDirs(t, start, dirs); | ||
|
|
||
| t.end(); | ||
| }); | ||
|
|
||
| t.test('with paths option', function (t) { | ||
| t.test('with useNodeModules=false option', function (t) { | ||
| var start = path.join(__dirname, 'resolver'); | ||
| var dirs = nodeModulesPaths('pkg', start, { useNodeModules: false }); | ||
|
|
||
| t.deepEqual(dirs, [], 'no node_modules was computed'); | ||
|
|
||
| t.end(); | ||
| }); | ||
|
|
||
| t.test('with paths=array option', function (t) { | ||
| var start = path.join(__dirname, 'resolver'); | ||
| var paths = ['a', 'b']; | ||
| var dirs = nodeModulesPaths(start, { paths: paths }); | ||
| var dirs = nodeModulesPaths('pkg', start, { paths: paths }); | ||
|
|
||
| verifyDirs(t, start, dirs, null, paths); | ||
|
|
||
| t.end(); | ||
| }); | ||
|
|
||
| t.test('with paths=function option', function (t) { | ||
| var paths = function paths(absoluteStart) { | ||
| return [path.join(absoluteStart, 'not node modules')]; | ||
| }; | ||
|
|
||
| var start = path.join(__dirname, 'resolver'); | ||
| var dirs = nodeModulesPaths('pkg', start, { paths: paths }); | ||
|
|
||
| console.log(dirs); | ||
|
|
||
| verifyDirs(t, start, dirs, null, [path.join(start, 'not node modules')]); | ||
|
|
||
| t.end(); | ||
| }); | ||
|
|
||
| t.test('with moduleDirectory option', function (t) { | ||
| var start = path.join(__dirname, 'resolver'); | ||
| var moduleDirectory = 'not node modules'; | ||
| var dirs = nodeModulesPaths(start, { moduleDirectory: moduleDirectory }); | ||
| var dirs = nodeModulesPaths('pkg', start, { moduleDirectory: moduleDirectory }); | ||
|
|
||
| verifyDirs(t, start, dirs, moduleDirectory); | ||
|
|
||
|
|
@@ -73,7 +102,7 @@ test('node-modules-paths', function (t) { | |
| var start = path.join(__dirname, 'resolver'); | ||
| var paths = ['a', 'b']; | ||
| var moduleDirectory = 'not node modules'; | ||
| var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectory }); | ||
| var dirs = nodeModulesPaths('pkg', start, { paths: paths, moduleDirectory: moduleDirectory }); | ||
|
|
||
| verifyDirs(t, start, dirs, moduleDirectory, paths); | ||
|
|
||
|
|
@@ -84,7 +113,7 @@ test('node-modules-paths', function (t) { | |
| var start = path.join(__dirname, 'resolver'); | ||
| var paths = ['a', 'b']; | ||
| var moduleDirectories = ['not node modules', 'other modules']; | ||
| var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories }); | ||
| var dirs = nodeModulesPaths('pkg', start, { paths: paths, moduleDirectory: moduleDirectories }); | ||
|
|
||
| verifyDirs(t, start, dirs, moduleDirectories, paths); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changing the signature in this way makes it a breaking change; also, what is "x"?
can we make this be the last argument instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure if it was a publicly exposed function, since it wasn't listed in the doc.
xis the request being made (took the variable name from other parts of the code). It can easily be moved to the end of the argument list, for sureThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything reachable is part of the public API; docs are irrelevant - so any break in anything that's exposed would be semver-major.