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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --script option to vite-node #2793

Merged
merged 10 commits into from
Feb 11, 2023
26 changes: 26 additions & 0 deletions packages/vite-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ npx vite-node --options.deps.inline="module-name" --options.deps.external="/modu

Note that for options supporting RegExps, strings passed to the CLI must start _and_ end with a `/`;

### Hashbang

If you prefer to write scripts that doesn't need to be passed into Vite Node, you can declare it in the [hashbang](https://bash.cyberciti.biz/guide/Shebang).
jgoux marked this conversation as resolved.
Show resolved Hide resolved

Simply add `#!/usr/bin/env vite-node --script` at the top of your file:

_file.ts_
```ts
#!/usr/bin/env vite-node --script

console.log('argv:', process.argv.slice(2))
```

And make the file executable:
```sh
chmod +x ./file.ts
```

Now, you can run the file without passing it into Vite Node:
```sh
$ ./file.ts hello
argv: [ 'hello' ]
```

Note that when using the `--script` option, Vite Node forwards every arguments and options to the script to execute, even the one supported by Vite Node itself.
jgoux marked this conversation as resolved.
Show resolved Hide resolved

## Programmatic Usage

In Vite Node, the server and runner (client) are separated, so you can integrate them in different contexts (workers, cross-process, or remote) if needed. The demo below shows a simple example of having both (server and runner) running in the same context
Expand Down
15 changes: 12 additions & 3 deletions packages/vite-node/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,42 @@ cli
.option('-r, --root <path>', 'Use specified root directory')
.option('-c, --config <path>', 'Use specified config file')
.option('-w, --watch', 'Restart on file changes, similar to "nodemon"')
.option('--script', 'Use vite-node as a script runner')
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
.option('--options <options>', 'Use specified Vite server options')
.help()

cli
.command('[...files]')
.allowUnknownOptions()
.action(run)

cli.parse()

export interface CliOptions {
root?: string
script?: boolean
config?: string
watch?: boolean
options?: ViteNodeServerOptionsCLI
'--'?: string[]
}

async function run(files: string[], options: CliOptions = {}) {
if (options.script) {
files = [files[0]]
options = {}
process.argv = [process.argv[0], files[0], ...process.argv.slice(2).filter(arg => arg !== '--script' && arg !== files[0])]
}
else {
process.argv = [...process.argv.slice(0, 2), ...(options['--'] || [])]
}

if (!files.length) {
console.error(c.red('No files specified.'))
cli.outputHelp()
process.exit(1)
}

// forward argv
process.argv = [...process.argv.slice(0, 2), ...(options['--'] || [])]

const serverOptions = options.options
? parseServerOptions(options.options)
: {}
Expand Down