Skip to content

Commit ec34eee

Browse files
committed
fix: do not mistake a --cwd value for the command
1 parent b841b87 commit ec34eee

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎packages/nuxi/src/launcher.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import { withNodePath } from '../../nuxt-cli/src/utils/paths'
1111

1212
constFLAG_RE=/^-/
1313

14+
/** Launcher-level flags whose value is a separate argument, so it is not the command. */
15+
constVALUE_FLAGS=newSet(['--cwd'])
16+
1417
constBACKSLASH_RE=/\\/g
1518

1619
constlauncherDist=comparablePath(join(fileURLToPath(newURL('../',import.meta.url)),'dist'))
@@ -116,8 +119,17 @@ export function supportsCommand(main: unknown, command: string): boolean {
116119
returncommandinsubCommands
117120
}
118121

119-
functioncommandName(rawArgs: string[]){
120-
returnrawArgs.find(arg=>!FLAG_RE.test(arg))
122+
/** The command being run, skipping the value of any flag written as `--flag value`. */
123+
exportfunctioncommandName(rawArgs: string[]): string|undefined{
124+
for(leti=0;i<rawArgs.length;i++){
125+
constarg=rawArgs[i]!
126+
if(!FLAG_RE.test(arg)){
127+
returnarg
128+
}
129+
if(VALUE_FLAGS.has(arg)){
130+
i++
131+
}
132+
}
121133
}
122134

123135
/**

‎packages/nuxi/test/launcher.spec.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fileURLToPath } from 'node:url'
66

77
import{afterEach,beforeEach,describe,expect,it}from'vitest'
88

9-
import{loadProjectCli,supportsCommand}from'../src/launcher'
9+
import{commandName,loadProjectCli,supportsCommand}from'../src/launcher'
1010

1111
constcwd=process.cwd()
1212
letnodePath: string|undefined
@@ -115,6 +115,23 @@ describe('loadProjectCli', () => {
115115
})
116116
})
117117

118+
describe('commandName',()=>{
119+
it('should read the first positional',()=>{
120+
expect(commandName(['dev','--port','3000'])).toBe('dev')
121+
expect(commandName(['--port=3000','dev'])).toBe('dev')
122+
})
123+
124+
it('should not mistake a `--cwd` value for the command',()=>{
125+
expect(commandName(['--cwd','my-app','dev'])).toBe('dev')
126+
expect(commandName(['--cwd=my-app','dev'])).toBe('dev')
127+
})
128+
129+
it('should be undefined when no command is given',()=>{
130+
expect(commandName(['--help'])).toBeUndefined()
131+
expect(commandName(['--cwd','my-app'])).toBeUndefined()
132+
})
133+
})
134+
118135
describe('supportsCommand',()=>{
119136
constmain={subCommands: {dev: ()=>{},init: ()=>{}}}
120137

0 commit comments

Comments
 (0)