@@ -0,0 +1,62 @@
1+import { existsSync } from "node:fs";
2+import { dirname, extname, join } from "node:path";
3+import { fileURLToPath } from "node:url";
4+import { resolveProcessScriptPath } from "../runtime/script.ts";
5+
6+function quoteShellArg(value: string): string {
7+ return `'${value.replaceAll("'", `'\\''`)}'`;
8+}
9+
10+function resolveSiblingFileEditScript(scriptPath: string): string | null {
11+ const ext = extname(scriptPath);
12+ if (!ext) return null;
13+
14+ const mainDir = dirname(scriptPath);
15+ const mainBase = scriptPath.slice(mainDir.length + 1);
16+ if (mainBase === `index${ext}` || mainBase === `mc${ext}`) {
17+ return join(mainDir, `mc-edit${ext}`);
18+ }
19+
20+ return null;
21+}
22+
23+function resolveModuleLocalFileEditScript(moduleUrl: string): string | null {
24+ const modulePath = fileURLToPath(moduleUrl);
25+ const ext = extname(modulePath);
26+ if (!ext) return null;
27+
28+ const helperPath = join(dirname(modulePath), "..", "..", `mc-edit${ext}`);
29+ return existsSync(helperPath) ? helperPath : null;
30+}
31+
32+export function resolveFileEditCommand(
33+ execPath: string,
34+ mainModule: string | undefined,
35+ argv1: string | undefined,
36+ moduleUrl = import.meta.url,
37+): string[] {
38+ const scriptPath = resolveProcessScriptPath(mainModule, argv1);
39+ const helperScript = scriptPath
40+ ? resolveSiblingFileEditScript(scriptPath)
41+ : null;
42+ if (helperScript) {
43+ return [execPath, helperScript];
44+ }
45+
46+ const moduleLocalHelper = resolveModuleLocalFileEditScript(moduleUrl);
47+ if (moduleLocalHelper) {
48+ return [execPath, moduleLocalHelper];
49+ }
50+
51+ return ["mc-edit"];
52+}
53+
54+function getFileEditCommand(): string[] {
55+ return resolveFileEditCommand(process.execPath, Bun.main, process.argv[1]);
56+}
57+
58+export function buildFileEditShellPrelude(
59+ command = getFileEditCommand(),
60+): string {
61+ return `mc-edit() { ${command.map(quoteShellArg).join(" ")} "$@"; }`;
62+}