feat: limit concurrency of root build script

Should fixes https://xcp-ng.org/forum/post/54567
This commit is contained in:
Julien Fontanet
2022-11-10 18:09:05 +01:00
parent 7a4cec5093
commit 08298d3284
3 changed files with 42 additions and 29 deletions
+37 -27
View File
@@ -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
View File
@@ -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
}