import { type ChildProcess, spawn } from "node:child_process"; import { createRequire } from "node:module"; import path from "node:path"; import { type PonyProject, pathExists } from "../core/index.js"; import { waitForChildExit } from "./child-process.js"; export const STATIC_WEB_CONFIG = "astro.static.config.mjs"; export const DEFAULT_STATIC_WEB_OUT_DIR = ".pony-web"; export interface StaticBuildOptions { base?: string; outDir?: string; } export interface ResolvedStaticBuildOptions { base: string; outDir: string; } export interface StaticBuildRuntime { pathExists(filePath: string): Promise; resolveAstroCli(): string; spawnProcess( command: string, args: string[], options: { cwd: string; env: NodeJS.ProcessEnv; stdio: "inherit"; }, ): ChildProcess; waitForExit(child: ChildProcess): Promise; } export function normalizeWebBase(base = "/"): string { const trimmed = base.trim(); if (!trimmed || trimmed === "/") return "/"; if ( trimmed.includes("://") || trimmed.includes("?") || trimmed.includes("#") ) { throw new Error("Static web base must be a URL pathname, not a full URL."); } const withLeadingSlash = trimmed.startsWith("/") ? trimmed : `/${trimmed}`; return withLeadingSlash.endsWith("/") ? withLeadingSlash : `${withLeadingSlash}/`; } export function resolveStaticBuildOptions( project: PonyProject, options: StaticBuildOptions, ): ResolvedStaticBuildOptions { const outDir = path.resolve( project.root, options.outDir?.trim() || DEFAULT_STATIC_WEB_OUT_DIR, ); const relativeToProject = path.relative(project.root, outDir); const isProjectRoot = relativeToProject === ""; const isPonyState = outDir === project.ponyDir || outDir.startsWith(`${project.ponyDir}${path.sep}`); if (isProjectRoot || isPonyState) { throw new Error( "Static web output must not overwrite the project root or .pony state.", ); } return { base: normalizeWebBase(options.base), outDir, }; } function resolveAstroCli(): string { const require = createRequire(import.meta.url); const astroPackageJson = require.resolve("astro/package.json"); const astroPackage = require(astroPackageJson) as { bin?: { astro?: string }; }; const binPath = astroPackage.bin?.astro; if (!binPath) { throw new Error("Could not locate the Astro CLI binary."); } return path.resolve(path.dirname(astroPackageJson), binPath); } const defaultStaticBuildRuntime: StaticBuildRuntime = { pathExists, resolveAstroCli, spawnProcess: (command, args, options) => spawn(command, args, options), waitForExit: (child) => waitForChildExit(child, "Static web build"), }; /** * Astro source belongs to the installed Pony package, while the generated * snapshot belongs to the selected workspace. Explicit paths preserve that * boundary for linked, local, and repo-installed Pony packages. */ export async function buildStaticWeb( packageRoot: string, project: PonyProject, options: StaticBuildOptions, env: NodeJS.ProcessEnv = process.env, runtime: StaticBuildRuntime = defaultStaticBuildRuntime, ): Promise { const configPath = path.join(packageRoot, STATIC_WEB_CONFIG); if (!(await runtime.pathExists(configPath))) { throw new Error(`Static web build config is missing at ${configPath}.`); } const resolved = resolveStaticBuildOptions(project, options); const child = runtime.spawnProcess( process.execPath, [ runtime.resolveAstroCli(), "build", "--root", packageRoot, "--config", STATIC_WEB_CONFIG, ], { cwd: packageRoot, env: { ...env, PONY_WEB_BASE: resolved.base, PONY_WEB_OUT_DIR: resolved.outDir, PONY_WORKSPACE_CWD: project.root, }, stdio: "inherit", }, ); await runtime.waitForExit(child); return resolved; }