frontend: Add parallel for-each-device helper with CLI output

This commit is contained in:
Danny Lin 2021-12-18 18:25:54 -08:00
parent 05a7ee26da
commit 44af21c885

28
src/frontend/devices.ts Normal file
View file

@ -0,0 +1,28 @@
import chalk from "chalk"
export async function forEachDevice<Device>(
devices: Device[],
parallel: boolean,
callback: (device: Device) => Promise<void>,
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)
}