Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Separate duplicated nodeModulesPaths function

  • Loading branch information...
commit 325584a685db8f42aae3d4876ffbe64069233601 1 parent b1e04d7
Forbes Lindesay authored committed
Showing with 26 additions and 41 deletions.
  1. +2 −20 lib/async.js
  2. +22 −0 lib/node-modules-paths.js
  3. +2 −21 lib/sync.js
View
22 lib/async.js
@@ -2,6 +2,7 @@ var core = require('./core');
var fs = require('fs');
var path = require('path');
var caller = require('./caller.js');
+var nodeModulesPaths = require('./node-modules-paths.js');
module.exports = function resolve (x, opts, cb) {
if (core[x]) return cb(null, x);
@@ -119,25 +120,6 @@ module.exports = function resolve (x, opts, cb) {
process(dirs.slice(1));
});
});
- })(nodeModulesPaths(start));
- }
-
- function nodeModulesPaths (start, cb) {
- var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
- var parts = start.split(splitRe);
-
- var dirs = [];
- for (var i = parts.length - 1; i >= 0; i--) {
- if (parts[i] === modules) continue;
- var dir = path.join(
- path.join.apply(path, parts.slice(0, i + 1)),
- modules
- );
- if (!parts[0].match(/([A-Za-z]:)/)) {
- dir = '/' + dir;
- }
- dirs.push(dir);
- }
- return dirs.concat(opts.paths);
+ })(nodeModulesPaths(start, opts));
}
};
View
22 lib/node-modules-paths.js
@@ -0,0 +1,22 @@
+var path = require('path');
+
+
+module.exports = function (start, opts) {
+ var modules = opts.moduleDirectory || 'node_modules';
+ var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
+ var parts = start.split(splitRe);
+
+ var dirs = [];
+ for (var i = parts.length - 1; i >= 0; i--) {
+ if (parts[i] === modules) continue;
+ var dir = path.join(
+ path.join.apply(path, parts.slice(0, i + 1)),
+ modules
+ );
+ if (!parts[0].match(/([A-Za-z]:)/)) {
+ dir = '/' + dir;
+ }
+ dirs.push(dir);
+ }
+ return dirs.concat(opts.paths);
+}
View
23 lib/sync.js
@@ -2,6 +2,7 @@ var core = require('./core');
var fs = require('fs');
var path = require('path');
var caller = require('./caller.js');
+var nodeModulesPaths = require('./node-modules-paths.js');
module.exports = function (x, opts) {
if (core[x]) return x;
@@ -16,7 +17,6 @@ module.exports = function (x, opts) {
var extensions = opts.extensions || [ '.js' ];
var y = opts.basedir || path.dirname(caller());
- var modules = opts.moduleDirectory || 'node_modules';
opts.paths = opts.paths || [];
@@ -68,7 +68,7 @@ module.exports = function (x, opts) {
}
function loadNodeModulesSync (x, start) {
- var dirs = nodeModulesPathsSync(start);
+ var dirs = nodeModulesPaths(start, opts);
for (var i = 0; i < dirs.length; i++) {
var dir = dirs[i];
var m = loadAsFileSync(path.join( dir, '/', x));
@@ -77,23 +77,4 @@ module.exports = function (x, opts) {
if (n) return n;
}
}
-
- function nodeModulesPathsSync (start) {
- var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
- var parts = start.split(splitRe);
-
- var dirs = [];
- for (var i = parts.length - 1; i >= 0; i--) {
- if (parts[i] === modules) continue;
- var dir = path.join(
- path.join.apply(path, parts.slice(0, i + 1)),
- modules
- );
- if (!parts[0].match(/([A-Za-z]:)/)) {
- dir = '/' + dir;
- }
- dirs.push(dir);
- }
- return dirs.concat(opts.paths);
- }
};
Please sign in to comment.
Something went wrong with that request. Please try again.