/**
* Link a npm package if missing.
* @function task.worker.linkNpmPackage
* @param {object} config - Task configuration.
* @param {string} config.packageName - Package name to link.
* @param {function} callback - Callback when done.
* @author Taka Okunishi
*/
var path = require('path'),
execCommand = require('./index').execCommand;
exports = module.exports = function (config, callback) {
var found = exports._findup(config.packageName)
if (found) {
callback(null);
} else {
execCommand({
command: ['npm', 'link', config.packageName].join(' ')
}, callback);
}
};
exports._findup = function (packageName) {
var dirnames = path.dirname(path.resolve('.')).split(path.sep);
while (dirnames.length) {
var searchPath = path.resolve(dirnames.join(path.sep), 'node_modules', packageName);
try {
require.resolve(searchPath);
return true;
} catch (e) {
//Do nothing.
}
dirnames.pop();
}
return false;
};