Files
shakespeare/src/lib/commands/utils.ts
T
Alex Gleason 376a982449 Overhaul shell commands for POSIX compliance and AI-agent ergonomics
Each built-in shell command has been audited and rewritten to support the
options AI agents (and humans) typically reach for, close critical behavior
gaps, and align with POSIX/GNU conventions:

- Shared argument parser (utils.ts) handles combined short flags, long
  options with '=VALUE' or space-separated values, the '--' sentinel,
  numeric shortcuts like 'head -5', and '-' for stdin.
- cat, ls, cp, mv, rm, mkdir, touch: added missing common flags; removed
  the legacy 'absolute paths are not supported' and 'reading from stdin
  is not supported' barriers where they made agent use awkward.
- mv now overwrites destinations by default (POSIX) and supports -n.
- touch updates mtime on existing files (by rewriting content) and
  accepts -c/-a/-m/-h/-r/-d/-t options.
- find: new expression engine with -iname, -path, -regex, -type,
  -empty, -size, -maxdepth, -mindepth, -not, -or, -print/-print0,
  -delete, -quit.
- grep: -v, -c, -l, -L, -w, -x, -o, -q, -E, -F, -G, -H, -h, -A/-B/-C
  context, --include/--exclude, multiple -e, -f scriptfile.
- head/tail: -c bytes, -NUM shorthand, negative/positive line counts,
  proper multi-file headers.
- sort: -k keys, -t separator, -o output, -f/-b/-d/-s, -h/-V/-M modes.
- uniq: -i, -f, -s, -w, -D, and the [input [output]] form.
- wc: -m characters and -L longest line; -c now counts bytes (UTF-8).
- cut: open-ended ranges (3-, -5), -s, --complement, --output-delimiter.
- tr: -c/-s/-t, [:class:] character classes, escape sequences.
- sed: real multi-command parser supporting ';', addresses (N, $,
  /re/, N,M), '!' negation, proper -n quiet mode, y///, and -i[SUFFIX].
- echo: -n, -e, -E with standard backslash escapes.
- pwd/cd: -L/-P, plus cd -, cd ~, cd $HOME with OLDPWD tracking.
- diff: proper LCS algorithm producing correct normal and unified
  output; adds -q, -s, -N, -i, -w, -b, -B.
- env: -i, -u, NAME=VAL assignments; reads the live shell environment.
- date: -u, -d, -R, -I, and a full GNU-subset format specifier set.

Tests for each command were updated to reflect the corrected behavior
(e.g. absolute paths and stdin are now accepted, POSIX error messages).
All 1334 tests pass; typecheck and production build succeed.
2026-04-26 23:58:13 -05:00

269 lines
7.8 KiB
TypeScript

import { join } from "path-browserify";
import { isAbsolutePath } from "../security";
/**
* Resolve a path argument to an absolute path.
* - Absolute paths are returned as-is.
* - Relative paths are resolved against `cwd`.
*/
export function resolvePath(path: string, cwd: string): string {
if (isAbsolutePath(path)) return path;
return join(cwd, path);
}
/**
* Classify a filesystem error message as ENOENT, EACCES, EISDIR, etc.
*/
export function classifyFsError(err: unknown): { kind: 'ENOENT' | 'EACCES' | 'EISDIR' | 'ENOTDIR' | 'EEXIST' | 'OTHER'; message: string } {
const message = err instanceof Error ? err.message : String(err);
if (/ENOENT|no such file/i.test(message)) return { kind: 'ENOENT', message };
if (/EACCES|permission/i.test(message)) return { kind: 'EACCES', message };
if (/EISDIR|is a directory/i.test(message)) return { kind: 'EISDIR', message };
if (/ENOTDIR|not a directory/i.test(message)) return { kind: 'ENOTDIR', message };
if (/EEXIST|file exists/i.test(message)) return { kind: 'EEXIST', message };
return { kind: 'OTHER', message };
}
/**
* Result from parseOptions: parsed boolean flags, value-bearing options, positional operands.
*/
export interface ParsedOptions {
/** Set of single-char boolean flags that were seen (e.g. 'l', 'a'). */
flags: Set<string>;
/** Long options (e.g. 'recursive') whose presence was seen. */
longFlags: Set<string>;
/** Values for options that take an argument (keyed by short char or long name). */
values: Map<string, string>;
/** Positional (non-option) arguments, in order. */
operands: string[];
/** Any unknown options encountered (for "invalid option" error reporting). */
unknown: string[];
}
export interface OptionSpec {
/** Single-char flag → no argument. */
booleanShort?: string[];
/** Single-char options that require a value (e.g. 'n' for -n 10). */
valueShort?: string[];
/** Long flags with no argument (e.g. 'recursive' for --recursive). */
booleanLong?: string[];
/** Long flags with a value (e.g. 'include' for --include=pat or --include pat). */
valueLong?: string[];
/**
* If true, all remaining arguments after the first positional are treated
* as operands. Used by commands like `echo` which don't parse interior flags.
*/
stopAtFirstOperand?: boolean;
/**
* Map of short flag → canonical short flag. Used for aliasing (e.g. -R → -r).
*/
shortAliases?: Record<string, string>;
/**
* Map of long flag → canonical short flag (e.g. --recursive → 'r').
*/
longToShort?: Record<string, string>;
}
/**
* POSIX-ish option parser.
*
* Supports:
* - Combined short flags: `-abc` === `-a -b -c`
* - Short option with value: `-n 10` or `-n10`
* - Long options: `--lines=10` or `--lines 10`
* - The `--` sentinel ends option parsing; everything after is an operand
* - `-` alone is an operand (commonly meaning stdin)
* - Numeric shortcut `-NUM` (e.g. `head -5`) is treated as a value of
* option 'n' when 'n' is in valueShort AND enableNumericShortcut is true
*/
export function parseOptions(
args: string[],
spec: OptionSpec,
opts: { enableNumericShortcut?: boolean } = {}
): ParsedOptions {
const result: ParsedOptions = {
flags: new Set(),
longFlags: new Set(),
values: new Map(),
operands: [],
unknown: [],
};
const boolShort = new Set(spec.booleanShort ?? []);
const valShort = new Set(spec.valueShort ?? []);
const boolLong = new Set(spec.booleanLong ?? []);
const valLong = new Set(spec.valueLong ?? []);
const shortAliases = spec.shortAliases ?? {};
const longToShort = spec.longToShort ?? {};
const normalizeShort = (c: string): string => shortAliases[c] ?? c;
let i = 0;
let stopped = false;
while (i < args.length) {
const arg = args[i];
if (stopped) {
result.operands.push(arg);
i++;
continue;
}
// `--` sentinel
if (arg === '--') {
stopped = true;
i++;
continue;
}
// Lone `-` is an operand (stdin)
if (arg === '-') {
result.operands.push(arg);
i++;
if (spec.stopAtFirstOperand) stopped = true;
continue;
}
// Long option `--name` or `--name=value`
if (arg.startsWith('--')) {
const eq = arg.indexOf('=');
const name = eq === -1 ? arg.slice(2) : arg.slice(2, eq);
const inline = eq === -1 ? undefined : arg.slice(eq + 1);
// Map long → short when specified
if (name in longToShort) {
const shortName = longToShort[name];
if (valShort.has(shortName)) {
let v: string | undefined = inline;
if (v === undefined) {
i++;
if (i >= args.length) {
result.unknown.push(arg);
continue;
}
v = args[i];
}
result.values.set(shortName, v);
} else {
result.flags.add(shortName);
}
i++;
continue;
}
if (valLong.has(name)) {
let v: string | undefined = inline;
if (v === undefined) {
i++;
if (i >= args.length) {
result.unknown.push(arg);
continue;
}
v = args[i];
}
result.values.set(name, v);
i++;
continue;
}
if (boolLong.has(name)) {
result.longFlags.add(name);
i++;
continue;
}
result.unknown.push(arg);
i++;
continue;
}
// Short options `-abc` or `-n10` or `-n 10`
if (arg.startsWith('-') && arg.length > 1) {
// Numeric shortcut -NUM (e.g. head -5 means -n 5)
if (
opts.enableNumericShortcut &&
/^-\d+$/.test(arg) &&
valShort.has('n')
) {
result.values.set('n', arg.slice(1));
i++;
continue;
}
let j = 1;
let consumedNext = false;
while (j < arg.length) {
const c = arg[j];
const canonical = normalizeShort(c);
if (valShort.has(canonical)) {
// Everything after this char in the same arg is the value.
const rest = arg.slice(j + 1);
if (rest.length > 0) {
result.values.set(canonical, rest);
} else {
// Value is the next argument.
i++;
consumedNext = true;
if (i >= args.length) {
result.unknown.push(`-${c}`);
break;
}
result.values.set(canonical, args[i]);
}
break;
}
if (boolShort.has(canonical)) {
result.flags.add(canonical);
j++;
continue;
}
result.unknown.push(`-${c}`);
j++;
}
i++;
if (consumedNext) continue;
continue;
}
// Positional operand
result.operands.push(arg);
i++;
if (spec.stopAtFirstOperand) stopped = true;
}
return result;
}
/**
* Split input text into lines, preserving knowledge of whether the
* final line had a trailing newline. Useful for line-oriented commands
* that need to reconstruct output faithfully.
*/
export function splitLines(content: string): { lines: string[]; trailingNewline: boolean } {
if (content === '') return { lines: [], trailingNewline: false };
const trailingNewline = content.endsWith('\n');
const body = trailingNewline ? content.slice(0, -1) : content;
return { lines: body.split('\n'), trailingNewline };
}
/**
* Join lines back into content, re-adding the trailing newline if one
* was present in the original input.
*/
export function joinLines(lines: string[], trailingNewline: boolean): string {
if (lines.length === 0) return '';
return lines.join('\n') + (trailingNewline ? '\n' : '');
}
/**
* Format an "invalid option" error line.
*/
export function invalidOptionError(cmd: string, opt: string): string {
return `${cmd}: invalid option -- '${opt.replace(/^-+/, '')}'`;
}