tooljs-tool
Version:
Composible Tooling for JavaScript
59 lines (48 loc) • 1.63 kB
JavaScript
/**
* Module dependencies.
*/
var path = require('path');
/**
* TODO: move to a separate module.
*/
module.exports = function(){
var paths = [];
var win32 = process.platform === 'win32';
// Walk up the CWD and add `node_modules/` folder lookup on each level
process.cwd().split(path.sep).forEach(function(part, i, parts){
var lookup = path.join.apply(path, parts.slice(0, i + 1).concat(['node_modules']));
if (!win32) lookup = '/' + lookup;
paths.push(lookup);
});
// Adding global NPM directories
// We tried using NPM to get the global modules path, but it haven't work out
// because of bugs in the parseable implementation of `ls` command and mostly
// performance issues. So, we go with our best bet for now.
if (process.env.NODE_PATH) {
paths = compact(process.env.NODE_PATH.split(path.delimiter)).concat(paths);
} else {
// global node_modules should be 5 directory up this one (most of the time)
paths.push(path.join(__dirname, '../../../../..'));
// adds support for generator resolving when yeoman-generator has been linked
paths.push(path.join(path.dirname(process.argv[1]), '../..'));
// Default paths for each system
if (win32) {
paths.push(path.join(process.env.APPDATA, 'npm/node_modules'));
} else {
paths.push('/usr/lib/node_modules');
paths.push('/usr/local/lib/node_modules');
}
}
return paths.reverse();
};
/**
* Remove blank items from array.
*/
function compact(arr) {
var res = [];
for (var i = 0, n = arr.length; i < n; i++) {
if (!arr[i]) continue;
res.push(arr[i]);
}
return res;
}