The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 2 | import * as Command from "alchemy/Command"; |
| 3 | import type { MemoOptions } from "alchemy/Command/Memo"; |
| 4 | import * as Namespace from "alchemy/Namespace"; |
| 5 | import * as Output from "alchemy/Output"; |
| 6 | import { ProviderModePolicy } from "alchemy/ProviderMode"; |
| 7 | import { initialCwd } from "alchemy/Util/Node"; |
| 8 | import { loadFrontendCore } from "alchemy/Website/FrontendCore"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Path from "effect/Path"; |
| 11 | import type * as Redacted from "effect/Redacted"; |
| 12 | import { App } from "../App.ts"; |
| 13 | import { Certificate } from "../Certificate.ts"; |
| 14 | import { IpAssignment } from "../IpAssignment.ts"; |
| 15 | import { Service, type ServiceProps } from "../Service.ts"; |
| 16 | import { |
| 17 | type FrameworkSite, |
| 18 | publishWebsiteDomainDns, |
| 19 | type Ref, |
| 20 | type WebsiteAssetsProps, |
| 21 | type WebsiteDomain, |
| 22 | staticConfigFromAssets, |
| 23 | websiteDomainName, |
| 24 | } from "./FrameworkSite.ts"; |
| 25 | |
| 26 | const DEFAULT_PORT = 3000; |
| 27 | |
| 28 | const resolveRef = <T>(ref: Ref<T>) => (Effect.isEffect(ref) ? ref : Effect.succeed(ref)); |
| 29 | |
| 30 | export interface StaticSiteProps { |
| 31 | /** Deployment strategy forwarded to the hosted Fly Service. */ |
| 32 | deploy?: ServiceProps["deploy"]; |
| 33 | /** Process shutdown policy for the generated server. */ |
| 34 | shutdown?: ServiceProps["shutdown"]; |
| 35 | /** Named Machine readiness checks. */ |
| 36 | checks?: ServiceProps["checks"]; |
| 37 | /** Override proxy services and their routing health checks. */ |
| 38 | services?: ServiceProps["services"]; |
| 39 | /** |
| 40 | * Path to the local site directory (working directory for |
| 41 | * {@link build.command}). |
| 42 | * @default "." |
| 43 | */ |
| 44 | path?: string; |
| 45 | /** |
| 46 | * Build executed before deploy. |
| 47 | */ |
| 48 | build: { |
| 49 | /** Shell command that produces the site (e.g. `"hugo --minify"`). */ |
| 50 | command: string; |
| 51 | /** Directory the command writes, relative to {@link path}. */ |
| 52 | output: string; |
| 53 | /** Environment variables for the build command. */ |
| 54 | env?: Record<string, string | Redacted.Redacted<string>>; |
| 55 | }; |
| 56 | /** |
| 57 | * Controls which files are hashed to decide whether the build re-runs. |
| 58 | * @default true |
| 59 | */ |
| 60 | memo?: MemoOptions | boolean; |
| 61 | /** |
| 62 | * Process environment for the hosted static server. |
| 63 | */ |
| 64 | env?: Record<string, string | Redacted.Redacted<string>>; |
| 65 | /** |
| 66 | * Miss handling for the generated file server. |
| 67 | */ |
| 68 | assets?: WebsiteAssetsProps; |
| 69 | /** |
| 70 | * Parent Fly App. When omitted, a `Fly.App` is created. The Service |
| 71 | * stays in the caller namespace; only `Build` / `Dev` are pushed. |
| 72 | */ |
| 73 | app?: Ref<App>; |
| 74 | /** |
| 75 | * Optional custom hostname. Requests ACME (`Fly.Certificate`) on the App |
| 76 | * and `url` becomes `https://<domain>`. |
| 77 | * |
| 78 | * A string (or `{ name }`) leaves DNS to you. Set `dns` (e.g. |
| 79 | * `{ name: "www.example.com", dns: Cloudflare.DNS.Adapter() }`) to |
| 80 | * publish the ACME challenge `CNAME` and the `A` / `AAAA` records |
| 81 | * through that DNS host. See |
| 82 | * [DNS Adapters](/infrastructure-as-code/dns-adapters). |
| 83 | */ |
| 84 | domain?: WebsiteDomain; |
| 85 | /** |
| 86 | * User-defined tags. Accepted for API parity; Fly Services do not |
| 87 | * surface resource tags. |
| 88 | */ |
| 89 | tags?: Record<string, string>; |
| 90 | /** |
| 91 | * Local dev configuration. When `alchemy dev` runs with `dev.command`, |
| 92 | * the build is skipped and `command` is spawned as a long-lived child. |
| 93 | */ |
| 94 | dev?: { |
| 95 | /** |
| 96 | * Shell command to run as the local dev server (e.g. `npm run dev`). |
| 97 | */ |
| 98 | command: string; |
| 99 | /** |
| 100 | * Working directory for {@link command}. Defaults to {@link cwd}. |
| 101 | */ |
| 102 | cwd?: string; |
| 103 | /** |
| 104 | * Environment variables for {@link command}. |
| 105 | */ |
| 106 | env?: Record<string, string | Redacted.Redacted<string>>; |
| 107 | /** |
| 108 | * Override for the `url` output if alchemy fails to detect it from |
| 109 | * stdout of the dev command. |
| 110 | */ |
| 111 | url?: string; |
| 112 | }; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Deploy a static site built by a shell command to Fly. |
| 117 | * |
| 118 | * `StaticSite` runs a build command (e.g. `npm run build` / `hugo`), |
| 119 | * content-hashes the output directory, and deploys a Service that serves |
| 120 | * those files (plus `/health`). Use this when the site has its own build |
| 121 | * step — Hugo, Zola, Eleventy, or any custom pipeline. |
| 122 | * |
| 123 | * For Vite-based projects, prefer `Fly.Website.Vite`. |
| 124 | * |
| 125 | * `Build` / `Dev` use constant logical ids under `Namespace.push(id)`. |
| 126 | * The Service stays in the caller namespace (same as |
| 127 | * `Cloudflare.Website.StaticSite`). |
| 128 | * |
| 129 | * |
| 130 | * ### Basic Usage |
| 131 | * **Example:** Deploying a Hugo site |
| 132 | * ```typescript |
| 133 | * const site = yield* Fly.Website.StaticSite("Blog", { |
| 134 | * build: { command: "hugo --minify", output: "public" }, |
| 135 | * }); |
| 136 | * ``` |
| 137 | * |
| 138 | * **Example:** SPA-style routing |
| 139 | * ```typescript |
| 140 | * const site = yield* Fly.Website.StaticSite("App", { |
| 141 | * build: { command: "npm run build", output: "dist" }, |
| 142 | * assets: { notFoundHandling: "single-page-application" }, |
| 143 | * }); |
| 144 | * ``` |
| 145 | * |
| 146 | * ### Building from a Subdirectory |
| 147 | * **Example:** Building a frontend in a monorepo |
| 148 | * ```typescript |
| 149 | * const site = yield* Fly.Website.StaticSite("Web", { |
| 150 | * path: "apps/web", |
| 151 | * build: { command: "npm run build", output: "dist" }, |
| 152 | * }); |
| 153 | * ``` |
| 154 | * |
| 155 | * ### Local Development |
| 156 | * **Example:** External dev command |
| 157 | * ```typescript |
| 158 | * const site = yield* Fly.Website.StaticSite("App", { |
| 159 | * build: { command: "npm run build", output: "dist" }, |
| 160 | * dev: { command: "npm run dev" }, |
| 161 | * }); |
| 162 | * ``` |
| 163 | * |
| 164 | * @resource |
| 165 | * @product Website |
| 166 | */ |
| 167 | export const StaticSite = (id: string, props: StaticSiteProps) => |
| 168 | Effect.gen(function* () { |
| 169 | const ctx = yield* AlchemyContext; |
| 170 | const remoted = yield* ProviderModePolicy; |
| 171 | const isLocal = ctx.dev && remoted !== true; |
| 172 | const path = yield* Path.Path; |
| 173 | |
| 174 | const empty = (): FrameworkSite => ({ |
| 175 | url: undefined, |
| 176 | app: undefined, |
| 177 | service: undefined, |
| 178 | ip: undefined, |
| 179 | certificate: undefined, |
| 180 | }); |
| 181 | |
| 182 | if (isLocal && props.dev) { |
| 183 | const dev = yield* Command.Dev("Dev", { |
| 184 | command: props.dev.command, |
| 185 | cwd: props.dev.cwd ?? props.path, |
| 186 | env: props.dev.env ?? props.env, |
| 187 | }).pipe(Namespace.push(id)); |
| 188 | const url = Output.map(dev.url, (value) => value ?? props.dev?.url); |
| 189 | return { ...empty(), url }; |
| 190 | } |
| 191 | |
| 192 | const build = yield* Command.Build("Build", { |
| 193 | command: props.build.command, |
| 194 | cwd: props.path, |
| 195 | memo: props.memo, |
| 196 | outdir: props.build.output, |
| 197 | env: props.build.env ?? props.env, |
| 198 | }).pipe(Namespace.push(id)); |
| 199 | |
| 200 | const cwd = path.resolve(initialCwd, props.path ?? "."); |
| 201 | const outdir = path.resolve(cwd, props.build.output); |
| 202 | const internal = staticConfigFromAssets(props.assets); |
| 203 | const notFoundHandling = |
| 204 | internal.errorPage !== undefined |
| 205 | ? ("404-page" as const) |
| 206 | : internal.spa === true |
| 207 | ? ("spa" as const) |
| 208 | : ("none" as const); |
| 209 | |
| 210 | const { NODE_SERVE_ENTRY_FILE_NAME, relativeClientDirExpression, writeNodeServeEntry } = |
| 211 | yield* loadFrontendCore; |
| 212 | const servePath = path.join(path.dirname(outdir), NODE_SERVE_ENTRY_FILE_NAME); |
| 213 | const serveOutput = { |
| 214 | clientDirectory: outdir, |
| 215 | serverModules: [], |
| 216 | externalWorkspaces: new Set<string>(), |
| 217 | }; |
| 218 | yield* writeNodeServeEntry({ |
| 219 | output: serveOutput, |
| 220 | servePath, |
| 221 | serveModuleName: NODE_SERVE_ENTRY_FILE_NAME, |
| 222 | clientDirExpression: relativeClientDirExpression(servePath, outdir), |
| 223 | notFoundHandling, |
| 224 | printUrl: isLocal, |
| 225 | platform: "node", |
| 226 | }); |
| 227 | |
| 228 | if (isLocal) { |
| 229 | const runtime = yield* Effect.sync(() => process.execPath); |
| 230 | const dev = yield* Command.Dev("Dev", { |
| 231 | command: `${runtime} ${servePath}`, |
| 232 | cwd: path.dirname(servePath), |
| 233 | env: { |
| 234 | ...props.env, |
| 235 | PORT: "0", |
| 236 | HOST: "127.0.0.1", |
| 237 | ALCHEMY_BUILD_HASH: build.hash.output as unknown as string, |
| 238 | }, |
| 239 | }).pipe(Namespace.push(id)); |
| 240 | return { ...empty(), url: Output.map(dev.url, (value) => value) }; |
| 241 | } |
| 242 | |
| 243 | const main = servePath; |
| 244 | |
| 245 | const app = |
| 246 | props.app !== undefined |
| 247 | ? yield* resolveRef(props.app) |
| 248 | : yield* App("App").pipe(Namespace.push(id)); |
| 249 | |
| 250 | const ip = yield* IpAssignment("Shared", { app, type: "shared_v4" }).pipe(Namespace.push(id)); |
| 251 | |
| 252 | // Serve the built tree from the Machine. Fly Tigris `statics` on |
| 253 | // `urlPrefix: "/"` do not rewrite HTML routes and hang GET `/`. |
| 254 | // Hashed `/assets` still go to Tigris on FrameworkSite. |
| 255 | const service = yield* Service(id, { |
| 256 | app, |
| 257 | deploy: props.deploy, |
| 258 | shutdown: props.shutdown, |
| 259 | checks: props.checks, |
| 260 | services: props.services, |
| 261 | main, |
| 262 | port: DEFAULT_PORT, |
| 263 | // Generated static-file server is a complete bun/node program. |
| 264 | isExternal: true, |
| 265 | env: props.env, |
| 266 | extraFiles: [ |
| 267 | { |
| 268 | // Keep the build dependency so planning cannot hash the previous artifact. |
| 269 | source: Output.map(build.outdir, (dir) => path.resolve(initialCwd, dir)), |
| 270 | dest: path.basename(outdir), |
| 271 | }, |
| 272 | ], |
| 273 | }); |
| 274 | |
| 275 | const domainName = websiteDomainName(props.domain); |
| 276 | const certificate = |
| 277 | domainName !== undefined |
| 278 | ? yield* Certificate("Certificate", { |
| 279 | app, |
| 280 | hostname: domainName, |
| 281 | kind: "acme", |
| 282 | }).pipe(Namespace.push(id)) |
| 283 | : undefined; |
| 284 | if (props.domain !== undefined && certificate !== undefined) { |
| 285 | yield* publishWebsiteDomainDns({ |
| 286 | domain: props.domain, |
| 287 | certificate, |
| 288 | ip, |
| 289 | }).pipe(Namespace.push(id)); |
| 290 | } |
| 291 | |
| 292 | const url = domainName !== undefined ? `https://${domainName}` : app.url; |
| 293 | |
| 294 | return { url, app, service, ip, certificate }; |
| 295 | }).pipe(Effect.orDie); |