Skip to content

Commit b6f2690

Browse files
committed
fix(devtools): harden wizard invocation
1 parent 9e3b1cb commit b6f2690

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

‎packages/nuxt-cli/src/commands/devtools.ts‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
importprocessfrom'node:process'
2-
3-
import{styleText}from'node:util'
4-
51
import{defineCommand}from'citty'
62
import{x}from'tinyexec'
7-
import{logger}from'../utils/logger'
83

94
import{resolveRootDir}from'../utils/paths'
105
import{rootDirArgs}from'./_shared'
@@ -15,7 +10,6 @@ export default defineCommand({
1510
description: 'Enable or disable devtools in a Nuxt project',
1611
},
1712
args: {
18-
// `command` has to precede the `dir` positional supplied by `rootDirArgs`
1913
command: {
2014
type: 'positional',
2115
description: 'Command to run',
@@ -27,14 +21,13 @@ export default defineCommand({
2721
constcwd=resolveRootDir(ctx.args)
2822
constcommand=ctx.args.command
2923

30-
if(!command||!['enable','disable'].includes(command)){
31-
logger.error(`Unknown command ${styleText('cyan',command||'')}.`)
32-
process.exit(1)
24+
if(command!=='enable'&&command!=='disable'){
25+
thrownewError(`Unknown devtools command \`${command}\`. Expected \`enable\` or \`disable\`.`)
3326
}
3427

3528
awaitx(
3629
'npx',
37-
['@nuxt/devtools-wizard@latest',command,cwd],
30+
['--yes','@nuxt/devtools-wizard@latest',command],
3831
{
3932
throwOnError: true,
4033
nodeOptions: {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import{resolve}from'pathe'
2+
import{beforeEach,describe,expect,it,vi}from'vitest'
3+
4+
import{runCommand}from'../../../src/run'
5+
6+
const{ x }=vi.hoisted(()=>({
7+
x: vi.fn(()=>Promise.resolve({exitCode: 0})),
8+
}))
9+
10+
vi.mock('tinyexec',()=>({ x }))
11+
12+
describe('nuxt devtools command',()=>{
13+
beforeEach(()=>{
14+
vi.clearAllMocks()
15+
})
16+
17+
it.each(['enable','disable'])('runs the devtools wizard to %s devtools',async(action)=>{
18+
awaitrunCommand('devtools',[action,'apps/web'])
19+
20+
expect(x).toHaveBeenCalledOnce()
21+
expect(x).toHaveBeenCalledWith(
22+
'npx',
23+
['--yes','@nuxt/devtools-wizard@latest',action],
24+
{
25+
throwOnError: true,
26+
nodeOptions: {
27+
stdio: 'inherit',
28+
cwd: resolve('apps/web'),
29+
},
30+
},
31+
)
32+
})
33+
34+
it('rejects unknown actions without terminating programmatic callers',async()=>{
35+
awaitexpect(runCommand('devtools',['toggle'])).rejects.toThrow(
36+
'Unknown devtools command `toggle`. Expected `enable` or `disable`.',
37+
)
38+
expect(x).not.toHaveBeenCalled()
39+
})
40+
})

0 commit comments

Comments
 (0)