Skip to content

Commit 40877ae

Browse files
committedOct 22, 2024
fix(upgrade): include other core nuxt packages in upgrade
resolves #505
1 parent 61f0172 commit 40877ae

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed
 

Diff for: ‎src/commands/upgrade.ts

+20-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { existsSync } from 'node:fs'
33
import { consola } from 'consola'
44
import { colors } from 'consola/utils'
55
import { relative, resolve } from 'pathe'
6+
import type { PackageJson } from 'pkg-types'
67
import { readPackageJSON } from 'pkg-types'
78
import { defineCommand } from 'citty'
89
import {
@@ -28,28 +29,22 @@ async function getNuxtVersion(path: string): Promise<string | null> {
2829
}
2930
}
3031

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'
4135
}
42-
catch {
43-
return null
36+
if (pkg.devDependencies && pkg.devDependencies['nuxt']) {
37+
return 'devDependencies'
4438
}
39+
return 'dependencies'
4540
}
4641

4742
function hasPnpmWorkspaceFile(cwd: string): boolean {
4843
const pnpmWorkspaceFilePath = resolve(cwd, 'pnpm-workspace.yaml')
4944
return existsSync(pnpmWorkspaceFilePath)
5045
}
5146

52-
async function getNightlyVersion(): Promise<{ npmVersion: string, nuxtVersion: string }> {
47+
async function getNightlyVersion(packageNames: string[]): Promise<{ npmPackages: string[], nuxtVersion: string }> {
5348
const nuxtVersion = await consola.prompt(
5449
'Which nightly Nuxt release channel do you want to install? (3.x or 4.x)',
5550
{
@@ -63,17 +58,17 @@ async function getNightlyVersion(): Promise<{ npmVersion: string, nuxtVersion: s
6358
'3.x': '3x',
6459
'4.x': 'latest',
6560
}
66-
const npmVersion = `nuxt@npm:nuxt-nightly@${versions[nuxtVersion]}`
61+
const npmPackages = packageNames.map(p => `${p}@npm:${p}-nightly@${versions[nuxtVersion]}`)
6762

68-
return { npmVersion, nuxtVersion }
63+
return { npmPackages, nuxtVersion }
6964
}
7065

71-
async function getRequiredNewVersion(channel: string): Promise<{ npmVersion: string, nuxtVersion: string }> {
66+
async function getRequiredNewVersion(packageNames: string[], channel: string): Promise<{ npmPackages: string[], nuxtVersion: string }> {
7267
if (channel === 'nightly') {
73-
return getNightlyVersion()
68+
return getNightlyVersion(packageNames)
7469
}
7570

76-
return { npmVersion: 'nuxt@latest', nuxtVersion: '3' }
71+
return { npmPackages: packageNames.map(p => `${p}@latest`), nuxtVersion: '3' }
7772
}
7873

7974
export default defineCommand({
@@ -116,8 +111,10 @@ export default defineCommand({
116111
const currentVersion = (await getNuxtVersion(cwd)) || '[unknown]'
117112
consola.info('Current Nuxt version:', currentVersion)
118113

114+
const pkg = await readPackageJSON(cwd).catch(() => null)
115+
119116
// Check if Nuxt is a dependency or devDependency
120-
const nuxtDependencyType = await checkNuxtDependencyType(cwd)
117+
const nuxtDependencyType = pkg ? await checkNuxtDependencyType(pkg) : 'dependencies'
121118

122119
// Force install
123120
const pmLockFile = resolve(cwd, packageManagerLocks[packageManager])
@@ -141,8 +138,10 @@ export default defineCommand({
141138
await touchFile(pmLockFile)
142139
}
143140

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+
144143
// Install latest version
145-
const { npmVersion, nuxtVersion } = await getRequiredNewVersion(ctx.args.channel)
144+
const { npmPackages, nuxtVersion } = await getRequiredNewVersion(['nuxt', ...packagesToUpdate], ctx.args.channel)
146145

147146
const versionType = ctx.args.channel === 'nightly' ? 'nightly' : 'latest stable'
148147
consola.info(`Installing ${versionType} Nuxt ${nuxtVersion} release...`)
@@ -152,7 +151,7 @@ export default defineCommand({
152151
packageManager === 'yarn' ? 'add' : 'install',
153152
nuxtDependencyType === 'devDependencies' ? '-D' : '',
154153
packageManager === 'pnpm' && hasPnpmWorkspaceFile(cwd) ? '-w' : '',
155-
npmVersion,
154+
...npmPackages,
156155
].filter(Boolean).join(' ')
157156

158157
execSync(command, { stdio: 'inherit', cwd })

0 commit comments

Comments
 (0)
Please sign in to comment.