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
| 1 | import { AlchemyContext } from "alchemy/AlchemyContext"; |
| 2 | import * as Command from "alchemy/Command"; |
| 3 | import * as Namespace from "alchemy/Namespace"; |
| 4 | import type * 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 { CustomDomain } from "../CustomDomain.ts"; |
| 12 | import { Project } from "../Project.ts"; |
| 13 | import { Service } from "../Service.ts"; |
| 14 | import { |
| 15 | publishWebsiteDomainDns, |
| 16 | WEBSITE_PORT, |
| 17 | websiteDomainName, |
| 18 | type FrameworkSiteProps, |
| 19 | type Website, |
| 20 | } from "./FrameworkSite.ts"; |
| 21 | |
| 22 | // `env` comes from FrameworkSiteProps (which additionally accepts `Output` |
| 23 | // values, e.g. `VITE_API_URL: api.url`) — it must be omitted from the |
| 24 | // BuildProps side because interfaces cannot extend two parents whose `env` |
| 25 | // members differ. |
| 26 | export interface StaticSiteProps |
| 27 | extends |
| 28 | Omit<Command.BuildProps, "env">, |
| 29 | Pick<FrameworkSiteProps, "project" | "environment" | "domain" | "tags" | "env"> { |
| 30 | /** |
| 31 | * Local dev configuration. When `alchemy dev` runs with `dev.command`, |
| 32 | * the build is skipped and `command` is spawned as a long-lived child |
| 33 | * (`Command.Dev`). Without `dev.command`, the site still builds and a |
| 34 | * local static server serves `outdir` — no Railway Service is created. |
| 35 | */ |
| 36 | dev?: { |
| 37 | /** |
| 38 | * Shell command to run as the local dev server (e.g. `npm run dev`). |
| 39 | */ |
| 40 | command: string; |
| 41 | /** |
| 42 | * Working directory for {@link command}. Defaults to |
| 43 | * {@link Command.BuildProps.cwd}. |
| 44 | */ |
| 45 | cwd?: string; |
| 46 | /** |
| 47 | * Environment variables for {@link command}, merged on top of |
| 48 | * `process.env`. |
| 49 | */ |
| 50 | env?: Record<string, string | Redacted.Redacted<string>>; |
| 51 | /** |
| 52 | * Override for the `url` output if alchemy fails to detect it from |
| 53 | * the stdout of the dev command. |
| 54 | */ |
| 55 | url?: string; |
| 56 | }; |
| 57 | /** |
| 58 | * Answer misses with the index page (200) instead of a 404 so |
| 59 | * client-side routes deep-link. Mutually exclusive with {@link errorPage}. |
| 60 | */ |
| 61 | spa?: boolean; |
| 62 | /** |
| 63 | * Serve this page (e.g. `404.html`) with status 404 when no file |
| 64 | * matches. Mutually exclusive with {@link spa}. |
| 65 | */ |
| 66 | errorPage?: string; |
| 67 | } |
| 68 | |
| 69 | const envRecord = ( |
| 70 | env: |
| 71 | | Record<string, string | Redacted.Redacted<string> | Output.Output<string | undefined>> |
| 72 | | undefined, |
| 73 | ): Record<string, string | Output.Output<string | undefined>> | undefined => { |
| 74 | if (env === undefined) return undefined; |
| 75 | return Object.fromEntries( |
| 76 | Object.entries(env).map(([key, value]) => [ |
| 77 | key, |
| 78 | Redacted.isRedacted(value) ? Redacted.value(value) : value, |
| 79 | ]), |
| 80 | ); |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * A Railway Service that serves static assets built by a shell command. |
| 85 | * |
| 86 | * `StaticSite` runs a build command (e.g. `npm run build`), content-hashes |
| 87 | * the output directory, and deploys the result as a Node static-file |
| 88 | * server on one `Railway.Service`. Use this when your site has its own |
| 89 | * build step that produces a directory of files — Hugo, Zola, Eleventy, or |
| 90 | * any custom pipeline. |
| 91 | * |
| 92 | * For Vite-based projects, prefer {@link Vite | Railway.Website.Vite} |
| 93 | * which handles building automatically. |
| 94 | * |
| 95 | * Cloudflare DX: top-level `command` + `outdir` (`Command.BuildProps`). |
| 96 | * Only `Command.Build("Build")` / `Command.Dev("Dev")` are pushed under |
| 97 | * the site id; the Service stays in the caller namespace. |
| 98 | * |
| 99 | * During `alchemy dev`, `dev.command` skips the build and is the site. |
| 100 | * Without `dev.command`, the site still builds and a local static server |
| 101 | * serves `outdir` — no Project or Service is created. |
| 102 | * |
| 103 | * ### Basic Usage |
| 104 | * **Example:** Deploying a Hugo site |
| 105 | * ```typescript |
| 106 | * const site = yield* Railway.Website.StaticSite("Blog", { |
| 107 | * command: "hugo --minify", |
| 108 | * outdir: "public", |
| 109 | * }); |
| 110 | * ``` |
| 111 | * |
| 112 | * **Example:** SPA-style routing |
| 113 | * ```typescript |
| 114 | * const site = yield* Railway.Website.StaticSite("App", { |
| 115 | * command: "npm run build", |
| 116 | * outdir: "dist", |
| 117 | * spa: true, |
| 118 | * }); |
| 119 | * ``` |
| 120 | * |
| 121 | * ### Building from a Subdirectory |
| 122 | * **Example:** Building a frontend in a monorepo |
| 123 | * ```typescript |
| 124 | * const site = yield* Railway.Website.StaticSite("Web", { |
| 125 | * cwd: "apps/web", |
| 126 | * command: "npm run build", |
| 127 | * outdir: "dist", |
| 128 | * }); |
| 129 | * ``` |
| 130 | * |
| 131 | * ### Local Development |
| 132 | * **Example:** External dev command |
| 133 | * ```typescript |
| 134 | * const site = yield* Railway.Website.StaticSite("App", { |
| 135 | * command: "npm run build", |
| 136 | * outdir: "dist", |
| 137 | * dev: { command: "npm run dev" }, |
| 138 | * }); |
| 139 | * ``` |
| 140 | * |
| 141 | * @resource |
| 142 | * @product Website |
| 143 | */ |
| 144 | export const StaticSite = (id: string, props: StaticSiteProps) => |
| 145 | Effect.gen(function* () { |
| 146 | const ctx = yield* AlchemyContext; |
| 147 | const remoted = yield* ProviderModePolicy; |
| 148 | const isLocal = ctx.dev && remoted !== true; |
| 149 | const path = yield* Path.Path; |
| 150 | |
| 151 | if (isLocal && props.dev !== undefined) { |
| 152 | const dev = yield* Command.Dev("Dev", { |
| 153 | command: props.dev.command, |
| 154 | cwd: props.dev.cwd ?? props.cwd, |
| 155 | env: props.dev.env ?? props.env, |
| 156 | }).pipe(Namespace.push(id)); |
| 157 | return { |
| 158 | url: props.dev.url ?? dev.url, |
| 159 | service: undefined, |
| 160 | project: undefined, |
| 161 | } satisfies Website; |
| 162 | } |
| 163 | |
| 164 | yield* Command.Build("Build", { |
| 165 | command: props.command, |
| 166 | cwd: props.cwd, |
| 167 | memo: props.memo, |
| 168 | outdir: props.outdir, |
| 169 | env: props.env, |
| 170 | shell: props.shell, |
| 171 | timeout: props.timeout, |
| 172 | }).pipe(Namespace.push(id)); |
| 173 | |
| 174 | const cwd = path.resolve(initialCwd, props.cwd ?? "."); |
| 175 | const clientAbs = path.resolve(cwd, props.outdir); |
| 176 | |
| 177 | const { NODE_SERVE_ENTRY_FILE_NAME, relativeClientDirExpression, writeNodeServeEntry } = |
| 178 | yield* loadFrontendCore; |
| 179 | const servePath = path.join(path.dirname(clientAbs), NODE_SERVE_ENTRY_FILE_NAME); |
| 180 | yield* writeNodeServeEntry({ |
| 181 | output: { |
| 182 | clientDirectory: clientAbs, |
| 183 | serverModules: [], |
| 184 | externalWorkspaces: new Set<string>(), |
| 185 | }, |
| 186 | servePath, |
| 187 | serveModuleName: NODE_SERVE_ENTRY_FILE_NAME, |
| 188 | clientDirExpression: relativeClientDirExpression(servePath, clientAbs), |
| 189 | notFoundHandling: |
| 190 | props.errorPage !== undefined ? "404-page" : props.spa === true ? "spa" : "none", |
| 191 | printUrl: isLocal, |
| 192 | platform: "node", |
| 193 | }); |
| 194 | |
| 195 | if (isLocal) { |
| 196 | const runtime = yield* Effect.sync(() => process.execPath); |
| 197 | const dev = yield* Command.Dev("Dev", { |
| 198 | command: `${runtime} ${servePath}`, |
| 199 | cwd: path.dirname(servePath), |
| 200 | env: { ...props.env, PORT: "0", HOST: "127.0.0.1" }, |
| 201 | }).pipe(Namespace.push(id)); |
| 202 | return { url: dev.url, service: undefined, project: undefined } satisfies Website; |
| 203 | } |
| 204 | |
| 205 | const project = Effect.isEffect(props.project) |
| 206 | ? yield* props.project |
| 207 | : (props.project ?? (yield* Project("Project").pipe(Namespace.push(id)))); |
| 208 | const environment = Effect.isEffect(props.environment) |
| 209 | ? yield* props.environment |
| 210 | : (props.environment ?? project); |
| 211 | const service = yield* Service(id, { |
| 212 | project, |
| 213 | environment, |
| 214 | main: servePath, |
| 215 | port: WEBSITE_PORT, |
| 216 | healthcheck: "/health", |
| 217 | isExternal: true, |
| 218 | env: envRecord(props.env), |
| 219 | extraFiles: [{ source: clientAbs, dest: path.basename(clientAbs) }], |
| 220 | }); |
| 221 | |
| 222 | const domainName = websiteDomainName(props.domain); |
| 223 | if (domainName !== undefined && domainName.length > 0) { |
| 224 | const customDomain = yield* CustomDomain("Domain", { |
| 225 | service, |
| 226 | environment, |
| 227 | domain: domainName, |
| 228 | targetPort: WEBSITE_PORT, |
| 229 | }).pipe(Namespace.push(id)); |
| 230 | yield* publishWebsiteDomainDns({ |
| 231 | domain: props.domain, |
| 232 | customDomain, |
| 233 | }).pipe(Namespace.push(id)); |
| 234 | return { |
| 235 | url: `https://${domainName}`, |
| 236 | service, |
| 237 | project, |
| 238 | } satisfies Website; |
| 239 | } |
| 240 | |
| 241 | return { url: service.url, service, project } satisfies Website; |
| 242 | }).pipe(Effect.orDie); |