Repository navigation
feat: add CodeGen to run the codegen without the dev-server - #100
Merged
Merged
Conversation
The dev-server, the bundler and the test runner all generate code as a side effect of doing something else. CodeGen performs the same work on its own, so the contents of the ".adonisjs" directory can be refreshed deterministically. For example, inside a CI pipeline before typechecking or creating a build. The class covers both the codegen stages. The static stage runs the "init" hooks against an index generator and writes the index files. The runtime stage runs the routes hooks against the state of a booted application. Assembler never boots an application, hence the caller boots it from within the "collectSnapshot" callback and returns the state we cannot collect ourselves. The callback is invoked after the index files have been written, because an application imports them from its preload files and therefore cannot be booted any earlier. The sequence for sharing and scanning the routes is now shared with the dev-server using the "processRoutes" util, so the two cannot drift apart.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
CodeGenclass that performs the codegen the dev-server performs, without the server and without the file watcher.Pairs with adonisjs/application#48 and the
node ace codegencommand in core.Why
The dev-server, the bundler and the test runner all generate code as a side effect of doing something else, so there is no way to refresh
.adonisjson its own. That matters in CI, where you want the generated types to be current before typechecking or building, without starting a long running process.Worth noting: only the dev-server loads the routes hooks today. The bundler loads
['init', 'buildStarting', 'buildFinished']and the test runner['init', 'testsStarting', 'testsFinished']. So anything hanging offroutesCommitted/routesScanned— a Tuyau registry, for instance — is only regenerated whilenpm run devis running.The two stages, and why the order is forced
Static stage — run the
inithooks against anIndexGeneratorand write the index files. Only the file-system is needed.Runtime stage — run the routes hooks. This needs state only a booted application knows about.
Assembler never boots an application, so the caller supplies that state. It cannot simply be passed in, because the stages are ordered: an application imports the generated barrel files from its preload files (
start/routes.tsdoesimport { controllers } from '#generated/controllers'), so it cannot be booted until the index files exist. The dev-server generates them before spawning the app for exactly this reason.Hence the callback:
Ordering is structural rather than documented, and there is a test pinning it. Skip the callback to generate the index files alone.
The snapshot is a keyed bag rather than a positional argument, so a second runtime producer later does not change the signature.
Shared with the dev-server
The sequence for sharing and scanning routes was identical in both, so it now lives in
processRoutes/hasRoutesHooksinutils.ts. Each caller keeps its own edges: the dev-server keeps the throttle, the file read, theunlinks and the try/catch that logs and continues;CodeGentakes the routes in memory and lets errors throw, so a command can exit non-zero.Breaking
The hook
parentunions widen —CommonHooks.inittoDevServer | TestRunner | Bundler | CodeGen, and the threeRouterHookstoDevServer | CodeGen. A hook readingDevServer-only members without narrowing will no longer compile.Tests
9 new tests in
tests/codegen.spec.ts, including one that asserts the index files exist by the time the snapshot callback runs.