npm-batch-install
Version:
Install dependencies in batches
157 lines (120 loc) • 3.93 kB
JavaScript
;
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
const pkg = require('../package.json');
const chalk = require('chalk');
const spawnSync = require('cross-spawn').sync;
var _require = require('./usage');
const help = _require.help;
const config = _require.config;
const argv = require('minimist')(process.argv.slice(2), config);
const PREFIX = chalk.bgBlue.bold(pkg.name);
/**
* getMissingDeps
* @return {[String]} list of packages
*/
function getMissingDeps() {
const missing = [];
const npmList = spawnSync('npm', ['ls', '--depth=0', '--json', '--prod', '--dev']);
var _JSON$parse = JSON.parse(npmList.stdout.toString());
const dependencies = _JSON$parse.dependencies;
for (let depName of Object.keys(dependencies)) {
const dep = dependencies[depName];
if (dep.missing && dep.missing === true) {
missing.push(depName);
}
}
return missing;
}
/**
* [logInfo description]
* @param {[type]} args [description]
* @return {[type]} [description]
*/
function logInfo() {
var _console;
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (_console = console).log.apply(_console, [PREFIX, chalk.dim('INFO')].concat(args));
}
/**
* [logError description]
* @param {[type]} args [description]
* @return {[type]} [description]
*/
function logError() {
return console.log(PREFIX, chalk.red.bold('ERR!'), chalk.red.apply(chalk, arguments));
}
/**
* [logPackages description]
* @param {[type]} packages [description]
* @param {String} [icon=''] [description]
* @return {[type]} [description]
*/
function logPackages(packages) {
let icon = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
const indent = '├──';
const args = icon ? [indent, icon] : [indent];
return packages.map(pkg => {
var _console2;
return (_console2 = console).log.apply(_console2, args.concat([pkg]));
});
}
/**
* [npmInstall description]
* @param {[type]} args [description]
* @return {[type]} [description]
*/
function npmInstall() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return spawnSync('npm', ['install', '--silent'].concat(args), { stdio: 'inherit' });
}
if (argv.help === true) {
console.log(help(pkg));
process.exit(0);
}
if (argv.version === true) {
console.log(pkg.version);
process.exit(1);
}
const installArgs = argv._;
const missing = installArgs.length ? installArgs : getMissingDeps();
const failed = [];
const installed = [];
const batchSize = argv.batchSize;
if (missing.length) {
logInfo(missing.length, 'packages to install');
logPackages(missing);
} else {
logInfo('Nothing to do here.');
}
while (missing.length) {
const batch = missing.splice(0, batchSize);
logInfo('Attempting to install:', batch.join(', '));
var _npmInstall = npmInstall.apply(undefined, _toConsumableArray(batch));
const status = _npmInstall.status;
if (status) {
logError('Could not install batch:', batch.join(', '));
if (batchSize == 1) {
failed.push.apply(failed, _toConsumableArray(batch));
continue;
}
batch.forEach(dep => {
logInfo('Trying to install', dep);
var _npmInstall2 = npmInstall(dep);
const status = _npmInstall2.status;
if (status) {
logError('Failed to install:', dep);
failed.push(dep);
}
});
} else {
Array.prototype.push.apply(installed, batch);
}
}
logInfo('Installed:', installed.length, 'Failed:', failed.length);
logPackages(installed, chalk.green('\u2713'));
logPackages(failed, chalk.red('\u2717'));
process.exit(failed.length ? 1 : 0);