Skip to content

Commit 79a829d

Browse files
committed
fix: don't mistake another package.json for nuxt's own
1 parent d0b43f8 commit 79a829d

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎packages/nuxt-cli/src/commands/module/search.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { kebabCase, upperFirst } from 'scule'
99
import{formatInfoBox}from'../../utils/formatting'
1010
import{logger}from'../../utils/logger'
1111
import{logNetworkError}from'../../utils/network'
12-
import{getNuxtVersion}from'../../utils/versions'
12+
import{DEFAULT_NUXT_VERSION,getNuxtVersion}from'../../utils/versions'
1313
import{cwdArgs}from'../_shared'
1414
import{checkNuxtCompatibility,fetchModules,MODULES_API_URL}from'./_utils'
1515

@@ -41,7 +41,7 @@ export default defineCommand({
4141
},
4242
},
4343
asyncsetup(ctx){
44-
constnuxtVersion=awaitgetNuxtVersion(ctx.args.cwd)
44+
constnuxtVersion=awaitgetNuxtVersion(ctx.args.cwd).catch(()=>DEFAULT_NUXT_VERSION)
4545
returnfindModuleByKeywords(ctx.args._.join(' '),nuxtVersion)
4646
},
4747
})

‎packages/nuxt-cli/src/utils/versions.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ import { coerce } from 'verkit'
55

66
import{tryResolveNuxt}from'./kit'
77

8+
/**
9+
* Names a resolved `nuxt` dependency can legitimately have, so that a
10+
* `package.json` reached through `pkg-types`' nearest-file fallback (which
11+
* happens when `nuxt` cannot be resolved at all) is not mistaken for Nuxt's own.
12+
*/
13+
constNUXT_PACKAGE_NAMES=newSet(['nuxt','nuxt-nightly','nuxt3','nuxt-edge'])
14+
15+
/** Assumed Nuxt version when the project declares no resolvable one. */
16+
exportconstDEFAULT_NUXT_VERSION='3.0.0'
17+
818
exportasyncfunctiongetNuxtVersion(cwd: string,cache=true){
919
constnuxtPkg=awaitreadPackageJSON('nuxt',{url: cwd,try: true, cache }).catch(()=>null)
10-
if(nuxtPkg){
11-
returnnuxtPkg.version!
20+
if(nuxtPkg?.version&&NUXT_PACKAGE_NAMES.has(nuxtPkg.name!)){
21+
returnnuxtPkg.version
1222
}
1323
constpkg=awaitreadPackageJSON(cwd)
1424
constpkgDep=pkg?.dependencies?.nuxt||pkg?.devDependencies?.nuxt
15-
return(pkgDep&&coerce(pkgDep))||'3.0.0'
25+
return(pkgDep&&coerce(pkgDep))||DEFAULT_NUXT_VERSION
1626
}
1727

1828
exportfunctiongetPkgVersion(cwd: string,pkg: string,options?: {via?: string[]}){
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import{mkdtemp,rm,writeFile}from'node:fs/promises'
2+
import{tmpdir}from'node:os'
3+
import{join}from'node:path'
4+
5+
import{afterEach,beforeEach,describe,expect,it}from'vitest'
6+
7+
import{getNuxtVersion}from'../../../src/utils/versions'
8+
9+
describe('getNuxtVersion',()=>{
10+
lettempDir: string
11+
12+
beforeEach(async()=>{
13+
tempDir=awaitmkdtemp(join(tmpdir(),'nuxt-versions-test-'))
14+
})
15+
16+
afterEach(async()=>{
17+
awaitrm(tempDir,{recursive: true,force: true})
18+
})
19+
20+
it('should read the declared version when nuxt is not installed',async()=>{
21+
awaitwriteFile(join(tempDir,'package.json'),JSON.stringify({devDependencies: {nuxt: '^4.2.0'}}))
22+
23+
expect(awaitgetNuxtVersion(tempDir,false)).toBe('4.2.0')
24+
})
25+
})

0 commit comments

Comments
 (0)