From d86c722d8de66560c672c2fbdb338cc9a024caa2 Mon Sep 17 00:00:00 2001 From: Shane McLaughlin Date: Thu, 7 Jul 2022 10:07:31 -0500 Subject: [PATCH] feat: build/pack in parallel (#914) --- src/commands/pack/tarballs.ts | 2 ++ src/tarballs/build.ts | 22 ++++++++++++++++++---- src/tarballs/node.ts | 7 ++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/commands/pack/tarballs.ts b/src/commands/pack/tarballs.ts index 1f9151f46..a8374ae8d 100644 --- a/src/commands/pack/tarballs.ts +++ b/src/commands/pack/tarballs.ts @@ -13,6 +13,7 @@ This can be used to create oclif CLIs that use the system node or that come prel root: Flags.string({char: 'r', description: 'path to oclif CLI root', default: '.', required: true}), targets: Flags.string({char: 't', description: 'comma-separated targets to pack (e.g.: linux-arm,win32-x64)'}), xz: Flags.boolean({description: 'also build xz', allowNo: true}), + parallel: Flags.boolean({description: 'build tarballs in parallel'}), tarball: Flags.string({char: 'l', description: 'optionally specify a path to a tarball already generated by NPM', required: false}), } @@ -27,6 +28,7 @@ This can be used to create oclif CLIs that use the system node or that come prel await Tarballs.build(buildConfig, { tarball: flags.tarball, + parallel: flags.parallel, }) qq.cd(prevCwd) } diff --git a/src/tarballs/build.ts b/src/tarballs/build.ts index 1a07f58ac..a86541479 100644 --- a/src/tarballs/build.ts +++ b/src/tarballs/build.ts @@ -25,6 +25,7 @@ export async function build(c: BuildConfig, options: { platform?: string; pack?: boolean; tarball?: string; + parallel?: boolean; } = {}): Promise { const {xz, config} = c const prevCwd = qq.cwd() @@ -115,8 +116,16 @@ export async function build(c: BuildConfig, options: { tmp: qq.join(config.root, 'tmp'), }) if (options.pack === false) return - await pack(workspace, c.dist(gzLocalKey)) - if (xz) await pack(workspace, c.dist(xzLocalKey)) + if (options.parallel) { + await Promise.all( + [pack(workspace, c.dist(gzLocalKey))] + .concat(xz ? [pack(workspace, c.dist(xzLocalKey))] : []), + ) + } else { + await pack(workspace, c.dist(gzLocalKey)) + if (xz) await pack(workspace, c.dist(xzLocalKey)) + } + if (!c.updateConfig.s3.host) return const rollout = (typeof c.updateConfig.autoupdate === 'object' && c.updateConfig.autoupdate.rollout) @@ -153,8 +162,13 @@ export async function build(c: BuildConfig, options: { await addDependencies() await writeBinScripts({config, baseWorkspace: c.workspace(), nodeVersion: c.nodeVersion}) await pretarball() - for (const target of c.targets) { - if (!options.platform || options.platform === target.platform) { + const targetsToBuild = c.targets.filter(t => !options.platform || options.platform === t.platform) + if (options.parallel) { + log(`will build ${targetsToBuild.length} targets in parallel`) + await Promise.all(targetsToBuild.map(t => buildTarget(t))) + } else { + log(`will build ${targetsToBuild.length} targets sequentially`) + for (const target of targetsToBuild) { // eslint-disable-next-line no-await-in-loop await buildTarget(target) } diff --git a/src/tarballs/node.ts b/src/tarballs/node.ts index 83b8e0016..be4b577cd 100644 --- a/src/tarballs/node.ts +++ b/src/tarballs/node.ts @@ -53,14 +53,11 @@ export async function fetchNodeBinary({nodeVersion, output, platform, arch, tmp} const extract = async () => { log(`extracting ${nodeBase}`) const nodeTmp = path.join(tmp, 'node') - await qq.rm([nodeTmp, nodeBase]) await qq.mkdirp(nodeTmp) await qq.mkdirp(path.dirname(cache)) if (platform === 'win32') { - qq.pushd(nodeTmp) - await qq.x(`7z x -bd -y "${tarball}"`) - await qq.mv([nodeBase, 'node.exe'], cache) - qq.popd() + await qq.x(`7z x -bd -y "${tarball}"`, {cwd: nodeTmp}) + await qq.mv([nodeTmp, nodeBase, 'node.exe'], cache) } else { await qq.x(`tar -C "${tmp}/node" -xJf "${tarball}"`) await qq.mv([nodeTmp, nodeBase, 'bin/node'], cache)