@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, 06:00 UTC

main
ts 115 lines 3.7 KB
Raw
1 import cloudflareVitePlugin, {
2 type CloudflareVitePluginOptions,
3 } from "@alchemy.run/cloudflare-runtime/vite";
4 import * as FrameworkCore from "@alchemy.run/frontend-frameworks/core";
5 import * as Context from "effect/Context";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as FileSystem from "effect/FileSystem";
9 import * as Layer from "effect/Layer";
10 import type * as Scope from "effect/Scope";
11 import type * as ViteModule from "vite";
12 import { Cwd } from "./Cwd.ts";
13
14 export class ViteError extends Data.TaggedError<"ViteError">("ViteError")<{
15 readonly message: string;
16 readonly cause?: unknown;
17 }> {}
18
19 export type BuildOutput = FrameworkCore.BuildOutput;
20
21 export class Vite extends Context.Service<
22 Vite,
23 {
24 readonly build: (
25 pluginOptions?: CloudflareVitePluginOptions,
26 config?: ViteModule.InlineConfig,
27 ) => Effect.Effect<BuildOutput, ViteError>;
28 readonly dev: (
29 pluginOptions?: CloudflareVitePluginOptions,
30 config?: ViteModule.InlineConfig,
31 ) => Effect.Effect<{ url: string; server: ViteModule.ViteDevServer }, ViteError, Scope.Scope>;
32 }
33 >()("@alchemy/Vite") {}
34
35 export const ViteLive = Layer.effect(
36 Vite,
37 Effect.gen(function* () {
38 const fs = yield* FileSystem.FileSystem;
39 const cwd = yield* Cwd;
40
41 const load = (root: string = cwd): Effect.Effect<typeof ViteModule, ViteError> =>
42 FrameworkCore.loadProjectModule<typeof ViteModule>(root, "vite").pipe(
43 Effect.mapError(
44 (error) =>
45 new ViteError({
46 message: "Failed to load Vite",
47 cause: error.cause,
48 }),
49 ),
50 );
51
52 return Vite.of({
53 build: Effect.fn(function* (pluginOptions, config) {
54 const vite = yield* load(config?.root);
55 const collector = yield* FrameworkCore.makeBuildOutputCollector({
56 entryEnvironment: pluginOptions?.viteEnvironments?.entry,
57 }).pipe(Effect.provideService(FileSystem.FileSystem, fs));
58 yield* Effect.tryPromise({
59 try: async () => {
60 const builder = await vite.createBuilder(
61 {
62 ...config,
63 plugins: [
64 ...(config?.plugins ?? []),
65 cloudflareVitePlugin(pluginOptions),
66 collector.plugin,
67 ],
68 },
69 null,
70 );
71 await builder.buildApp();
72 },
73 catch: (error) => new ViteError({ message: "Failed to build", cause: error }),
74 });
75 return yield* collector
76 .collect()
77 .pipe(
78 Effect.mapError(
79 (error) => new ViteError({ message: error.message, cause: error.cause }),
80 ),
81 );
82 }),
83 dev: Effect.fn(function* (pluginOptions, config) {
84 const vite = yield* load(config?.root);
85 const server = yield* Effect.acquireRelease(
86 Effect.tryPromise({
87 try: async () => {
88 const server = await vite.createServer({
89 ...config,
90 plugins: [...(config?.plugins ?? []), cloudflareVitePlugin(pluginOptions)],
91 });
92 return await server.listen();
93 },
94 catch: (error) =>
95 new ViteError({
96 message: "Failed to run development server",
97 cause: error,
98 }),
99 }),
100 (server) => Effect.promise(async () => await server.close()),
101 );
102 const url = server.resolvedUrls?.local[0];
103 if (!url) {
104 throw new ViteError({
105 message: "Could not get URL of development server",
106 });
107 }
108 return {
109 url,
110 server,
111 };
112 }),
113 });
114 }),
115 );