import { spawnSync } from "node:child_process"; import path from "node:path"; import { pathToFileURL } from "node:url"; /** * 2026-11-07. The template ships no feature. The browser checks that one * feature works end to end (its route, form and save, a save whose result is * lost) run on the feature `create:feature` writes: a fixture build * (fixture-builds.ts) gets it with `addGeneratedFeature` as its edit, so a * change of the generator's output fails here. The `--keyed` write slice is * the widest one the command makes. */ export const GENERATED_FEATURE_ID = "order-feature"; export type GeneratedRoute = Readonly<{ routeId: string; path: string; chunkId: string; /** The lazy page's module id (`routeRuntimeContracts`), the name of its chunk. */ moduleId: string; }>; export type GeneratedFeature = Readonly<{ list: GeneratedRoute; detail: GeneratedRoute; create: GeneratedRoute; /** The feature's ko-KR and en-US catalogs. */ messages: Readonly>>>; /** The collection the operations call: API_BASE_URL of the build plus the path. */ api: string; }>; function node(root: string, args: readonly string[]): string { const result = spawnSync(process.execPath, args, { cwd: root, encoding: "utf8" }); if (result.status !== 0) { throw new Error(`node ${args.join(" ")} failed in ${root}:\n${result.stdout}\n${result.stderr}`); } return result.stdout; } /** The edit of a fixture build: `corepack pnpm create:feature order-feature --with-create --keyed`. */ export async function addGeneratedFeature(root: string): Promise { node(root, ["scripts/generate-product-feature-index.ts"]); node(root, ["scripts/create-feature.ts", GENERATED_FEATURE_ID, "--with-create", "--keyed"]); } /** * What the specs need to drive the feature, read from the copy the command * wrote it into (node runs that TypeScript as it is): the three routes, the * words and where its API is. */ export function generatedFeature(root: string): GeneratedFeature { const feature = path.join(root, "src/features", GENERATED_FEATURE_ID); const module = (file: string) => JSON.stringify(pathToFileURL(path.join(feature, file)).href); const source = ` const { ORDER_FEATURE_CONTRACT } = await import(${module("contracts/order-feature-contract.ts")}); const { ORDER_MESSAGE_CATALOGS } = await import(${module("contracts/order-message-catalog.ts")}); const { ORDER_LIST_ITEMS_CONTRACT } = await import(${module("contracts/order-contract-contribution.ts")}); const { API_BASE_URL } = JSON.parse(await (await import("node:fs/promises")).readFile("config/runtime/local.json", "utf8")); const route = (id) => ({ routeId: id, path: ORDER_FEATURE_CONTRACT.routes[id].path, chunkId: ORDER_FEATURE_CONTRACT.routes[id].chunkId, moduleId: ORDER_FEATURE_CONTRACT.routeRuntimeContracts[id].moduleId, }); process.stdout.write(JSON.stringify({ list: route("ORDER_LIST"), detail: route("ORDER_DETAIL"), create: route("ORDER_CREATE"), messages: ORDER_MESSAGE_CATALOGS, api: new URL(ORDER_LIST_ITEMS_CONTRACT.contract.pathTemplate.slice(1), API_BASE_URL).href, })); `; return JSON.parse(node(root, ["--input-type=module", "-e", source])) as GeneratedFeature; } /** A route's path with its one parameter filled in. */ export function routePath(route: GeneratedRoute, id?: string): string { return id === undefined ? route.path : route.path.replace(/:[A-Za-z][A-Za-z0-9_]*/u, id); }