|
| 1 | +import { error } from '@actions/core'; |
| 2 | +import { bold, red } from '@std/fmt/colors'; |
| 3 | + |
| 4 | +export interface CommandParams { |
| 5 | + name: string; |
| 6 | + args: ConstructorParameters<typeof Deno.Command>; |
| 7 | +} |
| 8 | + |
| 9 | +export async function runCommands( |
| 10 | + commandParams: Iterable<CommandParams, void, undefined>, |
| 11 | +): Promise<void> { |
| 12 | + const processes = Array.from(commandParams, ({ name, args }) => { |
| 13 | + return { name, process: new Deno.Command(...args).spawn() }; |
| 14 | + }); |
| 15 | + |
| 16 | + for (const [idx, { name, process }] of processes.entries()) { |
| 17 | + await Promise.all([ |
| 18 | + process.stderr.pipeTo(Deno.stderr.writable, { preventClose: true, preventAbort: true }), |
| 19 | + process.stdout.pipeTo(Deno.stdout.writable, { preventClose: true, preventAbort: true }), |
| 20 | + ]); |
| 21 | + |
| 22 | + if (!(await process.status).success) { |
| 23 | + console.log('\n'); |
| 24 | + |
| 25 | + annotateError({ title: 'CI', message: `${name} failed!` }); |
| 26 | + } |
| 27 | + |
| 28 | + if (idx !== processes.length - 1) { |
| 29 | + console.log(`\n${bold('-'.repeat(Deno.consoleSize().columns))}\n\n`); |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export function isCI() { |
| 35 | + return Deno.env.get('CI') === 'true'; |
| 36 | +} |
| 37 | + |
| 38 | +export function permissionArgs(permissionSetName?: string): string[] { |
| 39 | + return [permissionSetName ? `-P=${permissionSetName}` : '-P', '--no-prompt']; |
| 40 | +} |
| 41 | + |
| 42 | +interface Annotation { |
| 43 | + title: string; |
| 44 | + message: string; |
| 45 | +} |
| 46 | + |
| 47 | +function annotateError(annotation: Annotation): void { |
| 48 | + const { title, message } = annotation; |
| 49 | + |
| 50 | + if (isCI()) { |
| 51 | + error(red(message), { title }); |
| 52 | + } else { |
| 53 | + console.error(`${bold(`${title}:`)} ${red(message)}`); |
| 54 | + } |
| 55 | +} |
0 commit comments