AK
Size: a a a
AK
АП
˸A
const parallelShell = (commands, settings = {}) => {
const enrichedSettings = {
stopOnError: false,
threads: 10,
logLevel: 'warning',
...settings
}
const log = (level, message) => {
if (message && LogLevels.indexOf(enrichedSettings.logLevel) >= LogLevels.indexOf(level))
level === 'error' || level === 'warning'
? console.error(message)
: console.log(message)
}
let lazyPromises = commands.reduce((result, command, index) => ({...result,
[index]: () => {
const start = new Date().getTime()
const shellCommand = Array.isArray(command) ? command[0] : command
const infoCommand = Array.isArray(command) && command[1] ? command[1] : shellCommand
log('info', `${infoCommand} - Started...`)
delete lazyPromises[index]
return new Promise((res, rej) => {
exec(shellCommand, (err, out, stderr) => {
log('debug', out)
log('warning', stderr)
log('error', err)
if (err && enrichedSettings.stopOnError) return rej(err)
log('info', `${infoCommand} - Done in ${(new Date().getTime() - start)/1000}s`)
if (Object.keys(lazyPromises).length === 0) {
return res()
}
return lazyPromises[Object.keys(lazyPromises)[0]]()
})
})
}
}), {})
return Promise.all(Object.values(lazyPromises).slice(0, enrichedSettings.threads).map(f => f()))
}
F
АМ
F
VL
F
Т
Т
АМ
ES
˸A
АМ
N
Т
ES
Т
ES
N