Skip to content

Commit

Permalink
feat: add --script option to vite-node (#2793)
Browse files Browse the repository at this point in the history
* feat: add script-mode option to vite-node

* make it more resilient

* edit README

* remove tsx mention

* Update packages/vite-node/README.md

Co-authored-by: Anjorin Damilare <[email protected]>

* Update packages/vite-node/README.md

Co-authored-by: Anjorin Damilare <[email protected]>

---------

Co-authored-by: Anjorin Damilare <[email protected]>
  • Loading branch information
jgoux and dammy001 authored Feb 11, 2023
1 parent d534184 commit d3d6b1f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
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 don't need to be passed into Vite Node, you can declare it in the [hashbang](https://bash.cyberciti.biz/guide/Shebang).

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 argument and option to the script to execute, even the one supported by Vite Node itself.

## 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')
.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

0 comments on commit d3d6b1f

Please sign in to comment.