From 3016b811b8bc540952c4f64c5ace6442ac607cc5 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 3 Dec 2020 13:22:28 -0500 Subject: [PATCH] build: parallelize multi package builds --- scripts/build.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/scripts/build.js b/scripts/build.js index f3a66051..26e287c4 100644 --- a/scripts/build.js +++ b/scripts/build.js @@ -50,9 +50,25 @@ async function run() { } async function buildAll(targets) { - for (const target of targets) { - await build(target) + await runParallel(require('os').cpus().length, targets, build) +} + +async function runParallel(maxConcurrency, source, iteratorFn) { + const ret = [] + const executing = [] + for (const item of source) { + const p = Promise.resolve().then(() => iteratorFn(item, source)) + ret.push(p) + + if (maxConcurrency <= source.length) { + const e = p.then(() => executing.splice(executing.indexOf(e), 1)) + executing.push(e) + if (executing.length >= maxConcurrency) { + await Promise.race(executing) + } + } } + return Promise.all(ret) } async function build(target) {