문제: `tests/e2e/support/service-worker-builds.ts`가 checkout 복사, node modules 연결, 빌드 실행(`run`), 한 번 치환(`replaceOnce`)을 `fixture-builds.ts`와 따로 가졌다. 한쪽을 고친 변경(로컬 빌드 env 1103/11-08, 복사 제외 목록 1105/12-04)을 두 번 해야 했다. 고친 것: `fixture-builds.ts`에 `buildEditedCopies` 하나를 두고(복사 → `prepare` 한 번 → 빌드마다 `edit`·빌드·`dist-<name>`·`collect`), `prepareFixtureBuilds`와 `prepareServiceWorkerBuilds`가 그것을 쓴다. `replaceOnce`는 `editOnce`를 가리킨다. 준비 중 실패한 사본은 전처럼 `sharedFixtureBuild`가 지운다. 검증: desktop `tests/unit/e2e-build-helpers.test.ts` 통과, SW update·recovery e2e와 fixture 빌드 e2e는 FE-GATE-008에서 본다. Refs: 0929/11-07
147 lines
5.4 KiB
TypeScript
147 lines
5.4 KiB
TypeScript
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<Name extends string> = Readonly<{
|
|
name: Name;
|
|
edit?: (root: string) => Promise<void>;
|
|
/** Extra build variables for this build only (e.g. `APP_PROFILE`). */
|
|
env?: Readonly<Record<string, string>>;
|
|
}>;
|
|
|
|
export type FixtureBuilds<Name extends string> = Readonly<{
|
|
root: string;
|
|
dist: Readonly<Record<Name, string>>;
|
|
/** 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<void> {
|
|
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<Record<string, string>> = {},
|
|
): 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 type EditedCopyBuild<Name extends string> = Readonly<{
|
|
name: Name;
|
|
/** VITE_BUILD_ID and RELEASE_ID of this build. */
|
|
buildId: string;
|
|
edit?: (root: string) => Promise<void>;
|
|
env?: Readonly<Record<string, string>>;
|
|
}>;
|
|
|
|
/**
|
|
* 0929/11-07. The one way an e2e spec gets real production builds of an
|
|
* edited copy of the checkout; prepareFixtureBuilds and
|
|
* prepareServiceWorkerBuilds (service-worker-builds.ts) are its two shapes.
|
|
* It copies the checkout (without `checkout-copy-skip`), links node_modules,
|
|
* runs `prepare` once, then for each build runs `edit`, builds, moves `dist`
|
|
* to `dist-<name>` and asks `collect` what the spec needs from that build.
|
|
* `key` and `fingerprint` name the result in the e2e run
|
|
* (shared-fixture-builds.ts), which removes the copy when preparing fails.
|
|
*/
|
|
export async function buildEditedCopies<Name extends string, Collected = null>(
|
|
input: Readonly<{
|
|
key: string;
|
|
fingerprint: string;
|
|
prepare?: (root: string) => Promise<void>;
|
|
builds: readonly EditedCopyBuild<Name>[];
|
|
collect?: (name: Name, root: string) => Promise<Collected>;
|
|
}>,
|
|
): Promise<Readonly<{
|
|
root: string;
|
|
dist: Readonly<Record<Name, string>>;
|
|
collected: Readonly<Record<Name, Collected>>;
|
|
shared: boolean;
|
|
}>> {
|
|
const { root, value, shared } = await sharedFixtureBuild(
|
|
input.key,
|
|
input.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);
|
|
await input.prepare?.(root);
|
|
const dist = {} as Record<Name, string>;
|
|
const collected = {} as Record<Name, Collected>;
|
|
for (const build of input.builds) {
|
|
await build.edit?.(root);
|
|
run(root, "scripts/generate-product-feature-index.ts", build.buildId, build.env);
|
|
run(root, "scripts/build-frontend.ts", build.buildId, build.env);
|
|
dist[build.name] = path.join(root, `dist-${build.name}`);
|
|
await rename(path.join(root, "dist"), dist[build.name]);
|
|
collected[build.name] = input.collect ? await input.collect(build.name, root) : (null as Collected);
|
|
}
|
|
return { dist, collected };
|
|
},
|
|
);
|
|
return { root, ...value, shared };
|
|
}
|
|
|
|
export async function prepareFixtureBuilds<Name extends string>(
|
|
prefix: string,
|
|
builds: readonly FixtureBuild<Name>[],
|
|
): Promise<FixtureBuilds<Name>> {
|
|
// An edit's source text names the build (two specs never share a prefix).
|
|
const { root, dist, shared } = await buildEditedCopies({
|
|
key: `fixture-${prefix}`,
|
|
fingerprint: JSON.stringify(
|
|
builds.map((build) => [build.name, build.env ?? {}, build.edit?.toString() ?? ""]),
|
|
),
|
|
builds: builds.map((build) => ({ ...build, buildId: `${prefix}-${build.name}` })),
|
|
});
|
|
return { root, dist, shared };
|
|
}
|
|
|
|
export async function removeFixtureBuilds(
|
|
builds: Readonly<{ root: string; shared: boolean }> | undefined,
|
|
): Promise<void> {
|
|
if (builds && !builds.shared) await rm(builds.root, { recursive: true, force: true });
|
|
}
|