import {join} from 'node:path'; import {runCommands, type Command} from 'runstorm'; import {findLongestCommonPath} from '../../augments/path.js'; import {MonoCliInputError} from '../../cli/mono-cli-input.error.js'; import {type CommandInputs, type CommandOutput} from '../command.js'; import {getRelativePosixPackagePathsInDependencyOrder} from '../workspace-packages/get-package-dependency-order.js'; import {runForEachCommand} from './for-each.command.js'; /** * Run the command for each package in parallel. * * @category Internal */ export async function runForEachAsyncCommand({ cwd, commandInputs, maxConcurrency, exclude, }: Readonly): Promise { if (maxConcurrency === 1) { return await runForEachCommand({cwd, commandInputs, exclude}); } const relativePackagePathsInOrder = await getRelativePosixPackagePathsInDependencyOrder( cwd, exclude, ); const shellCommand = commandInputs.join(' '); if (!shellCommand) { throw new MonoCliInputError(`No inputs were given to the for-each-async command.`); } const commonPath = findLongestCommonPath(relativePackagePathsInOrder); const commands: Exclude, string>[] = relativePackagePathsInOrder.map( (relativePackagePath) => { return { command: shellCommand, cwd: join(cwd, relativePackagePath), name: relativePackagePath.replace(commonPath, '').replace(/^\//, ''), }; }, ); return await runCommands(commands, { maxConcurrency, }); }