Files
ipal-kit/src/modules/seed/exitGuard.ts
T
Radosław SmolińskiandClaude Opus 5.5 f789e36636 Seeds: guardSeedExit() — a run that ends early exits 1 with the reason, never a silent 0
payload run starts the CLI with `void start()` and imports the script; when the script's top-level await
is left without live handles, Node exits with code 0 and no output — a success in Coolify's Terminal.
guardSeedExit() (@intecion/ipal-kit/seed, dependency-free) listens for beforeExit: until summary() runs it
prints "[seed] ended before Payload initialized" / "… before the seed finished" and exits 1. createSeeder
marks the run initialized, summary() finished; process.exit paths (normal, errors, the production
refusals) are untouched. Wired into the ipal-site and ipal-form seed templates and the reference seed, so
ipal-kit create sites get it. Tests include a real child process started like payload run.
Docs: seeding.md, deployment.md §4.7, AGENT.md, ipal-reference (+ api.md), CHANGELOG Unreleased.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-29 19:19:14 +01:00

88 lines
3.1 KiB
TypeScript

/**
* A seed run that ends early must fail loudly. `payload run` starts the CLI with `void start()` and then
* `await import(script)`: when a seed script's top-level `await getPayload()` (or a later await) is left with
* nothing that keeps Node's event loop alive, Node simply exits — code 0, no output. In Coolify's Terminal
* that looks exactly like a successful run. The guard turns it into exit 1 with the reason.
*
* Dependency-free on purpose: no Payload, no Node imports — safe to import first in any seed script.
*/
type ExitListener = () => void
/** The bits of `process` the guard uses — injectable for tests. */
export type SeedExitProcess = {
exit: (code: number) => void
exitCode?: null | number | string
off: (event: 'beforeExit', listener: ExitListener) => unknown
on: (event: 'beforeExit', listener: ExitListener) => unknown
}
export type SeedExitGuardOptions = {
/** Prefix of the message. Default '[seed]'. */
label?: string
/** Where the message goes. Default stderr. */
log?: (line: string) => void
/** Default the real `process`. */
process?: SeedExitProcess
}
export type SeedExitGuard = {
/** Stops watching (the run decides its exit code itself). */
dispose: () => void
/** The seed ended: `summary()` calls it. */
finished: () => void
/** Payload is up: `createSeeder` calls it. */
initialized: () => void
readonly state: 'finished' | 'initialized' | 'waiting'
}
let active: null | SeedExitGuard = null
/** The guard of the current run, if the script started one (`createSeeder` and `summary()` report to it). */
export function activeSeedExitGuard(): null | SeedExitGuard {
return active
}
/**
* Call it first in a seed script, before `getPayload()`. Until `summary()` runs, an exit through an empty
* event loop prints "[seed] ended before Payload initialized" (or "… before the seed finished") and exits 1.
* A normal `process.exit(code)` — after the summary, on an error, or the guard's own refusal — is untouched.
*/
export function guardSeedExit(options: SeedExitGuardOptions = {}): SeedExitGuard {
const proc: SeedExitProcess = options.process ?? process
const label = options.label ?? '[seed]'
const log = options.log ?? ((line: string) => {process.stderr.write(`${line}\n`)})
let state: SeedExitGuard['state'] = 'waiting'
const onBeforeExit = () => {
if (state === 'finished') {return}
const when = state === 'waiting' ? 'before Payload initialized' : 'before the seed finished'
log(
`${label} ended ${when} — the run stopped early and nothing after that point was written. ` +
'Exit code 1. Run it again; if it repeats, check the database connection and the seed script.',
)
proc.exitCode = 1
proc.exit(1)
}
const guard: SeedExitGuard = {
dispose: () => {
proc.off('beforeExit', onBeforeExit)
if (active === guard) {active = null}
},
finished: () => {
state = 'finished'
},
initialized: () => {
if (state === 'waiting') {state = 'initialized'}
},
get state() {
return state
},
}
active?.dispose()
proc.on('beforeExit', onBeforeExit)
active = guard
return guard
}