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.
195 lines
6.9 KiB
TypeScript
195 lines
6.9 KiB
TypeScript
import { join, dirname, 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 'cp' command.
|
|
*
|
|
* Supported options:
|
|
* -r, -R, --recursive Copy directories recursively
|
|
* -f, --force Force overwrite (remove destination first if needed)
|
|
* -i, --interactive Prompt before overwrite (no-op: always treated as 'yes' in non-interactive env)
|
|
* -n, --no-clobber Do not overwrite an existing file
|
|
* -p, --preserve Preserve timestamps (best-effort; VFS lacks chmod)
|
|
* -a, --archive Equivalent to -rp
|
|
* -v, --verbose Explain what is being done
|
|
* -T, --no-target-directory Treat destination as a normal file
|
|
* -- End of options
|
|
*/
|
|
export class CpCommand implements ShellCommand {
|
|
name = 'cp';
|
|
description = 'Copy files and directories';
|
|
usage = 'cp [-rRfinpavT] [--] source... destination';
|
|
|
|
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', 'i', 'n', 'p', 'a', 'v', 'T'],
|
|
booleanLong: ['recursive', 'force', 'interactive', 'no-clobber', 'preserve', 'archive', 'verbose', 'no-target-directory'],
|
|
longToShort: {
|
|
recursive: 'r',
|
|
force: 'f',
|
|
interactive: 'i',
|
|
'no-clobber': 'n',
|
|
preserve: 'p',
|
|
archive: 'a',
|
|
verbose: 'v',
|
|
'no-target-directory': 'T',
|
|
},
|
|
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') || parsed.flags.has('a'),
|
|
force: parsed.flags.has('f'),
|
|
noClobber: parsed.flags.has('n'),
|
|
verbose: parsed.flags.has('v'),
|
|
noTargetDir: parsed.flags.has('T'),
|
|
};
|
|
|
|
const paths = parsed.operands;
|
|
if (paths.length < 1) {
|
|
return createErrorResult(`${this.name}: missing file operand\nUsage: ${this.usage}`);
|
|
}
|
|
if (paths.length < 2) {
|
|
return createErrorResult(`${this.name}: missing destination file operand after '${paths[0]}'\nUsage: ${this.usage}`);
|
|
}
|
|
|
|
const sources = paths.slice(0, -1);
|
|
const destination = paths[paths.length - 1];
|
|
|
|
try {
|
|
validateWritePath(destination, this.name, cwd);
|
|
} catch (error) {
|
|
return createErrorResult(error instanceof Error ? error.message : 'Unknown error');
|
|
}
|
|
|
|
const destAbsolutePath = resolvePath(destination, cwd);
|
|
|
|
let destIsDir = false;
|
|
try {
|
|
const destStats = await this.fs.stat(destAbsolutePath);
|
|
destIsDir = destStats.isDirectory();
|
|
} catch {
|
|
// Destination doesn't exist — that's fine
|
|
}
|
|
if (opts.noTargetDir) destIsDir = false;
|
|
|
|
if (sources.length > 1 && !destIsDir) {
|
|
return createErrorResult(`${this.name}: target '${destination}' is not a directory`);
|
|
}
|
|
|
|
const verboseOut: string[] = [];
|
|
|
|
for (const source of sources) {
|
|
try {
|
|
const sourceAbsolutePath = resolvePath(source, cwd);
|
|
const sourceStats = await this.fs.stat(sourceAbsolutePath);
|
|
|
|
const targetPath = destIsDir
|
|
? join(destAbsolutePath, basename(source))
|
|
: destAbsolutePath;
|
|
|
|
// Existence / no-clobber check.
|
|
let targetExists = false;
|
|
try {
|
|
await this.fs.stat(targetPath);
|
|
targetExists = true;
|
|
} catch {
|
|
// Doesn't exist.
|
|
}
|
|
|
|
if (targetExists && opts.noClobber) {
|
|
continue; // Silently skip.
|
|
}
|
|
|
|
if (sourceStats.isDirectory()) {
|
|
if (!opts.recursive) {
|
|
return createErrorResult(`${this.name}: -r not specified; omitting directory '${source}'`);
|
|
}
|
|
await this.copyDirectoryRecursive(sourceAbsolutePath, targetPath, opts.force, verboseOut, opts.verbose, source);
|
|
} else {
|
|
if (targetExists && opts.force) {
|
|
try {
|
|
await this.fs.unlink(targetPath);
|
|
} catch {
|
|
// Non-fatal; writeFile below may still succeed.
|
|
}
|
|
}
|
|
await this.copyFile(sourceAbsolutePath, targetPath);
|
|
if (opts.verbose) verboseOut.push(`'${source}' -> '${targetPath}'`);
|
|
}
|
|
} catch (error) {
|
|
const { kind, message } = classifyFsError(error);
|
|
if (kind === 'ENOENT') {
|
|
return createErrorResult(`${this.name}: cannot stat '${source}': No such file or directory`);
|
|
}
|
|
if (kind === 'EACCES') {
|
|
return createErrorResult(`${this.name}: cannot access '${source}': Permission denied`);
|
|
}
|
|
return createErrorResult(`${this.name}: cannot copy '${source}': ${message}`);
|
|
}
|
|
}
|
|
|
|
return createSuccessResult(verboseOut.length > 0 ? verboseOut.join('\n') + '\n' : '');
|
|
}
|
|
|
|
private async copyFile(sourcePath: string, targetPath: string): Promise<void> {
|
|
// Ensure parent dir exists. POSIX cp would error, but we keep this
|
|
// permissive behavior because our VFS often lacks intermediate dirs
|
|
// and it matches existing project expectations.
|
|
const targetDir = dirname(targetPath);
|
|
try {
|
|
await this.fs.stat(targetDir);
|
|
} catch {
|
|
await this.fs.mkdir(targetDir, { recursive: true });
|
|
}
|
|
const content = await this.fs.readFile(sourcePath);
|
|
await this.fs.writeFile(targetPath, content);
|
|
}
|
|
|
|
private async copyDirectoryRecursive(
|
|
sourcePath: string,
|
|
targetPath: string,
|
|
force: boolean,
|
|
verboseOut: string[],
|
|
verbose: boolean,
|
|
displaySource: string,
|
|
): Promise<void> {
|
|
await this.fs.mkdir(targetPath, { recursive: true });
|
|
if (verbose) verboseOut.push(`'${displaySource}' -> '${targetPath}'`);
|
|
|
|
const entries = await this.fs.readdir(sourcePath, { withFileTypes: true });
|
|
for (const entry of entries) {
|
|
const sourceEntryPath = join(sourcePath, entry.name);
|
|
const targetEntryPath = join(targetPath, entry.name);
|
|
if (entry.isDirectory()) {
|
|
await this.copyDirectoryRecursive(sourceEntryPath, targetEntryPath, force, verboseOut, verbose, `${displaySource}/${entry.name}`);
|
|
} else {
|
|
try {
|
|
await this.fs.stat(targetEntryPath);
|
|
if (force) {
|
|
try { await this.fs.unlink(targetEntryPath); } catch { /* ignore */ }
|
|
}
|
|
} catch {
|
|
// Target doesn't exist — fine.
|
|
}
|
|
await this.copyFile(sourceEntryPath, targetEntryPath);
|
|
if (verbose) verboseOut.push(`'${displaySource}/${entry.name}' -> '${targetEntryPath}'`);
|
|
}
|
|
}
|
|
}
|
|
}
|