Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxi): cli wrapper for self restart #18641

Merged
merged 9 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 32 additions & 2 deletions packages/nuxi/bin/nuxi.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
#!/usr/bin/env node
process._startTime = Date.now()
import('../dist/cli.mjs').then(r => (r.default || r).main())
import { fileURLToPath } from 'node:url'
import { execa } from 'execa'
danielroe marked this conversation as resolved.
Show resolved Hide resolved

const EXIT_CODE_RESTART = 44

const ENTRY = new URL('../dist/cli-run.mjs', import.meta.url)

async function start (preArgs, postArgs) {
const child = await execa(
'node',
[
...preArgs,
fileURLToPath(ENTRY),
...postArgs
],
{
reject: false,
stdio: 'inherit',
env: {
...process.env,
NUXI_CLI_WRAPPER: 'true'
}
}
)
if (child.exitCode === EXIT_CODE_RESTART) {
await start(preArgs, postArgs)
} else {
process.exit(child.exitCode)
}
}

start([], process.argv.slice(2))
1 change: 1 addition & 0 deletions packages/nuxi/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default defineBuildConfig({
},
entries: [
'src/cli',
'src/cli-run',
'src/index'
],
externals: [
Expand Down
4 changes: 4 additions & 0 deletions packages/nuxi/src/cli-run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @ts-ignore
process._startTime = Date.now()
// @ts-ignore
import('./cli').then(r => (r.default || r).main())
12 changes: 12 additions & 0 deletions packages/nuxi/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ export async function runCommand (command: string, argv = process.argv.slice(2))
}
await cmd.invoke(args)
}

/**
* Special exit code to restart the process
*
* Usage:
* ```ts
* if (process.env.NUXI_CLI_WRAPPER) {
* process.exit(EXIT_CODE_RESTART)
* }
* ```
*/
export const EXIT_CODE_RESTART = 44