warp.firstclass
Version:
Allows to access all your modules directly like: require('moduleName') by creating symlinks
53 lines (45 loc) • 1.53 kB
JavaScript
var link = require('fs-symlink'),
_ = require('lodash'),
promise = require('bluebird'),
fs = require('fs');
module.exports = {
link: function(dirToLink){
if (!dirToLink){
throw new TypeError("No 'dirToLink' argument provided");
} else {
console.log('Running warp.firstclass');
}
var opts = {
dirToLink: format(dirToLink)
};
var dirs = fs.readdirSync(opts.dirToLink);
var dirsToLink = _.filter(dirs, function(dir){
return !/\./.test(dir); // only directories, no files allowed
});
return promise.each(dirsToLink, function(dir){
var from = './node_modules/' + dir,
to = opts.dirToLink + dir ;
console.log('creating symlink from %s to %s', from, to);
if (/^win/.test(process.platform)) {
return link( to , from, 'junction');
} else {
return link( to , from, 'dir');
}
}).then(function () {
console.log('done');
return promise.resolve()
}).catch(function(err){
console.log(err);
return promise.reject(err);
});
function format(dir){
// add forward slash in the end if needed
return /\/$/.test(dir)?dir : dir+'/';
}
}
};
if (__filename == process.argv[1]){
// start if invoked directly
var dirToLink = process.argv[2];
module.exports.link(dirToLink);
}