| |
| |
| |
| |
| |
| |
|
|
| import { splitCommand_DEPRECATED } from '../../utils/bash/commands.js' |
|
|
| export type CommandSemantic = ( |
| exitCode: number, |
| stdout: string, |
| stderr: string, |
| ) => { |
| isError: boolean |
| message?: string |
| } |
|
|
| |
| |
| |
| const DEFAULT_SEMANTIC: CommandSemantic = (exitCode, _stdout, _stderr) => ({ |
| isError: exitCode !== 0, |
| message: |
| exitCode !== 0 ? `Command failed with exit code ${exitCode}` : undefined, |
| }) |
|
|
| |
| |
| |
| const COMMAND_SEMANTICS: Map<string, CommandSemantic> = new Map([ |
| |
| [ |
| 'grep', |
| (exitCode, _stdout, _stderr) => ({ |
| isError: exitCode >= 2, |
| message: exitCode === 1 ? 'No matches found' : undefined, |
| }), |
| ], |
|
|
| |
| [ |
| 'rg', |
| (exitCode, _stdout, _stderr) => ({ |
| isError: exitCode >= 2, |
| message: exitCode === 1 ? 'No matches found' : undefined, |
| }), |
| ], |
|
|
| |
| [ |
| 'find', |
| (exitCode, _stdout, _stderr) => ({ |
| isError: exitCode >= 2, |
| message: |
| exitCode === 1 ? 'Some directories were inaccessible' : undefined, |
| }), |
| ], |
|
|
| |
| [ |
| 'diff', |
| (exitCode, _stdout, _stderr) => ({ |
| isError: exitCode >= 2, |
| message: exitCode === 1 ? 'Files differ' : undefined, |
| }), |
| ], |
|
|
| |
| [ |
| 'test', |
| (exitCode, _stdout, _stderr) => ({ |
| isError: exitCode >= 2, |
| message: exitCode === 1 ? 'Condition is false' : undefined, |
| }), |
| ], |
|
|
| |
| [ |
| '[', |
| (exitCode, _stdout, _stderr) => ({ |
| isError: exitCode >= 2, |
| message: exitCode === 1 ? 'Condition is false' : undefined, |
| }), |
| ], |
|
|
| |
| |
| ]) |
|
|
| |
| |
| |
| function getCommandSemantic(command: string): CommandSemantic { |
| |
| const baseCommand = heuristicallyExtractBaseCommand(command) |
| const semantic = COMMAND_SEMANTICS.get(baseCommand) |
| return semantic !== undefined ? semantic : DEFAULT_SEMANTIC |
| } |
|
|
| |
| |
| |
| function extractBaseCommand(command: string): string { |
| return command.trim().split(/\s+/)[0] || '' |
| } |
|
|
| |
| |
| |
| |
| function heuristicallyExtractBaseCommand(command: string): string { |
| const segments = splitCommand_DEPRECATED(command) |
|
|
| |
| const lastCommand = segments[segments.length - 1] || command |
|
|
| return extractBaseCommand(lastCommand) |
| } |
|
|
| |
| |
| |
| export function interpretCommandResult( |
| command: string, |
| exitCode: number, |
| stdout: string, |
| stderr: string, |
| ): { |
| isError: boolean |
| message?: string |
| } { |
| const semantic = getCommandSemantic(command) |
| const result = semantic(exitCode, stdout, stderr) |
|
|
| return { |
| isError: result.isError, |
| message: result.message, |
| } |
| } |
|
|