@@ -0,0 +1,165 @@
1+import {
2+ applyExactTextEdit,
3+ FileEditError,
4+ type FileEditErrorCode,
5+} from "./exact-text.ts";
6+
7+interface FileEditCliArgs {
8+ cwd: string;
9+ path: string;
10+ oldText: string;
11+ newText: string;
12+}
13+
14+interface FileEditCliIo {
15+ stdout: (text: string) => void;
16+ stderr: (text: string) => void;
17+}
18+
19+type FileEditCliSuccess = {
20+ ok: true;
21+ path: string;
22+ changed: boolean;
23+};
24+
25+type FileEditCliFailure = {
26+ ok: false;
27+ code: FileEditErrorCode | "invalid_args";
28+ message: string;
29+ path?: string;
30+};
31+
32+const HELP = `Usage: mc-edit <path> (--old <text> | --old-file <path>) [--new <text> | --new-file <path>] [--cwd <path>]
33+
34+Apply one safe exact-text edit to an existing file.
35+- The expected old text must match exactly once.
36+- Omit --new / --new-file to delete the matched text.
37+- Output is a single JSON object on stdout.`;
38+
39+async function readArgText(
40+ flag: "--old-file" | "--new-file",
41+ filePath: string,
42+): Promise<string> {
43+ const file = Bun.file(filePath);
44+ if (!(await file.exists())) {
45+ throw new FileEditError(
46+ "file_not_found",
47+ `${flag} file not found: "${filePath}".`,
48+ );
49+ }
50+ return file.text();
51+}
52+
53+async function parseFileEditCliArgs(
54+ argv: string[],
55+): Promise<FileEditCliArgs | null> {
56+ let cwd = process.cwd();
57+ let path: string | null = null;
58+ let oldText: string | null = null;
59+ let oldFilePath: string | null = null;
60+ let newText: string | null = null;
61+ let newFilePath: string | null = null;
62+
63+ for (let i = 0; i < argv.length; i++) {
64+ const arg = argv[i] ?? "";
65+ switch (arg) {
66+ case "--help":
67+ case "-h":
68+ return null;
69+ case "--cwd":
70+ cwd = argv[++i] ?? process.cwd();
71+ break;
72+ case "--old":
73+ oldText = argv[++i] ?? "";
74+ break;
75+ case "--old-file":
76+ oldFilePath = argv[++i] ?? null;
77+ break;
78+ case "--new":
79+ newText = argv[++i] ?? "";
80+ break;
81+ case "--new-file":
82+ newFilePath = argv[++i] ?? null;
83+ break;
84+ default:
85+ if (arg.startsWith("-")) {
86+ throw new Error(`Unknown flag: ${arg}`);
87+ }
88+ if (path !== null) {
89+ throw new Error("Expected exactly one positional <path> argument.");
90+ }
91+ path = arg;
92+ }
93+ }
94+
95+ if (path === null) {
96+ throw new Error("Missing required <path> argument.");
97+ }
98+ if ((oldText === null) === (oldFilePath === null)) {
99+ throw new Error("Provide exactly one of --old or --old-file.");
100+ }
101+ if (newText !== null && newFilePath !== null) {
102+ throw new Error("Provide at most one of --new or --new-file.");
103+ }
104+
105+ return {
106+ cwd,
107+ path,
108+ oldText: oldText ?? (await readArgText("--old-file", oldFilePath ?? "")),
109+ newText:
110+ newText ??
111+ (newFilePath ? await readArgText("--new-file", newFilePath) : ""),
112+ };
113+}
114+
115+function writeJson(
116+ io: FileEditCliIo,
117+ payload: FileEditCliSuccess | FileEditCliFailure,
118+) {
119+ io.stdout(`${JSON.stringify(payload)}\n`);
120+}
121+
122+function normalizeCliError(error: unknown): FileEditCliFailure {
123+ if (error instanceof FileEditError) {
124+ return {
125+ ok: false,
126+ code: error.code,
127+ message: error.message,
128+ };
129+ }
130+ if (error instanceof Error) {
131+ return {
132+ ok: false,
133+ code: "invalid_args",
134+ message: error.message,
135+ };
136+ }
137+ return {
138+ ok: false,
139+ code: "invalid_args",
140+ message: "Unknown error.",
141+ };
142+}
143+
144+export async function runFileEditCli(
145+ argv: string[],
146+ io: FileEditCliIo = {
147+ stdout: (text) => process.stdout.write(text),
148+ stderr: (text) => process.stderr.write(text),
149+ },
150+): Promise<number> {
151+ try {
152+ const parsed = await parseFileEditCliArgs(argv);
153+ if (parsed === null) {
154+ io.stderr(`${HELP}\n`);
155+ return 0;
156+ }
157+
158+ const result = await applyExactTextEdit(parsed);
159+ writeJson(io, { ok: true, ...result });
160+ return 0;
161+ } catch (error) {
162+ writeJson(io, normalizeCliError(error));
163+ return 1;
164+ }
165+}