@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 142 lines 4.94 KB
Raw
1 import { AlchemyContext } from "alchemy/AlchemyContext";
2 import * as Command from "alchemy/Command";
3 import * as Namespace from "alchemy/Namespace";
4 import * as Output from "alchemy/Output";
5 import { ProviderModePolicy } from "alchemy/ProviderMode";
6 import { initialCwd } from "alchemy/Util/Node";
7 import { loadFrontendCore } from "alchemy/Website/FrontendCore";
8 import * as Effect from "effect/Effect";
9 import * as Path from "effect/Path";
10 import * as Redacted from "effect/Redacted";
11 import { WebsiteArtifact } from "./Artifact.ts";
12 import { deployWebsite, type FrameworkSiteProps, type Website } from "./FrameworkSite.ts";
13
14 /** A command-built website with the same build vocabulary as Fly and Railway. */
15 export interface StaticSiteProps
16 extends Omit<Command.BuildProps, "env">, Omit<FrameworkSiteProps, "dev" | "memo"> {
17 /** Local command; skips the production build. Without a command, a local static server serves the built output. */
18 dev?: {
19 /** Shell command that starts the native dev server. */
20 command: string;
21 /** Dev command directory; defaults to cwd or rootDir. */
22 cwd?: string;
23 /** Additional development environment variables. */
24 env?: Record<string, string | Redacted.Redacted<string>>;
25 /** Explicit dev URL when the command does not print one. */
26 url?: string;
27 };
28 /** Return index.html on unmatched paths. Mutually exclusive with errorPage. */
29 spa?: boolean;
30 /** HTML file returned with HTTP 404; relative to outdir. Mutually exclusive with spa. */
31 errorPage?: string;
32 }
33
34 const quote = (value: string) => `'${value.replaceAll("'", "'\\''")}'`;
35
36 /**
37 * Build a static website with a shell command and serve it on Prisma Compute.
38 * Only the output directory is packaged; the repository is never uploaded.
39 * Native development declares no Prisma Project, Compute, or custom domain.
40 * `Alchemy.remote()` opts into the live deployment during development.
41 *
42 * ### Creating a Static Website
43 * **Example:** Hugo website
44 * ```typescript
45 * const site = yield* Prisma.Website.StaticSite("Blog", {
46 * command: "hugo --minify",
47 * outdir: "public",
48 * });
49 * ```
50 *
51 * ### Monorepos and Development
52 * **Example:** A custom frontend build
53 * ```typescript
54 * const site = yield* Prisma.Website.StaticSite("Web", {
55 * cwd: "apps/web",
56 * command: "npm run build",
57 * outdir: "dist",
58 * spa: true,
59 * dev: { command: "npm run dev" },
60 * });
61 * ```
62 *
63 * @resource
64 * @product Website
65 */
66 export const StaticSite = (id: string, props: StaticSiteProps) =>
67 Effect.gen(function* () {
68 const context = yield* AlchemyContext;
69 const remote = yield* ProviderModePolicy;
70 const local = context.dev && remote !== true;
71 const cwd = props.cwd ?? props.rootDir;
72 if (local && props.dev !== undefined) {
73 const dev = yield* Command.Dev("Dev", {
74 command: props.dev.command,
75 cwd: props.dev.cwd ?? cwd,
76 env: { ...props.env, ...props.dev.env },
77 });
78 return {
79 url: props.dev.url ?? dev.url,
80 compute: undefined,
81 project: undefined,
82 domain: undefined,
83 } satisfies Website;
84 }
85 if (props.spa && props.errorPage !== undefined) {
86 return yield* Effect.die(new Error("StaticSite spa and errorPage are mutually exclusive."));
87 }
88 const build = yield* Command.Build("Build", {
89 command: props.command,
90 cwd,
91 env: props.env,
92 outdir: props.outdir,
93 memo: props.memo,
94 shell: props.shell,
95 timeout: props.timeout,
96 });
97 const handling = props.assets?.notFoundHandling;
98 const options = {
99 notFoundHandling:
100 props.errorPage !== undefined
101 ? ("404-page" as const)
102 : props.spa
103 ? ("spa" as const)
104 : handling === "single-page-application"
105 ? ("spa" as const)
106 : (handling ?? ("none" as const)),
107 htmlHandling: props.assets?.htmlHandling,
108 errorPage: props.errorPage,
109 };
110 if (local) {
111 const { makeNodeServeEntrySource } = yield* loadFrontendCore;
112 const path = yield* Path.Path;
113 const runtime = yield* Effect.sync(() => process.execPath);
114 const command = Output.map(build.outdir, (outdir) => {
115 const source = makeNodeServeEntrySource({
116 ...options,
117 clientDirExpression: JSON.stringify(path.resolve(initialCwd, outdir)),
118 printUrl: true,
119 });
120 return `${quote(runtime)} --eval ${quote(source)}`;
121 });
122 const dev = yield* Command.Dev("Dev", {
123 command,
124 cwd,
125 shell: true,
126 env: { ...props.env, HOST: "127.0.0.1", PORT: "0" },
127 });
128 return {
129 url: dev.url,
130 compute: undefined,
131 project: undefined,
132 domain: undefined,
133 } satisfies Website;
134 }
135 const artifact = yield* WebsiteArtifact("Artifact", {
136 root: cwd ?? ".",
137 distDir: build.outdir,
138 buildHash: build.hash.output,
139 static: options,
140 });
141 return yield* deployWebsite(props, artifact);
142 }).pipe(Namespace.push(id));