@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 141 lines 5.02 KB
Raw
1 import {
2 Framework,
3 FrameworkError,
4 loadProjectModule,
5 } from "@alchemy.run/frontend-frameworks/core";
6 import * as Effect from "effect/Effect";
7 import * as Layer from "effect/Layer";
8 import { Cwd } from "./Cwd.ts";
9 import * as Options from "./Options.ts";
10 import * as Vite from "./Vite.ts";
11
12 /**
13 * The built-in Vite implementation of framework-core's `Framework` service —
14 * the default when `e2e.config.ts` names no framework. Framework packages
15 * (waku/astro/sveltekit/nextjs) provide their own layers with the same shape.
16 */
17 export const makeViteFramework = (options: Options.Options): Options.Options.FrameworkLayer =>
18 Layer.effect(
19 Framework,
20 Effect.gen(function* () {
21 const vite = yield* Vite.Vite;
22 const { worker } = Options.resolveCloudflareOptions(options);
23 const wrapError = (error: Vite.ViteError) =>
24 new FrameworkError({
25 framework: "vite",
26 message: error.message,
27 cause: error.cause,
28 });
29 return Framework.of({
30 build: (buildOptions) =>
31 vite
32 .build(
33 worker,
34 buildOptions?.root !== undefined ? { root: buildOptions.root } : undefined,
35 )
36 .pipe(Effect.mapError(wrapError)),
37 dev: (devOptions) =>
38 vite
39 .dev(worker, {
40 ...(devOptions?.root !== undefined ? { root: devOptions.root } : undefined),
41 ...(devOptions?.port !== undefined
42 ? { server: { port: devOptions.port } }
43 : undefined),
44 })
45 .pipe(
46 Effect.map(({ url }) => ({ url })),
47 Effect.mapError(wrapError),
48 ),
49 });
50 }),
51 ).pipe(Layer.provide(Vite.ViteLive));
52
53 /** The exports a framework package may provide (see {@link resolve}). */
54 interface FrameworkModule {
55 readonly default?: unknown;
56 readonly framework?: unknown;
57 }
58
59 const invalidExport = (specifier: string) =>
60 new FrameworkError({
61 framework: specifier,
62 message:
63 `"${specifier}" does not provide a usable framework export: expected a default export ` +
64 "(or named export `framework`) that is a `(options) => Layer<Framework>` factory " +
65 "or a `Layer<Framework>`",
66 });
67
68 const toFrameworkLayer: (
69 candidate: unknown,
70 options: Options.Options,
71 specifier: string,
72 ) => Effect.Effect<Options.Options.FrameworkLayer, unknown, Options.Options.FrameworkServices> =
73 Effect.fn(function* (candidate: unknown, options: Options.Options, specifier: string) {
74 const resolved = Effect.isEffect(candidate)
75 ? // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- framework export from a dynamically imported module
76 yield* candidate as Effect.Effect<unknown, unknown, Options.Options.FrameworkServices>
77 : candidate;
78 if (Layer.isLayer(resolved)) {
79 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- framework export from a dynamically imported module
80 return resolved as Options.Options.FrameworkLayer;
81 }
82 if (typeof resolved === "function") {
83 return yield* toFrameworkLayer(
84 (resolved as Options.Options.FrameworkFactory)(options),
85 options,
86 specifier,
87 );
88 }
89 return yield* invalidExport(specifier);
90 });
91
92 /**
93 * Resolve the `Framework` Layer for a fixture from its options:
94 *
95 * - no `framework` — the built-in Vite implementation over `options.vite`
96 * - a string — imported from the fixture's own `node_modules` (via
97 * framework-core's `loadProjectModule`, so the fixture's installed copy of
98 * the framework is the one driven); its default export (or named export
99 * `framework`) is applied to the options
100 * - a factory — applied to the options
101 * - a Layer — used as-is
102 */
103 export const resolve = Effect.fn(function* (options: Options.Options) {
104 const input = options.framework;
105 if (input === undefined) {
106 return makeViteFramework(options);
107 }
108 if (typeof input === "string") {
109 const cwd = yield* Cwd;
110 const module_ = yield* loadProjectModule<FrameworkModule>(cwd, input).pipe(
111 Effect.mapError(
112 (error) =>
113 new FrameworkError({
114 framework: input,
115 message: error.message,
116 cause: error.cause,
117 }),
118 ),
119 );
120 const candidate = module_.default ?? module_.framework;
121 if (candidate === undefined) {
122 return yield* invalidExport(input);
123 }
124 return yield* toFrameworkLayer(candidate, options, input);
125 }
126 return yield* toFrameworkLayer(input, options, "e2e.config.ts framework option");
127 });
128
129 /**
130 * The `Framework` Layer for the current fixture: loads `e2e.config.ts` and
131 * resolves its `framework` option (defaulting to the built-in Vite
132 * implementation). Everything downstream (`Cli`, `Server`) dispatches through
133 * the `Framework` service and never talks to Vite directly.
134 */
135 export const layer: Layer.Layer<Framework, unknown, Options.Options.FrameworkServices> =
136 Layer.unwrap(
137 Effect.gen(function* () {
138 const options = yield* Options.load();
139 return yield* resolve(options);
140 }),
141 );