@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 145 lines 5.02 KB
Raw
1 import {
2 Framework,
3 FrameworkError,
4 readBuildOutput,
5 writeBuildOutput,
6 type BuildOutput,
7 type DeployTargetServer,
8 } from "@alchemy.run/frontend-frameworks/core";
9 import * as Context from "effect/Context";
10 import * as Effect from "effect/Effect";
11 import * as FileSystem from "effect/FileSystem";
12 import * as Layer from "effect/Layer";
13 import * as Path from "effect/Path";
14 import type * as Scope from "effect/Scope";
15 import { makeCloudflareTarget } from "./CloudflareTarget.ts";
16 import { Cwd } from "./Cwd.ts";
17 import * as Options from "./Options.ts";
18
19 export interface Instance {
20 url: URL;
21 fetch(path: string, init?: RequestInit): Promise<Response>;
22 fetchText(path: string, init?: RequestInit): Promise<string>;
23 fetchJson<T>(path: string, init?: RequestInit): Promise<T>;
24 dispose(): Promise<void>;
25 }
26
27 declare namespace Instance {
28 type Raw = Omit<Instance, "dispose">;
29 }
30
31 export class Server extends Context.Service<
32 Server,
33 {
34 readonly live: () => Effect.Effect<Instance.Raw, FrameworkError, Scope.Scope>;
35 readonly dev: () => Effect.Effect<Instance.Raw, FrameworkError, Scope.Scope>;
36 }
37 >()("@alchemy.run/cloudflare-test-tools/e2e/Server") {}
38
39 /**
40 * Where the harness persists a fixture's `BuildOutput`, relative to the
41 * fixture root. Persistence is the harness's E2E mechanism (preview serves
42 * from it) — it is NOT part of the `Framework` contract; `Framework.build`
43 * returns the `BuildOutput` purely in-memory.
44 */
45 export const BUILD_OUTPUT_FILE = "dist/build.json";
46
47 /**
48 * Run `Framework.build` and persist the returned `BuildOutput` to
49 * {@link BUILD_OUTPUT_FILE} (framework-core's `writeBuildOutput`), so
50 * `preview` can serve it without rebuilding.
51 *
52 * The fixture's `Options.root` (if any) is resolved against the harness cwd
53 * and threaded into `Framework.build` as `FrameworkBuildOptions.root`; the
54 * persisted `dist/build.json` stays anchored at the fixture root regardless.
55 */
56 export const buildAndPersist: Effect.Effect<
57 BuildOutput,
58 FrameworkError,
59 Framework | FileSystem.FileSystem | Path.Path
60 > = Effect.gen(function* () {
61 const framework = yield* Framework;
62 const path = yield* Path.Path;
63 const cwd = yield* Cwd;
64 const options = yield* Options.load().pipe(
65 Effect.mapError(
66 (cause) => new FrameworkError({ message: "Failed to load e2e.config.ts", cause }),
67 ),
68 );
69 const root = yield* Options.resolveRoot(options);
70 const output = yield* framework.build(root !== undefined ? { root } : undefined);
71 yield* writeBuildOutput(path.resolve(cwd, BUILD_OUTPUT_FILE), output).pipe(
72 Effect.mapError(
73 (cause) => new FrameworkError({ message: "Failed to write build.json", cause }),
74 ),
75 );
76 return output;
77 });
78
79 /** Wrap a target's serve result (or any base URL) into fetch helpers. */
80 const toInstance = (server: DeployTargetServer): Instance.Raw => {
81 const url = new URL(server.url);
82 const baseFetch =
83 server.fetch ?? ((path: string, init?: RequestInit) => fetch(new URL(path, url), init));
84 return {
85 url,
86 fetch: baseFetch,
87 fetchText: (path: string, init?: RequestInit) =>
88 baseFetch(path, init).then((response) => response.text()),
89 fetchJson: <T>(path: string, init?: RequestInit) =>
90 baseFetch(path, init).then((response) => response.json() as Promise<T>),
91 };
92 };
93
94 export const layer = Layer.effect(
95 Server,
96 Effect.gen(function* () {
97 const framework = yield* Framework;
98 const fs = yield* FileSystem.FileSystem;
99 const path = yield* Path.Path;
100 const cwd = yield* Cwd;
101 const options = yield* Options.load();
102 // The deploy target for this fixture. Only the cloudflare target exists
103 // today; `Options.TargetOptions` is where another platform would be
104 // selected.
105 const target = makeCloudflareTarget(Options.resolveCloudflareOptions(options));
106 // The fixture's project root (Options.root resolved against the harness
107 // cwd) — threaded into Framework.dev; buildAndPersist resolves it itself.
108 const root = yield* Options.resolveRoot(options);
109
110 const buildJsonPath = path.resolve(cwd, BUILD_OUTPUT_FILE);
111 const persistedBuild = buildAndPersist.pipe(
112 Effect.provideService(Framework, framework),
113 Effect.provideService(FileSystem.FileSystem, fs),
114 Effect.provideService(Path.Path, path),
115 );
116
117 const live = () =>
118 readBuildOutput(buildJsonPath).pipe(
119 Effect.provideService(FileSystem.FileSystem, fs),
120 Effect.catch(() => persistedBuild),
121 Effect.flatMap((build) =>
122 target.serve({ root: cwd, output: build }).pipe(
123 Effect.map(toInstance),
124 Effect.mapError(
125 (error) =>
126 new FrameworkError({
127 message: error.message,
128 cause: error.cause,
129 }),
130 ),
131 Effect.provideService(FileSystem.FileSystem, fs),
132 Effect.provideService(Path.Path, path),
133 ),
134 ),
135 );
136
137 const dev = () =>
138 framework.dev(root !== undefined ? { root } : undefined).pipe(Effect.map(toInstance));
139
140 return Server.of({
141 live,
142 dev,
143 });
144 }),
145 );