Comparing version 3.2.0 to 3.3.0
112
lib/loop.js
const chalk = require('chalk'); | ||
const debug = require('debug')('loop'); | ||
const expandCommand = require('../lib/expandCommand'); | ||
const getFlag = require('../lib/getFlag'); | ||
const getArgsForFlag = require('../lib/getArgsForFlag'); | ||
@@ -12,21 +13,21 @@ const listDirectories = require('../lib/listDirectories'); | ||
module.exports = (options, cb, errorCb) => { | ||
const cwd = options.dir || process.cwd(); | ||
var dirs = options.directories || listDirectories(options.looprc, cwd); | ||
var looprc = options["looprc"] || []; | ||
var looprc = options['looprc'] || []; | ||
options.exclude = options.exclude || getArgsForFlag(process.argv, '--exclude') || looprc.exclude, | ||
options.excludeOnly = options.excludeOnly || getArgsForFlag(process.argv, '--exclude-only') || looprc.excludeOnly, | ||
options.include = options.include || getArgsForFlag(process.argv, '--include') || looprc.include, | ||
options.includeOnly = options.includeOnly || getArgsForFlag(process.argv, '--include-only') || looprc.includeOnly | ||
options.exitOnError = options.exitOnError || getArgsForFlag(process.argv, '--exit-on-error'), | ||
options.exitOnAggregateError = options.exitOnAggregateError || getArgsForFlag(process.argv, '--exit-on-aggregate-error') | ||
options.exclude = options.exclude || getArgsForFlag(process.argv, '--exclude') || looprc.exclude; | ||
options.excludeOnly = options.excludeOnly || getArgsForFlag(process.argv, '--exclude-only') || looprc.excludeOnly; | ||
options.exitOnAggregateError = | ||
options.exitOnAggregateError || getArgsForFlag(process.argv, '--exit-on-aggregate-error'); | ||
options.exitOnError = options.exitOnError || getArgsForFlag(process.argv, '--exit-on-error'); | ||
options.include = options.include || getArgsForFlag(process.argv, '--include') || looprc.include; | ||
options.includeOnly = options.includeOnly || getArgsForFlag(process.argv, '--include-only') || looprc.includeOnly; | ||
options.parallel = options.parallel || getFlag(process.argv, '--parallel') || looprc.parallel; | ||
debug(`executing ${util.inspect(options)} ${process.argv}`); | ||
debug(`executing ${util.inspect(options)}`); | ||
if (options.excludeOnly) { | ||
dirs = listDirectories({ ignore: options.excludeOnly }, options.dir) | ||
dirs = listDirectories({ ignore: options.excludeOnly }, options.dir); | ||
} else if (options.exclude) { | ||
dirs = dirs.filter((dir) => { | ||
dirs = dirs.filter(dir => { | ||
return options.exclude.indexOf(path.basename(dir)) === -1; | ||
@@ -39,3 +40,8 @@ }); | ||
} else if (options.include) { | ||
dirs = _.union(dirs, options.include.map((i) => { return path.resolve(cwd, i); })).sort(); | ||
dirs = _.union( | ||
dirs, | ||
options.include.map(i => { | ||
return path.resolve(cwd, i); | ||
}) | ||
).sort(); | ||
} | ||
@@ -50,48 +56,52 @@ | ||
mapExec(commands, (err, commandOutputs) => { | ||
mapExec( | ||
commands, | ||
options, | ||
(err, commandOutputs) => { | ||
if (options.exitOnAggregateError && commandOutputsWithErrors.length > 0) { | ||
console.log(); | ||
let index = 0; | ||
console.log( | ||
`one or more child commands executed by loop encountered an error:\n${commandOutputsWithErrors.reduce( | ||
(a, b) => { | ||
return `${a}\n${++index}: ${b}`; | ||
}, | ||
'' | ||
)}` | ||
); | ||
console.log(); | ||
process.exit(1); | ||
} | ||
if (options.exitOnAggregateError && commandOutputsWithErrors.length > 0) { | ||
console.log(); | ||
let index = 0; | ||
console.log(`one or more child commands executed by loop encountered an error:\n${commandOutputsWithErrors.reduce((a, b) => { | ||
return `${a}\n${++index}: ${b}`; | ||
}, '')}`); | ||
console.log(); | ||
process.exit(1); | ||
} | ||
const results = commandOutputs.reduce((logMessage, commandOutput, index) => { | ||
const color = commandOutput.error ? 'red' : 'green'; | ||
const code = commandOutput.error && commandOutput.error.code !== 0 ? commandOutput.error.code : null; | ||
const output = commandOutput.output || commandOutput.error; | ||
const results = commandOutputs.reduce((logMessage, commandOutput, index) => { | ||
const color = commandOutput.error ? 'red' : 'green'; | ||
const code = commandOutput.error && commandOutput.error.code !== 0 | ||
? commandOutput.error.code | ||
: null; | ||
const output = commandOutput.output || commandOutput.error; | ||
if (commandOutput.error) { | ||
if (options.exitOnError) { | ||
console.log(`a child command executed by loop encountered an error:\n${commandOutput.error}`); | ||
process.exit(1); | ||
} | ||
commandOutputsWithErrors.push(commandOutput.error); | ||
} | ||
if (commandOutput.error) { | ||
if (options.exitOnError) { | ||
console.log(`a child command executed by loop encountered an error:\n${commandOutput.error}`); | ||
process.exit(1); | ||
if (code && options.exitOnError) { | ||
process.exit(code); | ||
} | ||
commandOutputsWithErrors.push(commandOutput.error); | ||
} | ||
if (code && options.exitOnError) { | ||
process.exit(code); | ||
return `${logMessage}\n${output}`; | ||
}, ''); | ||
if (cb) return cb(null, results); | ||
}, | ||
error => { | ||
if (options.exitOnError) { | ||
console.log(`a child command executed by loop encountered an error:\n${error}`); | ||
process.exit(1); | ||
} | ||
return `${logMessage}\n${output}`; | ||
}, ''); | ||
if (cb) return cb(null, results); | ||
}, (error) => { | ||
if (options.exitOnError) { | ||
console.log(`a child command executed by loop encountered an error:\n${error}`); | ||
process.exit(1); | ||
commandOutputsWithErrors.push(error); | ||
} | ||
commandOutputsWithErrors.push(error); | ||
}); | ||
); | ||
}; |
@@ -6,13 +6,20 @@ const async = require('async'); | ||
module.exports = (commands, cb, errorCb) => { | ||
return async.map(commands, (cmd, cb) => { | ||
return exec({ | ||
command: cmd.command, | ||
dir: cmd.dir, | ||
exitOnError: cmd.exitOnError, | ||
exitOnAggregateError: cmd.exitOnAggregateError, | ||
}, cb, errorCb); | ||
}, cb); | ||
} | ||
module.exports = (commands, options, cb, errorCb) => { | ||
return async.map( | ||
commands, | ||
(cmd, cb) => { | ||
return exec( | ||
{ | ||
command: cmd.command, | ||
dir: cmd.dir, | ||
exitOnError: cmd.exitOnError, | ||
exitOnAggregateError: cmd.exitOnAggregateError, | ||
parallel: options && options.parallel, | ||
}, | ||
cb, | ||
errorCb | ||
); | ||
}, | ||
cb | ||
); | ||
}; |
{ | ||
"name": "loop", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"description": "loop through commands in fun and amazing ways!", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19374
23
203