The best way to control Claude Code, Codex and more from any computer or phone

main
ts 169 lines 6.32 KB
Raw
1 import type { InputProps } from "alchemy/Input";
2 import * as Namespace from "alchemy/Namespace";
3 import { resolveAstroOutput } from "alchemy/Website/AstroOutput";
4 import * as Effect from "effect/Effect";
5 import { makeFrameworkSite, type FrameworkSiteProps } from "./FrameworkSite.ts";
6
7 /** The framework-integration package that drives the Astro build. */
8 export const ASTRO_FRAMEWORK_SPECIFIER = "@alchemy.run/frontend-frameworks/astro";
9
10 /** The AWS Lambda deploy target for the Astro build. */
11 export const ASTRO_AWS_TARGET_SPECIFIER = "@alchemy.run/frontend-frameworks/astro/aws";
12
13 /** The Node deploy target for the Lambda Web Function server (the default). */
14 export const ASTRO_WEB_TARGET_SPECIFIER = "@alchemy.run/frontend-frameworks/astro/node";
15
16 export type AstroProps = FrameworkSiteProps<{
17 // Astro is configured in YOUR `astro.config.*`, which loads natively —
18 // this resource only deploys it. The `astro` bag is a deploy-time
19 // overlay merged OVER the file for values a config file cannot hold
20 // (per-stage values, other resources' Outputs).
21 /**
22 * Deploy-time Astro config overrides, merged OVER your natively-loaded
23 * `astro.config.*` (values here win). Use it for values that vary per
24 * stage or derive from other resources' Outputs — everything else
25 * belongs in the config file.
26 */
27 astro?: {
28 /** Deployed URL origin (astro's `site`). */
29 site?: string;
30 /** Base path the site is served from (astro's `base`). */
31 base?: string;
32 /**
33 * Astro output target — a deploy-topology decision (whether a
34 * server function exists). `"server"` renders pages on demand in
35 * the Lambda; individual pages opt into prerendering with
36 * `export const prerender = true`. `"static"` prerenders every
37 * page at build time and deploys assets-only (no Lambda).
38 * Overrides the `output` in your `astro.config.*`; leave it unset to
39 * use the file's value.
40 * @default the config file's `output`, else `"server"`
41 */
42 output?: "server" | "static";
43 /** Source directory (astro's `srcDir`). */
44 srcDir?: string;
45 /** Public assets directory (astro's `publicDir`). */
46 publicDir?: string;
47 /** Build output directory (astro's `outDir`). */
48 outDir?: string;
49 /** Trailing-slash handling (astro's `trailingSlash`). */
50 trailingSlash?: "always" | "never" | "ignore";
51 };
52 /**
53 * Path to an alternate astro config file, relative to `rootDir`.
54 * Defaults to astro's own config discovery.
55 */
56 config?: string;
57 /**
58 * Serve the built error page (e.g. astro's `404.html`) for requests that
59 * match no uploaded file. Only applies to `output: "static"`
60 * sites — a server-backed site forwards misses to the Lambda instead.
61 */
62 errorPage?: string;
63 /**
64 * Answer misses with the index page (200) instead of a 404. Only applies
65 * to `output: "static"` sites.
66 */
67 spa?: boolean;
68 }>;
69
70 /**
71 * Deploy an [Astro](https://astro.build) application to AWS: the server
72 * bundle on a Lambda Web Function, static assets (prerendered pages
73 * included) in S3, and a CloudFront distribution whose edge router serves
74 * uploaded files from S3 and forwards everything else to the server.
75 *
76 * The server runs on a Lambda Web Function: the framework's Node server
77 * (built with the `@alchemy.run/frontend-frameworks/astro/node` deploy
78 * target) serves many requests per environment and streams responses. Set
79 * `server: "function"` to use a Lambda Function behind a streaming Function
80 * URL instead, built with the `/aws` target below, for Regions without Web
81 * Functions.
82 *
83 * The build runs through `@alchemy.run/frontend-frameworks/astro` with the
84 * `@alchemy.run/frontend-frameworks/astro/aws` deploy target (a wrangler-free
85 * AWS Lambda adapter is injected — your `astro.config.*` must not declare
86 * one) — the package must be installed in your project.
87 *
88 * Pages render on demand by default (`output: "server"`); pages that
89 * `export const prerender = true` are prerendered at build time and served
90 * from S3. When your `astro.config.*` or `astro: { output: "static" }`
91 * declares a static site, every page is prerendered and the deploy is
92 * assets-only — no Lambda.
93 *
94 * Your `astro.config.*` is the home for Astro configuration and loads
95 * natively; the `astro` prop is a deploy-time override bag merged OVER
96 * the file for values that vary per stage or derive from other
97 * resources' Outputs. `config` points at an alternate config file
98 * (relative to `rootDir`).
99 *
100 * ### Creating Astro Sites
101 * **Example:** Basic Astro App
102 * ```typescript
103 * const site = yield* AWS.Website.Astro("Web", {
104 * rootDir: "./app",
105 * });
106 * ```
107 *
108 * **Example:** Per-stage site URL override
109 * ```typescript
110 * const site = yield* AWS.Website.Astro("Blog", {
111 * rootDir: "./blog",
112 * astro: { site: "https://blog.example.com" },
113 * });
114 * ```
115 *
116 * **Example:** Custom Domain
117 * ```typescript
118 * const site = yield* AWS.Website.Astro("Web", {
119 * rootDir: "./app",
120 * domain: {
121 * name: "app.example.com",
122 * hostedZoneId: zone.hostedZoneId,
123 * },
124 * });
125 * ```
126 *
127 * ### Static Sites
128 * **Example:** Fully Static Astro Site
129 * ```typescript
130 * const site = yield* AWS.Website.Astro("Docs", {
131 * rootDir: "./docs",
132 * astro: { output: "static" },
133 * errorPage: "404.html",
134 * });
135 * ```
136 *
137 * ### Server Configuration
138 * **Example:** Tune The Server Function
139 * ```typescript
140 * const site = yield* AWS.Website.Astro("Web", {
141 * rootDir: "./app",
142 * memorySize: 2048,
143 * env: {
144 * API_BASE: api.url,
145 * },
146 * });
147 * ```
148 *
149 * @resource
150 */
151 export const Astro = (id: string, props: InputProps<AstroProps, "server"> = {}) => {
152 const p = props as AstroProps;
153 // The prop wins, then the config file's `output`, then `"server"` —
154 // astro's own zero-config default is `"static"`. Resolved here because
155 // an assets-only site creates no Lambda.
156 return resolveAstroOutput({ rootDir: p.rootDir, config: p.config, output: p.astro?.output }).pipe(
157 Effect.flatMap((output) =>
158 makeFrameworkSite(id, props, {
159 name: "Astro",
160 framework: ASTRO_FRAMEWORK_SPECIFIER,
161 target: ASTRO_AWS_TARGET_SPECIFIER,
162 webTarget: ASTRO_WEB_TARGET_SPECIFIER,
163 options: { astro: { ...p.astro, output }, config: p.config },
164 static: output === "static" ? { spa: p.spa, errorPage: p.errorPage } : undefined,
165 }),
166 ),
167 Namespace.push(id),
168 );
169 };