othernpm
Version:
Run npm commands in other directories
58 lines (52 loc) • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = othernpm;
/**
* Create a function which run npm commands by the specified methods.
* @param {Object} io
* @param {Function} io.spawn - A function which spawns a new process.
* @param {Function} io.isDirectory - A function which determines
* whether the given path is a directory or not.
* @param {Function} io.onExit - A callback executed when the process exits.
* @return {Function} - A function which actually run commands.
*/
function othernpm(_ref) {
var spawn = _ref.spawn,
isDirectory = _ref.isDirectory,
onExit = _ref.onExit;
/**
* Run npm commands in the specified directory.
* (Curried function).
* @param {string} path - A path to a directory where npm commands run.
* @param {string|Array} command - The npm command(s) like 'run test'.
* @param {Object} spawnConfig - A config object passed to 'spawn' function.
* @return {*} - An object returned by 'io.spawn'.
*/
return function (path) {
return function () {
var command = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var spawnConfig = arguments[1];
if (!isDirectory(path)) {
throw new Error('The directory \'' + path + '\' is not found');
}
var commands = parseCommand(command);
var config = Object.assign({
cwd: path,
stdio: 'inherit'
}, spawnConfig);
var proc = spawn('npm', commands, config);
if (proc && 'on' in proc) {
proc.on('exit', onExit);
}
return proc;
};
};
}
function parseCommand(command) {
if (Array.isArray(command)) {
return command;
}
return command.trim().length === 0 ? [] : command.trim().split(/\s+/);
}