feat: limit concurrency of root build script
Should fixes https://xcp-ng.org/forum/post/54567
This commit is contained in:
+37
-27
@@ -5,6 +5,7 @@
|
||||
const { delimiter } = require('path')
|
||||
const { forEach, fromEvent } = require('promise-toolbox')
|
||||
const { spawn } = require('child_process')
|
||||
const getopts = require('getopts')
|
||||
|
||||
const { getPackages } = require('./utils')
|
||||
|
||||
@@ -14,37 +15,46 @@ const { env } = process
|
||||
//
|
||||
// TODO: https://docs.npmjs.com/misc/scripts#environment
|
||||
require('exec-promise')(args => {
|
||||
const parallel = args[0] === '--parallel'
|
||||
const script = args[parallel ? 1 : 0]
|
||||
const {
|
||||
concurrency,
|
||||
parallel,
|
||||
_: [script],
|
||||
} = getopts(args, {
|
||||
boolean: ['parallel'],
|
||||
string: ['concurrency'],
|
||||
})
|
||||
|
||||
let errors = 0
|
||||
return getPackages(true)
|
||||
[parallel ? 'map' : 'forEach'](({ dir, name, package: { scripts } }) => {
|
||||
if (scripts == null) {
|
||||
return
|
||||
}
|
||||
|
||||
const spawnOpts = {
|
||||
cwd: dir,
|
||||
env: Object.assign({}, env, {
|
||||
PATH: `${dir}/node_modules/.bin${delimiter}${env.PATH}`,
|
||||
}),
|
||||
shell: true,
|
||||
stdio: 'inherit',
|
||||
}
|
||||
return forEach.call([`pre${script}`, script, `post${script}`], script => {
|
||||
const command = scripts[script]
|
||||
if (command !== undefined) {
|
||||
console.log(`* ${name}:${script} −`, command)
|
||||
return fromEvent(spawn(command, spawnOpts), 'exit').then(code => {
|
||||
if (code !== 0) {
|
||||
++errors
|
||||
console.log(`* ${name}:${script} − Error:`, code)
|
||||
}
|
||||
})
|
||||
[parallel ? 'map' : 'forEach'](
|
||||
({ dir, name, package: { scripts } }) => {
|
||||
if (scripts == null) {
|
||||
return
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
const spawnOpts = {
|
||||
cwd: dir,
|
||||
env: Object.assign({}, env, {
|
||||
PATH: `${dir}/node_modules/.bin${delimiter}${env.PATH}`,
|
||||
}),
|
||||
shell: true,
|
||||
stdio: 'inherit',
|
||||
}
|
||||
return forEach.call([`pre${script}`, script, `post${script}`], script => {
|
||||
const command = scripts[script]
|
||||
if (command !== undefined) {
|
||||
console.log(`* ${name}:${script} −`, command)
|
||||
return fromEvent(spawn(command, spawnOpts), 'exit').then(code => {
|
||||
if (code !== 0) {
|
||||
++errors
|
||||
console.log(`* ${name}:${script} − Error:`, code)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
{ concurrency: concurrency ? Number(concurrency) : undefined }
|
||||
)
|
||||
.then(() => {
|
||||
if (errors !== 0) {
|
||||
throw errors
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { asyncEach } = require('@vates/async-each')
|
||||
const { forEach, fromCallback } = require('promise-toolbox')
|
||||
const { join } = require('path')
|
||||
const fs = require('fs')
|
||||
@@ -34,7 +35,7 @@ exports.getPackages = (readPackageJson = false) => {
|
||||
: pkgs
|
||||
})
|
||||
p.forEach = fn => p.then(pkgs => forEach.call(pkgs, fn))
|
||||
p.map = fn => p.then(pkgs => Promise.all(pkgs.map(fn))).then(noop)
|
||||
p.map = (fn, opts) => p.then(pkgs => asyncEach(pkgs, fn, opts)).then(noop)
|
||||
return p
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user