|
| 1 | +import fs from "fs-extra"; |
| 2 | +import path from "path"; |
| 3 | +import { PackageJson } from "type-fest"; |
| 4 | +import * as R from "ramda"; |
| 5 | +import { log } from "../../../log"; |
| 6 | + |
| 7 | +/** |
| 8 | + * A static framework produces a directory of static assets (a SPA) that needs |
| 9 | + * to be served by a static server, instead of running its own node process. |
| 10 | + * |
| 11 | + * - `outputDir` is the build output relative to the project root. |
| 12 | + * - `serveCommand` builds the container start command used when the project |
| 13 | + * does NOT define its own `start` script. It must bind to 0.0.0.0 and the |
| 14 | + * `$PORT` env var (=80, set in the Dockerfile). |
| 15 | + * - `injectServe` is true for frameworks without a bundled preview/serve tool |
| 16 | + * (CRA, Vue, Angular): we install the standalone `serve` package into the |
| 17 | + * workdir before building the image so it ships in node_modules (no runtime |
| 18 | + * download). Vite/Astro/Gatsby ship their own and don't need it. |
| 19 | + * - `resolveOutput` lets a framework compute its output dir dynamically |
| 20 | + * (Angular reads it from angular.json). |
| 21 | + */ |
| 22 | +export interface Framework { |
| 23 | + type: string; |
| 24 | + /** Package names that, if present in (dev)dependencies, identify the framework. */ |
| 25 | + deps: string[]; |
| 26 | + outputDir?: string; |
| 27 | + serveCommand?: (dir: string) => string; |
| 28 | + injectServe?: boolean; |
| 29 | + resolveOutput?: (workdir: string) => string; |
| 30 | +} |
| 31 | + |
| 32 | +const has_dep = (pkg: PackageJson, name: string) => |
| 33 | + Boolean( |
| 34 | + R.view(R.lensPath(["dependencies", name]), pkg) || |
| 35 | + R.view(R.lensPath(["devDependencies", name]), pkg) |
| 36 | + ); |
| 37 | + |
| 38 | +/** |
| 39 | + * Read Angular's build output path from angular.json. Defaults to `dist` when |
| 40 | + * it can't be resolved. Angular ≥17 (application builder) emits into |
| 41 | + * `<outputPath>/browser`, so we append it when the project uses that builder. |
| 42 | + */ |
| 43 | +export const resolve_angular_output = (workdir: string): string => { |
| 44 | + const fallback = "dist"; |
| 45 | + try { |
| 46 | + const angular_json = fs.readJSONSync(path.join(workdir, "angular.json")); |
| 47 | + const projects = angular_json?.projects ?? {}; |
| 48 | + const project_name = |
| 49 | + angular_json?.defaultProject ?? Object.keys(projects)[0]; |
| 50 | + const build = projects?.[project_name]?.architect?.build; |
| 51 | + const output = build?.options?.outputPath as string | undefined; |
| 52 | + if (!output) return fallback; |
| 53 | + |
| 54 | + const builder: string = build?.builder ?? ""; |
| 55 | + const is_application_builder = |
| 56 | + builder.includes("application") || builder.includes("browser-esbuild"); |
| 57 | + return is_application_builder ? path.join(output, "browser") : output; |
| 58 | + } catch { |
| 59 | + return fallback; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * Framework registry, evaluated in order. Order matters: Astro/SvelteKit/CRA |
| 65 | + * pull Vite in transitively, so Vite must be the last static fallback. |
| 66 | + */ |
| 67 | +export const FRAMEWORKS: Framework[] = [ |
| 68 | + // Next.js: handled by its own runtime_strategy/PVC, never static-served here. |
| 69 | + { type: "next", deps: ["next"] }, |
| 70 | + |
| 71 | + { |
| 72 | + type: "astro", |
| 73 | + deps: ["astro"], |
| 74 | + outputDir: "dist", |
| 75 | + serveCommand: (dir) => `npx astro preview --host 0.0.0.0 --port $PORT`, |
| 76 | + }, |
| 77 | + { |
| 78 | + type: "gatsby", |
| 79 | + deps: ["gatsby"], |
| 80 | + outputDir: "public", |
| 81 | + serveCommand: (dir) => `npx gatsby serve --host 0.0.0.0 --port $PORT`, |
| 82 | + }, |
| 83 | + { |
| 84 | + type: "cra", |
| 85 | + deps: ["react-scripts"], |
| 86 | + outputDir: "build", |
| 87 | + injectServe: true, |
| 88 | + serveCommand: (dir) => `npx serve -s ${dir} -l $PORT`, |
| 89 | + }, |
| 90 | + { |
| 91 | + type: "vue", |
| 92 | + deps: ["@vue/cli-service"], |
| 93 | + outputDir: "dist", |
| 94 | + injectServe: true, |
| 95 | + serveCommand: (dir) => `npx serve -s ${dir} -l $PORT`, |
| 96 | + }, |
| 97 | + { |
| 98 | + type: "angular", |
| 99 | + deps: ["@angular/cli", "@angular-devkit/build-angular"], |
| 100 | + injectServe: true, |
| 101 | + resolveOutput: resolve_angular_output, |
| 102 | + serveCommand: (dir) => `npx serve -s ${dir} -l $PORT`, |
| 103 | + }, |
| 104 | + { |
| 105 | + type: "vite", |
| 106 | + deps: ["vite"], |
| 107 | + outputDir: "dist", |
| 108 | + serveCommand: (dir) => `npx vite preview --host 0.0.0.0 --port $PORT`, |
| 109 | + }, |
| 110 | +]; |
| 111 | + |
| 112 | +export interface DetectedFramework { |
| 113 | + type: string; |
| 114 | + /** Container start command to serve the static output, or null if none. */ |
| 115 | + start_command: string | null; |
| 116 | + /** Whether the standalone `serve` package must be injected before build. */ |
| 117 | + inject_serve: boolean; |
| 118 | +} |
| 119 | + |
| 120 | +interface DetectParams { |
| 121 | + pkg: PackageJson; |
| 122 | + workdir: string; |
| 123 | + /** True when package.json defines a `start` script (e.g. custom SSR server). */ |
| 124 | + has_start: boolean; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Detect the framework from package.json and compute how to serve it. |
| 129 | + * |
| 130 | + * When the project defines its own `start` script we never override it (the app |
| 131 | + * ships a real server — custom SSR, Nuxt, Remix, SvelteKit node-adapter, etc.), |
| 132 | + * so `start_command`/`inject_serve` stay neutral. |
| 133 | + */ |
| 134 | +export const detect_framework = (params: DetectParams): DetectedFramework => { |
| 135 | + const { pkg, workdir, has_start } = params; |
| 136 | + |
| 137 | + const framework = FRAMEWORKS.find((fw) => |
| 138 | + fw.deps.some((dep) => has_dep(pkg, dep)) |
| 139 | + ); |
| 140 | + |
| 141 | + if (!framework) { |
| 142 | + return { type: "node", start_command: null, inject_serve: false }; |
| 143 | + } |
| 144 | + |
| 145 | + // Static frameworks only override the start command when the project doesn't |
| 146 | + // ship its own server. |
| 147 | + if (framework.serveCommand && !has_start) { |
| 148 | + const output_dir = framework.resolveOutput |
| 149 | + ? framework.resolveOutput(workdir) |
| 150 | + : framework.outputDir ?? "dist"; |
| 151 | + const start_command = framework.serveCommand(output_dir); |
| 152 | + log.info( |
| 153 | + `No start script on package.json, serving ${framework.type} output (${output_dir}) with [${start_command}]` |
| 154 | + ); |
| 155 | + return { |
| 156 | + type: framework.type, |
| 157 | + start_command, |
| 158 | + inject_serve: Boolean(framework.injectServe), |
| 159 | + }; |
| 160 | + } |
| 161 | + |
| 162 | + return { type: framework.type, start_command: null, inject_serve: false }; |
| 163 | +}; |
0 commit comments