From 44af21c88536af3b64ec69227d6affa625e36ed2 Mon Sep 17 00:00:00 2001 From: Danny Lin Date: Sat, 18 Dec 2021 18:25:54 -0800 Subject: [PATCH] frontend: Add parallel for-each-device helper with CLI output --- src/frontend/devices.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/frontend/devices.ts diff --git a/src/frontend/devices.ts b/src/frontend/devices.ts new file mode 100644 index 0000000..25d6553 --- /dev/null +++ b/src/frontend/devices.ts @@ -0,0 +1,28 @@ +import chalk from "chalk" + +export async function forEachDevice( + devices: Device[], + parallel: boolean, + callback: (device: Device) => Promise, + deviceKey: (device: Device) => string = d => d as any as string, +) { + let jobs = [] + let isMultiDevice = devices.length > 1 + for (let device of devices) { + if (isMultiDevice) { + console.log(` + +${chalk.bold(chalk.blueBright(deviceKey(device)))} +`) + } + + let job = callback(device) + if (parallel) { + jobs.push(job) + } else { + await job + } + } + + await Promise.all(jobs) +}