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