[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

feat(vite-node): make CLI arguments parsing behavior consistent with node/tsx/ts-node (#3574)

authored by

rxliuli and committed by
GitHub
(Jul 28, 2023, 3:17 PM +0200) 1cd4eb0f 3c851872

+65 -4
+25 -4
packages/vite-node/src/cli.ts
··· 12 12 const cli = cac('vite-node') 13 13 14 14 cli 15 - .version(version) 16 15 .option('-r, --root <path>', 'Use specified root directory') 17 16 .option('-c, --config <path>', 'Use specified config file') 18 17 .option('-m, --mode <mode>', 'Set env mode') 19 18 .option('-w, --watch', 'Restart on file changes, similar to "nodemon"') 20 19 .option('--script', 'Use vite-node as a script runner') 21 20 .option('--options <options>', 'Use specified Vite server options') 22 - .help() 21 + .option('-v, --version', 'Output the version number') 22 + .option('-h, --help', 'Display help for command') 23 23 24 24 cli 25 25 .command('[...files]') 26 26 .allowUnknownOptions() 27 27 .action(run) 28 28 29 - cli.parse() 29 + cli.parse(process.argv, { run: false }) 30 + 31 + if (cli.args.length === 0) { 32 + cli.runMatchedCommand() 33 + } 34 + else { 35 + const i = cli.rawArgs.indexOf(cli.args[0]) + 1 36 + const scriptArgs = cli.rawArgs.slice(i).filter(it => it !== '--') 37 + const executeArgs = [...cli.rawArgs.slice(0, i), '--', ...scriptArgs] 38 + cli.parse(executeArgs) 39 + } 30 40 31 41 export interface CliOptions { 32 42 root?: string ··· 35 45 mode?: string 36 46 watch?: boolean 37 47 options?: ViteNodeServerOptionsCLI 48 + version?: boolean 49 + help?: boolean 38 50 '--'?: string[] 39 51 } 40 52 ··· 48 60 process.argv = [...process.argv.slice(0, 2), ...(options['--'] || [])] 49 61 } 50 62 63 + if (options.version) { 64 + cli.version(version) 65 + cli.outputVersion() 66 + process.exit(0) 67 + } 68 + if (options.help) { 69 + cli.version(version).outputHelp() 70 + process.exit(0) 71 + } 51 72 if (!files.length) { 52 73 console.error(c.red('No files specified.')) 53 - cli.outputHelp() 74 + cli.version(version).outputHelp() 54 75 process.exit(1) 55 76 } 56 77
+2
test/vite-node/src/cli-parse-args.js
··· 1 + // eslint-disable-next-line no-console 2 + console.log(process.argv)
+38
test/vite-node/test/cli.test.ts
··· 1 + import { execa } from 'execa' 2 + import { resolve } from 'pathe' 3 + import { expect, it } from 'vitest' 4 + 5 + const cliPath = resolve(__dirname, '../../../packages/vite-node/src/cli.ts') 6 + const entryPath = resolve(__dirname, '../src/cli-parse-args.js') 7 + 8 + const parseResult = (s: string) => JSON.parse(s.replaceAll('\'', '"')) 9 + 10 + it('basic', async () => { 11 + const result = await execa('npx', ['esno', cliPath, entryPath], { reject: true }) 12 + expect(result.stdout).include('node') 13 + expect(parseResult(result.stdout)).length(2) 14 + }, 60_000) 15 + 16 + it('--help', async () => { 17 + const r1 = await execa('npx', ['esno', cliPath, '--help', entryPath], { reject: true }) 18 + expect(r1.stdout).include('help') 19 + const r2 = await execa('npx', ['esno', cliPath, '-h', entryPath], { reject: true }) 20 + expect(r2.stdout).include('help') 21 + }, 60_000) 22 + 23 + it('--version', async () => { 24 + const r1 = await execa('npx', ['esno', cliPath, '--version', entryPath], { reject: true }) 25 + expect(r1.stdout).include('vite-node/') 26 + const r2 = await execa('npx', ['esno', cliPath, '-v', entryPath], { reject: true }) 27 + expect(r2.stdout).include('vite-node/') 28 + }, 60_000) 29 + 30 + it('script args', async () => { 31 + const r1 = await execa('npx', ['esno', cliPath, entryPath, '--version', '--help'], { reject: true }) 32 + expect(parseResult(r1.stdout)).include('--version').include('--help') 33 + }, 60_000) 34 + 35 + it('script args in -- after', async () => { 36 + const r1 = await execa('npx', ['esno', cliPath, entryPath, '--', '--version', '--help'], { reject: true }) 37 + expect(parseResult(r1.stdout)).include('--version').include('--help') 38 + }, 60_000)