@@ -3,6 +3,7 @@ import { existsSync } from 'node:fs'
3
3
import { consola } from 'consola'
4
4
import { colors } from 'consola/utils'
5
5
import { relative , resolve } from 'pathe'
6
+ import type { PackageJson } from 'pkg-types'
6
7
import { readPackageJSON } from 'pkg-types'
7
8
import { defineCommand } from 'citty'
8
9
import {
@@ -28,28 +29,22 @@ async function getNuxtVersion(path: string): Promise<string | null> {
28
29
}
29
30
}
30
31
31
- async function checkNuxtDependencyType ( path : string ) : Promise < 'dependencies' | 'devDependencies' | null > {
32
- try {
33
- const pkg = await readPackageJSON ( path )
34
- if ( pkg . dependencies && pkg . dependencies [ 'nuxt' ] ) {
35
- return 'dependencies'
36
- }
37
- if ( pkg . devDependencies && pkg . devDependencies [ 'nuxt' ] ) {
38
- return 'devDependencies'
39
- }
40
- return null
32
+ async function checkNuxtDependencyType ( pkg : PackageJson ) : Promise < 'dependencies' | 'devDependencies' | null > {
33
+ if ( pkg . dependencies && pkg . dependencies [ 'nuxt' ] ) {
34
+ return 'dependencies'
41
35
}
42
- catch {
43
- return null
36
+ if ( pkg . devDependencies && pkg . devDependencies [ 'nuxt' ] ) {
37
+ return 'devDependencies'
44
38
}
39
+ return 'dependencies'
45
40
}
46
41
47
42
function hasPnpmWorkspaceFile ( cwd : string ) : boolean {
48
43
const pnpmWorkspaceFilePath = resolve ( cwd , 'pnpm-workspace.yaml' )
49
44
return existsSync ( pnpmWorkspaceFilePath )
50
45
}
51
46
52
- async function getNightlyVersion ( ) : Promise < { npmVersion : string , nuxtVersion : string } > {
47
+ async function getNightlyVersion ( packageNames : string [ ] ) : Promise < { npmPackages : string [ ] , nuxtVersion : string } > {
53
48
const nuxtVersion = await consola . prompt (
54
49
'Which nightly Nuxt release channel do you want to install? (3.x or 4.x)' ,
55
50
{
@@ -63,17 +58,17 @@ async function getNightlyVersion(): Promise<{ npmVersion: string, nuxtVersion: s
63
58
'3.x' : '3x' ,
64
59
'4.x' : 'latest' ,
65
60
}
66
- const npmVersion = `nuxt @npm:nuxt -nightly@${ versions [ nuxtVersion ] } `
61
+ const npmPackages = packageNames . map ( p => ` ${ p } @npm:${ p } -nightly@${ versions [ nuxtVersion ] } `)
67
62
68
- return { npmVersion , nuxtVersion }
63
+ return { npmPackages , nuxtVersion }
69
64
}
70
65
71
- async function getRequiredNewVersion ( channel : string ) : Promise < { npmVersion : string , nuxtVersion : string } > {
66
+ async function getRequiredNewVersion ( packageNames : string [ ] , channel : string ) : Promise < { npmPackages : string [ ] , nuxtVersion : string } > {
72
67
if ( channel === 'nightly' ) {
73
- return getNightlyVersion ( )
68
+ return getNightlyVersion ( packageNames )
74
69
}
75
70
76
- return { npmVersion : 'nuxt @latest' , nuxtVersion : '3' }
71
+ return { npmPackages : packageNames . map ( p => ` ${ p } @latest` ) , nuxtVersion : '3' }
77
72
}
78
73
79
74
export default defineCommand ( {
@@ -116,8 +111,10 @@ export default defineCommand({
116
111
const currentVersion = ( await getNuxtVersion ( cwd ) ) || '[unknown]'
117
112
consola . info ( 'Current Nuxt version:' , currentVersion )
118
113
114
+ const pkg = await readPackageJSON ( cwd ) . catch ( ( ) => null )
115
+
119
116
// Check if Nuxt is a dependency or devDependency
120
- const nuxtDependencyType = await checkNuxtDependencyType ( cwd )
117
+ const nuxtDependencyType = pkg ? await checkNuxtDependencyType ( pkg ) : 'dependencies'
121
118
122
119
// Force install
123
120
const pmLockFile = resolve ( cwd , packageManagerLocks [ packageManager ] )
@@ -141,8 +138,10 @@ export default defineCommand({
141
138
await touchFile ( pmLockFile )
142
139
}
143
140
141
+ const packagesToUpdate = pkg ? [ '@nuxt/kit' , '@nuxt/schema' , '@nuxt/vite-builder' , '@nuxt/webpack-builder' , '@nuxt/rspack-builder' ] . filter ( p => pkg . dependencies ?. [ p ] || pkg . devDependencies ?. [ p ] ) : [ ]
142
+
144
143
// Install latest version
145
- const { npmVersion , nuxtVersion } = await getRequiredNewVersion ( ctx . args . channel )
144
+ const { npmPackages , nuxtVersion } = await getRequiredNewVersion ( [ 'nuxt' , ... packagesToUpdate ] , ctx . args . channel )
146
145
147
146
const versionType = ctx . args . channel === 'nightly' ? 'nightly' : 'latest stable'
148
147
consola . info ( `Installing ${ versionType } Nuxt ${ nuxtVersion } release...` )
@@ -152,7 +151,7 @@ export default defineCommand({
152
151
packageManager === 'yarn' ? 'add' : 'install' ,
153
152
nuxtDependencyType === 'devDependencies' ? '-D' : '' ,
154
153
packageManager === 'pnpm' && hasPnpmWorkspaceFile ( cwd ) ? '-w' : '' ,
155
- npmVersion ,
154
+ ... npmPackages ,
156
155
] . filter ( Boolean ) . join ( ' ' )
157
156
158
157
execSync ( command , { stdio : 'inherit' , cwd } )
0 commit comments