import { spawnSync } from "node:child_process"; import { cp, readFile, rename, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { localBuildEnvironment } from "../../../scripts/lib/build-environment.ts"; import { linkFixtureNodeModules } from "../../../scripts/lib/fixture-node-modules.ts"; import { selectRepositoryPaths } from "../../../scripts/lib/repository-layout.ts"; import { sharedFixtureBuild } from "./shared-fixture-builds.ts"; /** * Real production builds of an edited temp copy of the repository, in the * template's default configuration (`serviceWorker: null`). `edit` changes the * copy right before that build runs, and edits persist into later builds. The * working tree and its `dist` (served by the e2e preview server) are never * touched. Serve the result with `startBuildServer` from * `service-worker-builds.ts`. */ const repoRoot = path.resolve(import.meta.dirname, "../../.."); const skipped = new Set(selectRepositoryPaths("checkout-copy-skip")); export type FixtureBuild = Readonly<{ name: Name; edit?: (root: string) => Promise; /** Extra build variables for this build only (e.g. `APP_PROFILE`). */ env?: Readonly>; }>; export type FixtureBuilds = Readonly<{ root: string; dist: Readonly>; /** One copy for the whole e2e run (shared-fixture-builds.ts); the run removes it. */ shared: boolean; }>; /** Replaces the one occurrence of `search` in `file` of the copy. */ export async function editOnce( file: string, search: string, replacement: string, ): Promise { const source = await readFile(file, "utf8"); if (source.split(search).length !== 2) { throw new Error(`Expected exactly one "${search}" in ${file}.`); } await writeFile(file, source.replace(search, replacement)); } function run( cwd: string, script: string, buildId: string, env: Readonly> = {}, ): void { const result = spawnSync("node", [script], { cwd, encoding: "utf8", // 1103/11-08. A local build, not the CI job's release build. env: { ...localBuildEnvironment(), ...env, VITE_BUILD_ID: buildId, RELEASE_ID: buildId }, }); if (result.status !== 0) { throw new Error( `${script} failed for ${buildId}:\n${result.stdout}\n${result.stderr}`, ); } } export async function prepareFixtureBuilds( prefix: string, builds: readonly FixtureBuild[], ): Promise> { // An edit's source text names the build (two specs never share a prefix). const fingerprint = JSON.stringify( builds.map((build) => [build.name, build.env ?? {}, build.edit?.toString() ?? ""]), ); const { root, value, shared } = await sharedFixtureBuild( `fixture-${prefix}`, fingerprint, async (root) => { await cp(repoRoot, root, { recursive: true, filter: (source) => !skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!), }); await linkFixtureNodeModules(root, repoRoot); const dist = {} as Record; for (const build of builds) { await build.edit?.(root); const buildId = `${prefix}-${build.name}`; run(root, "scripts/generate-product-feature-index.ts", buildId, build.env); run(root, "scripts/build-frontend.ts", buildId, build.env); dist[build.name] = path.join(root, `dist-${build.name}`); await rename(path.join(root, "dist"), dist[build.name]); } return dist; }, ); return { root, dist: value, shared }; } export async function removeFixtureBuilds( builds: Readonly<{ root: string; shared: boolean }> | undefined, ): Promise { if (builds && !builds.shared) await rm(builds.root, { recursive: true, force: true }); }