@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 134 lines 4.89 KB
Raw
1 import * as NodePath from "node:path";
2 /**
3 * `@alchemy.run/frontend-frameworks/waku/node` — the Node container deploy
4 * target for the Waku integration.
5 *
6 * Waku's Node story is a plain-Node waku build with waku's own `node`
7 * adapter selected via `unstable_adapter`. The finishing pass writes a
8 * Node HTTP program that serves `clientDirectory` first (extensionless
9 * HTML: `/about` → `about/index.html`), then falls through to
10 * `INTERNAL_runFetch` on `PORT` (default 3000), and answers `GET /health`.
11 *
12 * - **`adapter`** — the project's `waku/adapters/node`.
13 * - **`vitePlugins`** — none (plain Node; no workerd plugin).
14 * - **`bundle`** — Node resolve conditions (no `workerd`, no `@aws-sdk/`).
15 */
16 import * as Effect from "effect/Effect";
17 import * as FileSystem from "effect/FileSystem";
18 import * as Path from "effect/Path";
19 import type { Config as WakuConfig } from "waku/config";
20 import { runBuildChild } from "../core/BuildChild.ts";
21 import { DeployTargetError, makeDeployTarget } from "../core/index.ts";
22 import {
23 NODE_BUNDLE_CONDITIONS,
24 NODE_SERVE_ENTRY_FILE_NAME,
25 relativeClientDirExpression,
26 writeNodeServeEntry,
27 } from "../core/NodeServe.ts";
28 import { make, type WakuTarget, type WakuTargetBuildContext } from "./Waku.ts";
29
30 export type { WakuTarget, WakuTargetContext } from "./Waku.ts";
31
32 export interface WakuNodeTargetConfig {
33 readonly main?: string | undefined;
34 }
35
36 const fail = (message: string) => (cause?: unknown) =>
37 new DeployTargetError({ platform: "node", message, cause });
38
39 const makeNodeAdapterTarget = (
40 config?: WakuNodeTargetConfig,
41 ): WakuTarget<WakuNodeTargetConfig | undefined> =>
42 makeDeployTarget({
43 platform: "node",
44 config,
45 entry: config?.main !== undefined ? { main: config.main } : undefined,
46 bundle: {
47 conditions: [...NODE_BUNDLE_CONDITIONS],
48 },
49 adapter: (context) =>
50 Effect.succeed(NodePath.join(context.wakuDirectory, "dist/adapters/node.js")),
51 vitePlugins: () => Effect.sync(() => []),
52 finish: (output, _context) =>
53 Effect.gen(function* () {
54 const fs = yield* FileSystem.FileSystem;
55 const path = yield* Path.Path;
56 if (output.distDirectory === undefined) {
57 return yield* fail("The waku build produced no dist directory")(undefined);
58 }
59 if (output.clientDirectory === undefined) {
60 return yield* fail(
61 "The waku build produced no client directory for the Node serve entry",
62 )(undefined);
63 }
64 const serverDir = path.join(output.distDirectory, "server");
65 const serverIndex = path.join(serverDir, "index.js");
66 const hasServerIndex = yield* fs
67 .exists(serverIndex)
68 .pipe(Effect.mapError(fail("Failed to probe the built server entry")));
69 if (!hasServerIndex) {
70 return yield* fail(`The waku build produced no server entry at ${serverIndex}`)(
71 undefined,
72 );
73 }
74 yield* fs
75 .writeFileString(
76 path.join(serverDir, "package.json"),
77 `${JSON.stringify({ type: "module" }, null, 2)}\n`,
78 )
79 .pipe(Effect.mapError(fail("Failed to write dist/server/package.json")));
80 const servePath = path.join(serverDir, NODE_SERVE_ENTRY_FILE_NAME);
81 return yield* writeNodeServeEntry({
82 output,
83 servePath,
84 serveModuleName: path.join("server", NODE_SERVE_ENTRY_FILE_NAME).replaceAll("\\", "/"),
85 clientDirExpression: relativeClientDirExpression(servePath, output.clientDirectory),
86 handler: {
87 kind: "fetch",
88 imports: `import { INTERNAL_runFetch } from "./index.js";`,
89 expr: "(req, ...args) => INTERNAL_runFetch(process.env, req, ...args)",
90 },
91 htmlHandling: "drop-trailing-slash",
92 platform: "node",
93 });
94 }),
95 });
96
97 export interface WakuNodeBuildChildConfig {
98 readonly rootDir: string;
99 readonly config: WakuNodeTargetConfig | undefined;
100 readonly waku: WakuConfig | undefined;
101 }
102
103 export const buildInChild = (config: WakuNodeBuildChildConfig) =>
104 Effect.gen(function* () {
105 const framework = yield* make({
106 root: config.rootDir,
107 target: makeNodeAdapterTarget(config.config),
108 waku: config.waku,
109 });
110 return yield* framework.build({ root: config.rootDir });
111 });
112
113 export const makeNodeTarget = (
114 config?: WakuNodeTargetConfig,
115 ): WakuTarget<WakuNodeTargetConfig | undefined> => ({
116 ...makeNodeAdapterTarget(config),
117 build: (context: WakuTargetBuildContext) =>
118 runBuildChild({
119 runtime: "node",
120 module: import.meta.url,
121 rootDir: context.root,
122 env: context.env,
123 framework: "waku",
124 config: {
125 rootDir: context.root,
126 config,
127 waku: context.waku,
128 } satisfies WakuNodeBuildChildConfig,
129 }).pipe(Effect.mapError((error) => fail(error.message)(error.cause))),
130 });
131
132 export const target = makeNodeTarget;
133
134 export default makeNodeTarget;