Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199#!/usr/bin/env node
/** Command executor wrapper for mise tasks. This script works around task runner limitations where tasks with `allowFailure: true` can't be dependencies. By wrapping commands in this executor, we can control exit codes while still showing all output and errors to the user. Usage: task-command --allowFailure; oxlint # Always exits with 0 task-command; oxlint # Exits with command's exit code The `--` separator is required to distinguish script args from command args. @example ```bash # Always exit with 0 task-command --allowFailure; oxlint # Exit with command's exit code task-command; oxlint # Execute through shell task-command --shell; "echo hello && echo world" # Execute with timeout of 5 seconds task-command --timeout 5000; npm test ``` */
// TODO: deprecate Optiqueimport { object, } from '@optique/core/constructs';// TODO: deprecate Optiqueimport { multiple, optional,} from '@optique/core/modifiers';// TODO: deprecate Optiqueimport { argument, option,} from '@optique/core/primitives';// TODO: deprecate Optiqueimport { integer, string,} from '@optique/core/valueparser';// TODO: deprecate Optiqueimport { runSync, } from '@optique/run';import spawn from 'nano-spawn';import dedent from 'string-dedent';import { match, } from 'ts-pattern';import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';
//region Parser definition: defines CLI flags and rest arguments after --
/** TODO: deprecate Optique Optique parser for the task-command CLI */const parser = object({ allowFailure: option( '-a', '--allowFailure', ), shell: option( '-s', '--shell', ), timeout: optional(option( '-t', '--timeout', integer(), ),), rest: multiple( argument(string(),), ),},);
//endregion Parser definition
/** TODO: deprecate Optique Parsed CLI arguments from process.argv */const args = runSync( parser, { programName: 'task-command', help: 'option', },);
/** Destructured command and its arguments from the rest args after `--` */const [command, ...commandArgs] = args.rest;
if ((command === undefined) || (command === '')) { throw new Error( dedent` No command specified after -- ${ JSON.stringify( args, null, 2, ) } `, );}
try { // Execute the command with nano-spawn await spawn( command, commandArgs, { stdout: 'inherit', stderr: 'inherit', stdin: 'inherit', shell: args.shell, timeout: ((typeof args.timeout) === 'number') ? args.timeout : undefined, }, );
// Script ends naturally with exit code 0}catch (error) { // nano-spawn throws SubprocessError when the process fails match(error,) .when( function isSubprocessError( candidate, ): candidate is { readonly exitCode?: number; readonly signalName?: string; readonly message: string; } { return (candidate !== null) && ((typeof candidate) === 'object') && ('exitCode' in candidate); }, function handleSubprocessError(subprocessError,): void { match(subprocessError.signalName,) .when( function hasSignal(signal,): signal is string { return signal !== undefined; }, function logSignal(signal,): void { console.error(`Command terminated by signal: ${signal}`,); }, );
// Exit with 0 if allowFailure is true, otherwise use the command's exit code match(args.allowFailure,) .with( false, function exitWithCode(): void { process.exitCode = subprocessError.exitCode ?? 1; }, ) .with( true, function allowFailureNoop(): void { // Let script end naturally with exit code 0 }, ); }, ) .otherwise(function handleUnknownError(): void { console.error( `Failed to execute command: ${ caughtValueText(error,) }`, ); match(args.allowFailure,) .with( false, function rethrowError(): void { throw error; }, ) .with( true, function allowFailureNoop(): void { // Let script end naturally with exit code 0 }, ); },);}