From d3d6b1fc82fc0773f57c0f39682d3b90b745e240 Mon Sep 17 00:00:00 2001 From: Julien Goux Date: Sat, 11 Feb 2023 14:08:19 +0100 Subject: [PATCH] feat: add --script option to vite-node (#2793) * 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 * Update packages/vite-node/README.md Co-authored-by: Anjorin Damilare --------- Co-authored-by: Anjorin Damilare --- packages/vite-node/README.md | 26 ++++++++++++++++++++++++++ packages/vite-node/src/cli.ts | 15 ++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/packages/vite-node/README.md b/packages/vite-node/README.md index 2bd2fa115ef3..558d3ba4ae7f 100644 --- a/packages/vite-node/README.md +++ b/packages/vite-node/README.md @@ -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 diff --git a/packages/vite-node/src/cli.ts b/packages/vite-node/src/cli.ts index 602848d0c86e..174f93467ed7 100644 --- a/packages/vite-node/src/cli.ts +++ b/packages/vite-node/src/cli.ts @@ -16,17 +16,20 @@ cli .option('-r, --root ', 'Use specified root directory') .option('-c, --config ', '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 ', '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 @@ -34,15 +37,21 @@ export interface CliOptions { } 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) : {}