|
| 1 | +import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs" |
| 2 | +import { tmpdir } from "node:os" |
| 3 | +import { delimiter, join, resolve } from "node:path" |
| 4 | +import { spawnSync } from "node:child_process" |
| 5 | + |
| 6 | +const projectRoot = resolve(import.meta.dirname, "..") |
| 7 | +const baselinePath = process.env.PAYLOAD_SCHEMA_BASELINE_FILE ? resolve(projectRoot, process.env.PAYLOAD_SCHEMA_BASELINE_FILE) : resolve(projectRoot, "src/payload-schema.snapshot") |
| 8 | +const temporaryDirectory = mkdtempSync(join(tmpdir(), "cygnus-payload-schema-")) |
| 9 | +const generatedPath = join(temporaryDirectory, "payload-schema.snapshot") |
| 10 | +const payloadBin = resolve(projectRoot, "node_modules/payload/bin.js") |
| 11 | + |
| 12 | +function normalizedFile(path) { |
| 13 | + return readFileSync(path, "utf8").replaceAll("\r\n", "\n").trim() |
| 14 | +} |
| 15 | + |
| 16 | +try { |
| 17 | + const result = spawnSync(process.execPath, [payloadBin, "generate:db-schema", "--log=false", "--prettify=false"], { |
| 18 | + cwd: projectRoot, |
| 19 | + encoding: "utf8", |
| 20 | + env: { |
| 21 | + ...process.env, |
| 22 | + PATH: `${resolve(projectRoot, "node_modules/.bin")}${delimiter}${process.env.PATH ?? ""}`, |
| 23 | + PAYLOAD_SCHEMA_OUTPUT_FILE: generatedPath, |
| 24 | + }, |
| 25 | + timeout: 120_000, |
| 26 | + }) |
| 27 | + |
| 28 | + if (result.error) throw result.error |
| 29 | + |
| 30 | + if (result.status !== 0) { |
| 31 | + process.stderr.write(result.stdout ?? "") |
| 32 | + process.stderr.write(result.stderr ?? "") |
| 33 | + throw new Error(`Payload schema generation failed with exit code ${result.status ?? "unknown"}.`) |
| 34 | + } |
| 35 | + |
| 36 | + if (!existsSync(generatedPath)) { |
| 37 | + process.stderr.write(result.stdout ?? "") |
| 38 | + process.stderr.write(result.stderr ?? "") |
| 39 | + throw new Error( |
| 40 | + [ |
| 41 | + `Payload did not create the temporary schema at ${generatedPath}.`, |
| 42 | + "Ensure postgresAdapter.generateSchemaOutputFile uses PAYLOAD_SCHEMA_OUTPUT_FILE when it is set.", |
| 43 | + ].join("\n") |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + let baseline |
| 48 | + try { |
| 49 | + baseline = normalizedFile(baselinePath) |
| 50 | + } catch { |
| 51 | + throw new Error(`Missing schema baseline at ${baselinePath}. Create the required migration and refresh the schema baseline.`) |
| 52 | + } |
| 53 | + |
| 54 | + if (normalizedFile(generatedPath) !== baseline) { |
| 55 | + console.error( |
| 56 | + [ |
| 57 | + "", |
| 58 | + "ERROR: Payload schema differs from the latest committed migration baseline.", |
| 59 | + "A Payload field may have been changed without creating a migration.", |
| 60 | + "", |
| 61 | + "Create and verify a migration:", |
| 62 | + " npm run migrate:create -- <migration-name>", |
| 63 | + "", |
| 64 | + "Then refresh the baseline:", |
| 65 | + " npm run migration:snapshot", |
| 66 | + "", |
| 67 | + ].join("\n") |
| 68 | + ) |
| 69 | + process.exitCode = 1 |
| 70 | + } else { |
| 71 | + console.log("Payload migration check passed: schema and migration baseline match.") |
| 72 | + } |
| 73 | +} finally { |
| 74 | + rmSync(temporaryDirectory, { recursive: true, force: true }) |
| 75 | +} |
0 commit comments