Skip to content

Commit a3fa129

Browse files
committed
perf: keep engine and update checks off the version path
1 parent 0b45675 commit a3fa129

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

‎packages/nuxt-cli/src/main.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import { cwdArgs } from './commands/_shared'
1414
import{runCommand,setCurrentCommand}from'./run'
1515
import{normaliseCwdArg}from'./utils/args'
1616
import{setupGlobalConsole}from'./utils/console'
17-
import{checkEngines}from'./utils/engines'
1817
import{debug,logger}from'./utils/logger'
1918
import{setupProxySupport}from'./utils/network'
2019
import{findInPath,withLocalBinPath}from'./utils/path-env'
21-
import{resolveProjectDir}from'./utils/paths'
2220
import{templateNames}from'./utils/templates/names'
2321
import{findUnknownFlags,suggestFlags}from'./utils/unknown-args'
2422
import{scheduleUpdateNudge}from'./utils/update-lazy'
@@ -49,13 +47,14 @@ const _main = defineCommand({
4947
setCurrentCommand(command)
5048
setupGlobalConsole({dev: command==='dev'})
5149

52-
if(command!=='_dev'&&provider!=='stackblitz'){
50+
if(command&&command!=='_dev'&&provider!=='stackblitz'){
5351
// The engine check is awaited so its warning cannot land in the middle of
5452
// a prompt, but the update checks are left running: they reach the user
5553
// through a `process.exit` handler, and a slow or unreachable registry
5654
// must never hold up the command.
57-
awaitcheckEngines().catch(err=>logger.error(String(err)))
58-
voidscheduleUpdateNudge(resolveProjectDir(ctx.args),command)
55+
awaitimport('./utils/engines').then(({ checkEngines })=>checkEngines()).catch(err=>logger.error(String(err)))
56+
voidimport('./utils/paths')
57+
.then(({ resolveProjectDir })=>scheduleUpdateNudge(resolveProjectDir(ctx.args),command))
5958
.catch(err=>debug('Failed to check for updates:',err))
6059
}
6160

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
importtype{CommandContext}from'citty'
2+
importtype{main}from'../../src/main'
3+
4+
import{beforeEach,describe,expect,it,vi}from'vitest'
5+
6+
constcheckEngines=vi.hoisted(()=>vi.fn(async()=>{}))
7+
constscheduleUpdateNudge=vi.hoisted(()=>vi.fn(async()=>{}))
8+
9+
asyncfunctionsetup(argv: string[]): Promise<void>{
10+
vi.doMock('../../src/utils/engines',()=>({ checkEngines }))
11+
vi.doMock('../../src/utils/update-lazy',()=>({ scheduleUpdateNudge }))
12+
vi.doMock('../../src/commands',()=>({
13+
commands: {info: ()=>({meta: {name: 'info'},args: {},run: ()=>{}})},
14+
}))
15+
vi.resetModules()
16+
17+
const{main: freshMain}=awaitimport('../../src/main')as{main: typeofmain}
18+
constcommand=argv.find(arg=>!arg.startsWith('-'))
19+
awaitfreshMain.setup!({
20+
rawArgs: argv,
21+
args: {_: command ? [command] : [],cwd: '.'},
22+
cmd: freshMain,
23+
data: {},
24+
}asunknownasCommandContext)
25+
26+
awaitnewPromise(resolve=>setImmediate(resolve))
27+
}
28+
29+
describe('startup checks',()=>{
30+
beforeEach(()=>{
31+
checkEngines.mockClear()
32+
scheduleUpdateNudge.mockClear()
33+
})
34+
35+
it('should run for a command',async()=>{
36+
awaitsetup(['info'])
37+
38+
expect(checkEngines).toHaveBeenCalled()
39+
awaitvi.waitFor(()=>expect(scheduleUpdateNudge).toHaveBeenCalled())
40+
})
41+
42+
it('should be skipped when no command is given',async()=>{
43+
awaitsetup(['--version'])
44+
45+
expect(checkEngines).not.toHaveBeenCalled()
46+
expect(scheduleUpdateNudge).not.toHaveBeenCalled()
47+
})
48+
})

0 commit comments

Comments
 (0)