The best way to control Claude Code, Codex and more from any computer or phone

main
ts 96 lines 3.24 KB
Raw
1 import * as crypto from "node:crypto";
2 import { fileURLToPath } from "node:url";
3 import * as Effect from "effect/Effect";
4 import * as FileSystem from "effect/FileSystem";
5 import * as Path from "effect/Path";
6 import { Stack } from "../Stack.ts";
7 import { Stage } from "../Stage.ts";
8
9 /**
10 * Resolve a bundle entrypoint (`main`) to a real filesystem path.
11 *
12 * `main` is user-supplied and is very commonly `import.meta.url` — a
13 * `file://` URL — which `FileSystem.realPath` cannot `lstat` directly
14 * (it would try to stat a literal `file:` path). Convert any `file://`
15 * URL to a path first, then resolve to the canonical real path.
16 */
17 export const resolveMainPath = Effect.fn(function* (main: string) {
18 const fs = yield* FileSystem.FileSystem;
19 const path = yield* Path.Path;
20 const asPath = yield* Effect.sync(() => {
21 try {
22 return fileURLToPath(main);
23 } catch {
24 return main;
25 }
26 });
27 return path.resolve(yield* fs.realPath(asPath));
28 });
29
30 /**
31 * Creates a unique bundle staging directory under the configured runtime directory's
32 * `tmp` root. Each invocation gets its own directory (via a random
33 * nonce) so concurrent bundle operations for the same resource never collide.
34 * Stale directories from previous crashed runs are cleaned up best-effort.
35 */
36 export const createTempBundleDir = (_entry: string, dotAlchemy: string, id: string) =>
37 Effect.gen(function* () {
38 const fs = yield* FileSystem.FileSystem;
39 const path = yield* Path.Path;
40 const stack = yield* Stack;
41 const stage = yield* Stage;
42 const tempRoot = path.join(dotAlchemy, "tmp");
43 yield* fs.makeDirectory(tempRoot, { recursive: true });
44
45 const nonce = crypto.randomUUID().slice(0, 8);
46 const bundleId = `${stack.name}-${stage}-${id}-${nonce}`;
47 const tempDir = path.join(tempRoot, bundleId);
48 yield* fs.makeDirectory(tempDir, { recursive: true });
49
50 return tempDir;
51 });
52
53 /**
54 * Returns a deterministic bundle staging directory without clearing it first.
55 * Useful for Docker build contexts where keeping the directory stable avoids
56 * unnecessary file churn between builds.
57 */
58 export const getStableContextDir = (_entry: string, dotAlchemy: string, id: string) =>
59 Effect.gen(function* () {
60 const fs = yield* FileSystem.FileSystem;
61 const path = yield* Path.Path;
62 const stack = yield* Stack;
63 const stage = yield* Stage;
64 const tempRoot = path.join(dotAlchemy, "tmp");
65 const bundleId = `${stack.name}-${stage}-${id}`;
66 const tempDir = path.join(tempRoot, bundleId);
67
68 yield* fs.makeDirectory(tempDir, { recursive: true });
69
70 return tempDir;
71 });
72
73 /**
74 * Cleans up a bundle's private temp directory.
75 */
76 export const cleanupBundleTempDir = (tempDir: string) =>
77 Effect.gen(function* () {
78 const fs = yield* FileSystem.FileSystem;
79 yield* fs.remove(tempDir, { recursive: true }).pipe(Effect.ignore);
80 });
81
82 export const findCwdForBundle = Effect.fn(function* (entry: string) {
83 const fs = yield* FileSystem.FileSystem;
84 const path = yield* Path.Path;
85 let current = path.dirname(entry);
86 while (true) {
87 if (yield* fs.exists(path.join(current, "package.json"))) {
88 return current;
89 }
90 const parent = path.dirname(current);
91 if (parent === current) {
92 return process.cwd();
93 }
94 current = parent;
95 }
96 });