Skip to content

Commit

Permalink
fix: correctly split the argv string
Browse files Browse the repository at this point in the history
  • Loading branch information
btea committed Feb 21, 2025
1 parent 28ad51b commit 549adfc
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/vitest/src/node/cli/cac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,26 @@ export function createCLI(options: CliParseOptions = {}): CAC {
return cli
}

function splitArgv(argv: string): string[] {
const reg = /(['"])(?:(?!\1).)+\1/g
argv = argv.replace(reg, match => match.replace(/\s/g, '\x00'))
return argv.split(' ').map((arg: string) => {
arg = arg.replace(/\0/g, ' ')
if (arg.startsWith('"') && arg.endsWith('"')) {
return arg.slice(1, -1)
}
if (arg.startsWith(`'`) && arg.endsWith(`'`)) {
return arg.slice(1, -1)
}
return arg
})
}

export function parseCLI(argv: string | string[], config: CliParseOptions = {}): {
filter: string[]
options: CliOptions
} {
const arrayArgs = typeof argv === 'string' ? argv.split(' ') : argv
const arrayArgs = typeof argv === 'string' ? splitArgv(argv) : argv
if (arrayArgs[0] !== 'vitest') {
throw new Error(`Expected "vitest" as the first argument, received "${arrayArgs[0]}"`)
}
Expand Down

0 comments on commit 549adfc

Please sign in to comment.