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
11 changes: 10 additions & 1 deletion packages/vite-node/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,40 @@ cli
.option('-c, --config <path>', 'Use specified config file')
.option('-w, --watch', 'Restart on file changes, similar to "nodemon"')
.option('--options <options>', 'Use specified Vite server options')
.option('--script-mode', 'Use vite-node as a script runner')
.help()

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

cli.parse()

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

async function run(files: string[], options: CliOptions = {}) {
if (options.scriptMode)
files = [options.scriptMode]
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved

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['--'] || [])]
if (options.scriptMode)
process.argv = [process.argv[0], options.scriptMode, ...process.argv.slice(4)]
else
process.argv = [...process.argv.slice(0, 2), ...(options['--'] || [])]

const serverOptions = options.options
? parseServerOptions(options.options)
Expand Down