feat: run startup export from scenarios/index.ts automatically on server init#1833
feat: run startup export from scenarios/index.ts automatically on server init#1833pmcelhaney merged 8 commits intomainfrom
startup export from scenarios/index.ts automatically on server init#1833Conversation
startup export from scenarios/index.ts automatically on server init
pmcelhaney
left a comment
There was a problem hiding this comment.
The pattern should cover the use of scenarios in general, not just startup.
Done in commit 4e22d10. Replaced the narrow "Seed Data on Startup" pattern doc with a broader "Scenario Scripts" pattern (
Updated the patterns index and the REPL feature doc cross-links accordingly. |
pmcelhaney
left a comment
There was a problem hiding this comment.
Rename ApplyContext to Scenario$
|
@copilot Rename ApplyContext to Scenario$ |
Done in commit bc4da17. Renamed
|
Already completed in commit bc4da17 — |
|
@copilot fix merge conflicts |
Head branch was pushed to by a user without write access
Resolved in commit f51cd32. Three-way merge with
All 22 tests pass, lint clean. |
Summary
Adds a
startuplifecycle hook to scenario scripts. Exporting a function namedstartupfromscenarios/index.tscauses it to run automatically after modules load, right before the REPL prompt appears — no manual.scenariocommand required. Skipped silently if absent.Also adds a new Scenario Scripts pattern doc covering the full lifecycle of scenario scripts: writing them, running them on demand with
.scenario, thestartupautomatic hook, and delegating to helper functions with extra arguments.The context type passed to scenario functions has been renamed from
ApplyContexttoScenario$throughout — in the generatedtypes/_.context.ts(merged from themake-applycontext-type-availablePR), in all docs code examples, and in the internal implementation. The generated type file also now includes aLoadContextDefinitionsintermediate interface for typedloadContext()overloads and aContext$interface for_.context.tsroute files.Original Prompt
A scenario in index.ts called startup should run when the server initializes, right before the REPL starts. It will be used to seed dummy data.
If the function doesn't exist, skip.
Update the docs.
Also create a new pattern. Note that startup() can call other scenario functions and that is recommended to separate concerns and keep the startup function clean. It will need to pass the $ arg to the other functions.
Note that it can configure the other functions by passing additional arguments, e.g.
addPets($, 20, "dog")
Manual acceptance tests
export const startup: Scenario = ($) => { ... }inscenarios/index.tshas context seeded before the REPL prompt appearsstartupexport starts normally with no error or warningstartupcan delegate to a helper —addPets($, 20, "dog")— and the helper's changes appear in the running contextstartupfunctions are awaited before the REPL startsstartupstub inscenarios/index.tsScenario$(notApplyContext) work correctly when called fromstartupand from the REPLTasks
src/app.ts: ExportedrunStartupScenario(scenarioRegistry, contextRegistry, config, openApiDocument?)— looks upstartupin theindexmodule, builds aScenario$({ context, loadContext, route, routes }), and calls it; called insidestart()aftermoduleLoader.load().src/typescript-generator/generate.ts: Updated the scaffoldedscenarios/index.tstemplate to include a documentedstartupstub with JSDoc explaining delegation and extra-args patterns. Generated interface renamed fromApplyContexttoScenario$; internal helpers renamedwriteApplyContextType→writeScenarioContextTypeandbuildApplyContextContent→buildScenarioContextContent. Type output file updated totypes/_.context.ts; addedLoadContextDefinitionsinterface andContext$interface to the generated output.test/app.test.ts: Five new unit tests covering: called when present, receives correct$, no-op with missing module, no-op whenstartupis not a function, awaits async functions.test/typescript-generator/generate.test.ts: Updated assertions to expectScenario$,Context$, andLoadContextDefinitionsin the generatedtypes/_.context.ts.docs/features/repl.md: Added "Startup scenario" section with basic and delegation examples; updated cross-link to point to the new Scenario Scripts pattern; updated type annotations toScenario$and import paths totypes/_.context.js.docs/patterns/scenario-scripts.md: New broad pattern doc — "Scenario Scripts" — covering writing scenarios, running them with.scenario(including path-resolution table), thestartupautomatic hook, and the helper-delegation pattern with extra arguments. UsesScenario$andtypes/_.context.jsin all code examples.docs/patterns/index.md: Replaced the narrow "Seed Data on Startup" entry with "Scenario Scripts" in both the prose and the pattern table.docs/adr/001-apply-command-with-function-injection.md: Updated code examples and prose to useScenario$instead ofApplyContextand import fromtypes/_.context.js.origin/maininto branch; resolved conflicts ingenerate.ts,repl.md, andgenerate.test.tsto integrate theLoadContextDefinitions/Context$structure andtypes/_.context.tsoutput path introduced by PR Export Context$ and Scenario$ interfaces for strongly-typed loadContext() in _.context.ts files #1822.