Repository navigation
Description
Describe the feature
nitro/builder covers dev and build well for tools that drive Nitro programmatically (createNitro with an inline config, then prepare / copyPublicAssets / prerender / build / createDevServer). preview and deploy have no equivalent: their logic lives only in the citty commands under src/cli/commands/, so a wrapper CLI that wants to offer <tool> preview / <tool> deploy has to spawn the nitro bin instead of calling an API.
Spawning works but has rough edges:
- The wrapper cannot pass its inline config; the Nitro CLI resolves everything from
rootDiragain. Fordeploythis means the wrapper must run its own build and then callnitro deploy --prebuilt. getBuildInfo(rootDir)finds the output vianode_modules/.nitro/last-build.jsonunderrootDir, falling back to<rootDir>/.output. A tool that rebasesoutput.dirsomewhere else must pick arootDirfor the spawned CLI that happens to make the fallback resolve correctly.- Preset-specific behaviour tied to the CLI flow (
globalThis.__nitroDeploying__/__nitroDeployed__, used by presets that deploy from a build hook) is not reachable from an API.
Suggested additions to nitro/builder:
- Export
startPreview(src/preview.ts). It already has a clean shape ({ rootDir, loader }→{ fetch, upgrade, close }) and is used by both the CLI command and the Vite preview integration, so this is mostly an export. - A
deployhelper extracted fromsrc/cli/commands/deploy.ts, e.g.deploy({ rootDir } | nitro, { args?: string[] }): resolve the build info, error when the preset has nocommands.deploy, rewrite the./paths relative tooutputDir, and run the command. The CLI command would become a thin wrapper around it, and the deploying/deployed globals could be set there so preset hooks keep working when called via the API. - Optionally let
getBuildInfo/startPreviewaccept an explicitoutputDirso wrappers that rebase the output do not depend onlast-build.jsonlookup.
Alternatives considered: spawning the nitro bin from the wrapper (works today, with the caveats above), or reimplementing the ~20 lines of deploy logic in the wrapper (drifts from Nitro's behaviour and misses the preset globals).
Happy to open a PR if this direction sounds right.
This issue was drafted with the help of an AI assistant, reviewed by a human before posting.
Additional information
- Would you be willing to help implement this feature?
Activity