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:08:48 +01:00
parent 7a4cec5093
commit 08298d3284
3 changed files with 42 additions and 29 deletions

View File

@ -3,6 +3,7 @@
"@babel/core": "^7.0.0", "@babel/core": "^7.0.0",
"@babel/eslint-parser": "^7.13.8", "@babel/eslint-parser": "^7.13.8",
"@babel/register": "^7.0.0", "@babel/register": "^7.0.0",
"@vates/async-each": "1.0.0",
"babel-jest": "^29.0.3", "babel-jest": "^29.0.3",
"benchmark": "^2.1.4", "benchmark": "^2.1.4",
"deptree": "^1.0.0", "deptree": "^1.0.0",
@ -16,6 +17,7 @@
"eslint-plugin-promise": "^6.0.0", "eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "^7.21.5", "eslint-plugin-react": "^7.21.5",
"exec-promise": "^0.7.0", "exec-promise": "^0.7.0",
"getopts": "^2.3.0",
"globby": "^13.1.1", "globby": "^13.1.1",
"handlebars": "^4.7.6", "handlebars": "^4.7.6",
"husky": "^4.2.5", "husky": "^4.2.5",
@ -77,7 +79,7 @@
}, },
"private": true, "private": true,
"scripts": { "scripts": {
"build": "scripts/run-script.js --parallel build", "build": "scripts/run-script.js --parallel --concurrency 2 build",
"ci": "yarn && yarn build && yarn test-integration", "ci": "yarn && yarn build && yarn test-integration",
"clean": "scripts/run-script.js --parallel clean", "clean": "scripts/run-script.js --parallel clean",
"dev": "scripts/run-script.js --parallel dev", "dev": "scripts/run-script.js --parallel dev",

View File

@ -5,6 +5,7 @@
const { delimiter } = require('path') const { delimiter } = require('path')
const { forEach, fromEvent } = require('promise-toolbox') const { forEach, fromEvent } = require('promise-toolbox')
const { spawn } = require('child_process') const { spawn } = require('child_process')
const getopts = require('getopts')
const { getPackages } = require('./utils') const { getPackages } = require('./utils')
@ -14,37 +15,46 @@ const { env } = process
// //
// TODO: https://docs.npmjs.com/misc/scripts#environment // TODO: https://docs.npmjs.com/misc/scripts#environment
require('exec-promise')(args => { require('exec-promise')(args => {
const parallel = args[0] === '--parallel' const {
const script = args[parallel ? 1 : 0] concurrency,
parallel,
_: [script],
} = getopts(args, {
boolean: ['parallel'],
string: ['concurrency'],
})
let errors = 0 let errors = 0
return getPackages(true) return getPackages(true)
[parallel ? 'map' : 'forEach'](({ dir, name, package: { scripts } }) => { [parallel ? 'map' : 'forEach'](
if (scripts == null) { ({ dir, name, package: { scripts } }) => {
return 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)
}
})
} }
})
}) 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(() => { .then(() => {
if (errors !== 0) { if (errors !== 0) {
throw errors throw errors

View File

@ -1,5 +1,6 @@
'use strict' 'use strict'
const { asyncEach } = require('@vates/async-each')
const { forEach, fromCallback } = require('promise-toolbox') const { forEach, fromCallback } = require('promise-toolbox')
const { join } = require('path') const { join } = require('path')
const fs = require('fs') const fs = require('fs')
@ -34,7 +35,7 @@ exports.getPackages = (readPackageJson = false) => {
: pkgs : pkgs
}) })
p.forEach = fn => p.then(pkgs => forEach.call(pkgs, fn)) 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 return p
} }