mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +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.
127 lines
4.0 KiB
TypeScript
127 lines
4.0 KiB
TypeScript
import { join, resolve, dirname } from "path-browserify";
|
|
import type { JSRuntimeFS } from "../JSRuntime";
|
|
import type { ShellCommand, ShellCommandResult } from "./ShellCommand";
|
|
import { createSuccessResult, createErrorResult } from "./ShellCommand";
|
|
import { isAbsolutePath } from "../security";
|
|
import { parseOptions } from "./utils";
|
|
|
|
/**
|
|
* Implementation of the 'cd' command.
|
|
*
|
|
* Supported options:
|
|
* -L Follow symbolic links (default; no-op in our VFS)
|
|
* -P Use the physical directory, without symlinks (no-op in our VFS)
|
|
*
|
|
* Supported forms:
|
|
* cd → $HOME (defaults to '/' when unset)
|
|
* cd - → previous directory (OLDPWD)
|
|
* cd ~ → $HOME
|
|
* cd ~/foo → $HOME/foo
|
|
* cd .. → parent
|
|
* cd /abs/path → absolute
|
|
* cd rel/path → relative to CWD
|
|
*/
|
|
export class CdCommand implements ShellCommand {
|
|
name = 'cd';
|
|
description = 'Change directory';
|
|
usage = 'cd [-LP] [directory | -]';
|
|
|
|
private fs: JSRuntimeFS;
|
|
/** Tracked previous working directory for `cd -`. */
|
|
private oldPwd: string | undefined;
|
|
/** Default home dir when HOME env var is not available. */
|
|
private homeDir = '/';
|
|
|
|
constructor(fs: JSRuntimeFS) {
|
|
this.fs = fs;
|
|
}
|
|
|
|
/** Optional hook used by the executor to supply HOME. */
|
|
setHomeDir(home: string): void {
|
|
this.homeDir = home;
|
|
}
|
|
|
|
async execute(args: string[], cwd: string, _input?: string): Promise<ShellCommandResult> {
|
|
const parsed = parseOptions(args, {
|
|
booleanShort: ['L', 'P'],
|
|
});
|
|
|
|
if (parsed.unknown.length > 0) {
|
|
return createErrorResult(`${this.name}: invalid option -- '${parsed.unknown[0].replace(/^-+/, '')}'`);
|
|
}
|
|
|
|
if (parsed.operands.length > 1) {
|
|
return createErrorResult(`${this.name}: too many arguments`);
|
|
}
|
|
|
|
let targetPath: string;
|
|
|
|
if (parsed.operands.length === 0) {
|
|
// cd with no args → $HOME
|
|
targetPath = this.homeDir;
|
|
} else {
|
|
targetPath = parsed.operands[0];
|
|
}
|
|
|
|
// Handle `cd -`
|
|
if (targetPath === '-') {
|
|
if (!this.oldPwd) {
|
|
return createErrorResult(`${this.name}: OLDPWD not set`);
|
|
}
|
|
const previous = this.oldPwd;
|
|
// POSIX: cd - prints the new directory and then chdir's to it.
|
|
const check = await this.checkDir(previous);
|
|
if (check) return check;
|
|
this.oldPwd = cwd;
|
|
return createSuccessResult(previous + '\n', previous);
|
|
}
|
|
|
|
// Tilde expansion (tokenizer normally handles this, but handle leftovers).
|
|
if (targetPath === '~' || targetPath.startsWith('~/')) {
|
|
targetPath = this.homeDir + targetPath.slice(1);
|
|
}
|
|
|
|
let newCwd: string;
|
|
if (isAbsolutePath(targetPath)) {
|
|
newCwd = targetPath;
|
|
} else if (targetPath === '.') {
|
|
newCwd = cwd;
|
|
} else if (targetPath === '..') {
|
|
const parentPath = dirname(cwd);
|
|
newCwd = parentPath || '/';
|
|
} else {
|
|
newCwd = join(cwd, targetPath);
|
|
}
|
|
|
|
newCwd = resolve(newCwd);
|
|
|
|
const err = await this.checkDir(newCwd, targetPath);
|
|
if (err) return err;
|
|
|
|
this.oldPwd = cwd;
|
|
return createSuccessResult('', newCwd);
|
|
}
|
|
|
|
private async checkDir(path: string, display?: string): Promise<ShellCommandResult | null> {
|
|
const label = display ?? path;
|
|
try {
|
|
const stats = await this.fs.stat(path);
|
|
if (!stats.isDirectory()) {
|
|
return createErrorResult(`${this.name}: ${label}: Not a directory`);
|
|
}
|
|
return null;
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
if (error.message.includes('ENOENT') || error.message.includes('not found')) {
|
|
return createErrorResult(`${this.name}: ${label}: No such file or directory`);
|
|
}
|
|
if (error.message.includes('EACCES') || error.message.includes('permission')) {
|
|
return createErrorResult(`${this.name}: ${label}: Permission denied`);
|
|
}
|
|
return createErrorResult(`${this.name}: ${label}: ${error.message}`);
|
|
}
|
|
return createErrorResult(`${this.name}: ${label}: Unknown error`);
|
|
}
|
|
}
|
|
}
|