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 { existsSync } from "node:fs"; |
| 2 | import * as NodePath from "node:path"; |
| 3 | import { pathToFileURL } from "node:url"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { FrameworkError } from "../core/Framework.ts"; |
| 6 | import { loadProjectModule, resolveProjectPackageDirectory } from "../core/Loader.ts"; |
| 7 | import { usesCloudflareCacheProvider } from "./cache.ts"; |
| 8 | |
| 9 | /** Astro's own config discovery list, in its lookup order. */ |
| 10 | const CONFIG_FILES = ["astro.config.mjs", "astro.config.js", "astro.config.ts", "astro.config.mts"]; |
| 11 | |
| 12 | /** The `output` an Astro config can declare. */ |
| 13 | export type AstroOutput = "server" | "static"; |
| 14 | |
| 15 | export interface ReadConfigOutputOptions { |
| 16 | /** Directory a relative {@link rootDir} resolves against. Defaults to the process working directory. */ |
| 17 | readonly cwd?: string | undefined; |
| 18 | /** Project root. Defaults to {@link cwd}. */ |
| 19 | readonly rootDir?: string | undefined; |
| 20 | /** |
| 21 | * Path to an alternate Astro config file, relative to {@link rootDir}. |
| 22 | * Defaults to astro's own config discovery. |
| 23 | */ |
| 24 | readonly config?: string | undefined; |
| 25 | } |
| 26 | |
| 27 | interface ViteModule { |
| 28 | readonly loadConfigFromFile: ( |
| 29 | configEnv: { command: "build"; mode: string }, |
| 30 | configFile: string, |
| 31 | configRoot: string, |
| 32 | logLevel: "silent", |
| 33 | customLogger: undefined, |
| 34 | configLoader: "runner", |
| 35 | ) => Promise<{ readonly config: unknown } | null>; |
| 36 | } |
| 37 | |
| 38 | /** The deploy-relevant settings a project's `astro.config.*` declares. */ |
| 39 | export interface AstroConfigSettings { |
| 40 | /** The declared `output`, or `undefined` when the file does not set one. */ |
| 41 | readonly output: AstroOutput | undefined; |
| 42 | /** |
| 43 | * Whether the config routes caching through the Cloudflare cache provider |
| 44 | * (`cache: { provider: cacheCloudflare() }`), which needs Workers Cache |
| 45 | * enabled on the Worker. |
| 46 | */ |
| 47 | readonly workerCache: boolean; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Read the deploy-relevant settings of a project's `astro.config.*` file. |
| 52 | * Without a config file, `output` is `undefined` and `workerCache` is |
| 53 | * `false`. |
| 54 | * |
| 55 | * Deploy composites call this at plan time: whether a site is assets-only |
| 56 | * decides which resources exist, and Astro gives no way to tell an explicit |
| 57 | * file-level `output: "static"` apart from its own `"static"` default once |
| 58 | * the config resolves. The file loads the way Astro loads it — a native |
| 59 | * import for `.js`/`.mjs`, otherwise through the project's Vite module |
| 60 | * runner. |
| 61 | */ |
| 62 | export const readConfigSettings = Effect.fn("Astro.readConfigSettings")(function* ( |
| 63 | options: ReadConfigOutputOptions = {}, |
| 64 | ) { |
| 65 | const root = yield* Effect.sync(() => |
| 66 | NodePath.resolve(options.cwd ?? process.cwd(), options.rootDir ?? "."), |
| 67 | ); |
| 68 | const configPath = yield* Effect.sync(() => |
| 69 | options.config !== undefined |
| 70 | ? NodePath.resolve(root, options.config) |
| 71 | : CONFIG_FILES.map((file) => NodePath.join(root, file)).find((file) => existsSync(file)), |
| 72 | ); |
| 73 | if (configPath === undefined) { |
| 74 | return { output: undefined, workerCache: false } satisfies AstroConfigSettings; |
| 75 | } |
| 76 | const fail = (cause: unknown) => |
| 77 | new FrameworkError({ |
| 78 | framework: "astro", |
| 79 | message: `Failed to load the Astro config at "${configPath}"`, |
| 80 | cause, |
| 81 | }); |
| 82 | const viaVite = resolveProjectPackageDirectory(root, "astro").pipe( |
| 83 | Effect.flatMap((astroDirectory) => loadProjectModule<ViteModule>(astroDirectory, "vite")), |
| 84 | Effect.mapError(fail), |
| 85 | Effect.flatMap((vite) => |
| 86 | Effect.tryPromise({ |
| 87 | try: () => |
| 88 | vite.loadConfigFromFile( |
| 89 | { command: "build", mode: "production" }, |
| 90 | configPath, |
| 91 | root, |
| 92 | "silent", |
| 93 | undefined, |
| 94 | "runner", |
| 95 | ), |
| 96 | catch: fail, |
| 97 | }), |
| 98 | ), |
| 99 | Effect.map((loaded) => loaded?.config), |
| 100 | ); |
| 101 | // Astro imports a JavaScript config natively (cache-busted, so an edited |
| 102 | // config is re-read) and falls back to Vite when that fails. |
| 103 | const config = /\.m?js$/.test(configPath) |
| 104 | ? yield* Effect.tryPromise({ |
| 105 | try: async () => |
| 106 | ( |
| 107 | (await import( |
| 108 | /* @vite-ignore */ `${pathToFileURL(configPath).href}?t=${Date.now()}` |
| 109 | )) as { default?: unknown } |
| 110 | ).default, |
| 111 | catch: fail, |
| 112 | }).pipe(Effect.catch(() => viaVite)) |
| 113 | : yield* viaVite; |
| 114 | const output = |
| 115 | typeof config === "object" && config !== null && "output" in config ? config.output : undefined; |
| 116 | return { |
| 117 | output: output === "server" || output === "static" ? (output as AstroOutput) : undefined, |
| 118 | workerCache: usesCloudflareCacheProvider(config), |
| 119 | } satisfies AstroConfigSettings; |
| 120 | }); |
| 121 | |
| 122 | /** |
| 123 | * Read the `output` declared by a project's `astro.config.*` file, or |
| 124 | * `undefined` when there is no config file or it does not set `output`. |
| 125 | * See {@link readConfigSettings}. |
| 126 | */ |
| 127 | export const readConfigOutput = Effect.fn("Astro.readConfigOutput")(function* ( |
| 128 | options: ReadConfigOutputOptions = {}, |
| 129 | ) { |
| 130 | return (yield* readConfigSettings(options)).output; |
| 131 | }); |