mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
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.
142 lines
4.7 KiB
TypeScript
142 lines
4.7 KiB
TypeScript
import { join, basename } from "path-browserify";
|
|
import type { JSRuntimeFS } from "../JSRuntime";
|
|
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
|
|
import { createSuccessResult, createErrorResult } from "./ShellCommand";
|
|
import { validateWritePath } from "../security";
|
|
import { classifyFsError, parseOptions, resolvePath } from "./utils";
|
|
|
|
/**
|
|
* Implementation of the 'rm' command.
|
|
*
|
|
* Supported options:
|
|
* -r, -R, --recursive Remove directories and their contents recursively
|
|
* -f, --force Ignore nonexistent files and suppress prompts
|
|
* -d, --dir Remove empty directories
|
|
* -v, --verbose Explain what is being done
|
|
* -i Prompt before every removal (no-op in non-interactive)
|
|
* -- End of options
|
|
*/
|
|
export class RmCommand implements ShellCommand {
|
|
name = 'rm';
|
|
description = 'Remove files and directories';
|
|
usage = 'rm [-rRfdvi] [--] file...';
|
|
|
|
private fs: JSRuntimeFS;
|
|
|
|
constructor(fs: JSRuntimeFS) {
|
|
this.fs = fs;
|
|
}
|
|
|
|
async execute(args: string[], cwd: string, _input?: string): Promise<ShellCommandResult> {
|
|
const parsed = parseOptions(args, {
|
|
booleanShort: ['r', 'R', 'f', 'd', 'v', 'i'],
|
|
booleanLong: ['recursive', 'force', 'dir', 'verbose'],
|
|
longToShort: {
|
|
recursive: 'r',
|
|
force: 'f',
|
|
dir: 'd',
|
|
verbose: 'v',
|
|
},
|
|
shortAliases: { R: 'r' },
|
|
});
|
|
|
|
if (parsed.unknown.length > 0) {
|
|
return createErrorResult(`${this.name}: invalid option -- '${parsed.unknown[0].replace(/^-+/, '')}'`);
|
|
}
|
|
|
|
const opts = {
|
|
recursive: parsed.flags.has('r'),
|
|
force: parsed.flags.has('f'),
|
|
allowEmptyDir: parsed.flags.has('d'),
|
|
verbose: parsed.flags.has('v'),
|
|
};
|
|
|
|
const paths = parsed.operands;
|
|
if (paths.length === 0) {
|
|
if (opts.force) return createSuccessResult('');
|
|
return createErrorResult(`${this.name}: missing operand\nUsage: ${this.usage}`);
|
|
}
|
|
|
|
const verboseOut: string[] = [];
|
|
const errors: string[] = [];
|
|
|
|
for (const path of paths) {
|
|
// Refuse to remove . or ..
|
|
const b = basename(path);
|
|
if (b === '.' || b === '..') {
|
|
if (!opts.force) {
|
|
errors.push(`${this.name}: refusing to remove '.' or '..' directory: skipping '${path}'`);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
validateWritePath(path, this.name, cwd);
|
|
} catch (error) {
|
|
if (!opts.force) {
|
|
errors.push(error instanceof Error ? error.message : 'Unknown error');
|
|
}
|
|
continue;
|
|
}
|
|
|
|
const absolutePath = resolvePath(path, cwd);
|
|
|
|
try {
|
|
const stats = await this.fs.stat(absolutePath);
|
|
|
|
if (stats.isDirectory()) {
|
|
if (opts.recursive) {
|
|
await this.removeDirectoryRecursive(absolutePath, verboseOut, opts.verbose);
|
|
if (opts.verbose) verboseOut.push(`removed directory '${path}'`);
|
|
} else if (opts.allowEmptyDir) {
|
|
await this.fs.rmdir(absolutePath);
|
|
if (opts.verbose) verboseOut.push(`removed directory '${path}'`);
|
|
} else {
|
|
if (!opts.force) {
|
|
errors.push(`${this.name}: cannot remove '${path}': Is a directory`);
|
|
}
|
|
}
|
|
} else {
|
|
await this.fs.unlink(absolutePath);
|
|
if (opts.verbose) verboseOut.push(`removed '${path}'`);
|
|
}
|
|
} catch (error) {
|
|
if (opts.force) continue;
|
|
const { kind, message } = classifyFsError(error);
|
|
if (kind === 'ENOENT') {
|
|
errors.push(`${this.name}: cannot remove '${path}': No such file or directory`);
|
|
} else if (kind === 'EACCES') {
|
|
errors.push(`${this.name}: cannot remove '${path}': Permission denied`);
|
|
} else {
|
|
errors.push(`${this.name}: cannot remove '${path}': ${message}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (errors.length > 0) {
|
|
const stdout = verboseOut.length > 0 ? verboseOut.join('\n') + '\n' : '';
|
|
return {
|
|
exitCode: 1,
|
|
stdout,
|
|
stderr: errors.join('\n'),
|
|
};
|
|
}
|
|
|
|
return createSuccessResult(verboseOut.length > 0 ? verboseOut.join('\n') + '\n' : '');
|
|
}
|
|
|
|
private async removeDirectoryRecursive(dirPath: string, verboseOut: string[], verbose: boolean): Promise<void> {
|
|
const entries = await this.fs.readdir(dirPath, { withFileTypes: true });
|
|
for (const entry of entries) {
|
|
const entryPath = join(dirPath, entry.name);
|
|
if (entry.isDirectory()) {
|
|
await this.removeDirectoryRecursive(entryPath, verboseOut, verbose);
|
|
} else {
|
|
await this.fs.unlink(entryPath);
|
|
if (verbose) verboseOut.push(`removed '${entryPath}'`);
|
|
}
|
|
}
|
|
await this.fs.rmdir(dirPath);
|
|
}
|
|
}
|