From 08298d3284119ad855552af36a810a3a9a006759 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 10 Nov 2022 18:08:48 +0100 Subject: [PATCH] feat: limit concurrency of root build script Should fixes https://xcp-ng.org/forum/post/54567 --- package.json | 4 ++- scripts/run-script.js | 64 +++++++++++++++++++++++++------------------ scripts/utils.js | 3 +- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 4b26fd2aa0..8c4b45325f 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "@babel/core": "^7.0.0", "@babel/eslint-parser": "^7.13.8", "@babel/register": "^7.0.0", + "@vates/async-each": "1.0.0", "babel-jest": "^29.0.3", "benchmark": "^2.1.4", "deptree": "^1.0.0", @@ -16,6 +17,7 @@ "eslint-plugin-promise": "^6.0.0", "eslint-plugin-react": "^7.21.5", "exec-promise": "^0.7.0", + "getopts": "^2.3.0", "globby": "^13.1.1", "handlebars": "^4.7.6", "husky": "^4.2.5", @@ -77,7 +79,7 @@ }, "private": true, "scripts": { - "build": "scripts/run-script.js --parallel build", + "build": "scripts/run-script.js --parallel --concurrency 2 build", "ci": "yarn && yarn build && yarn test-integration", "clean": "scripts/run-script.js --parallel clean", "dev": "scripts/run-script.js --parallel dev", diff --git a/scripts/run-script.js b/scripts/run-script.js index d765a51528..a0fe462bba 100755 --- a/scripts/run-script.js +++ b/scripts/run-script.js @@ -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 diff --git a/scripts/utils.js b/scripts/utils.js index f051d310ba..d9aff1c8ca 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -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 }