Skip to content

Commit

Permalink
fix(upgrade): include other core nuxt packages in upgrade
Browse files Browse the repository at this point in the history
resolves #505
  • Loading branch information
danielroe committed Oct 22, 2024
1 parent 61f0172 commit 40877ae
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/commands/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { existsSync } from 'node:fs'
import { consola } from 'consola'
import { colors } from 'consola/utils'
import { relative, resolve } from 'pathe'
import type { PackageJson } from 'pkg-types'
import { readPackageJSON } from 'pkg-types'
import { defineCommand } from 'citty'
import {
Expand All @@ -28,28 +29,22 @@ async function getNuxtVersion(path: string): Promise<string | null> {
}
}

async function checkNuxtDependencyType(path: string): Promise<'dependencies' | 'devDependencies' | null> {
try {
const pkg = await readPackageJSON(path)
if (pkg.dependencies && pkg.dependencies['nuxt']) {
return 'dependencies'
}
if (pkg.devDependencies && pkg.devDependencies['nuxt']) {
return 'devDependencies'
}
return null
async function checkNuxtDependencyType(pkg: PackageJson): Promise<'dependencies' | 'devDependencies' | null> {
if (pkg.dependencies && pkg.dependencies['nuxt']) {
return 'dependencies'
}
catch {
return null
if (pkg.devDependencies && pkg.devDependencies['nuxt']) {
return 'devDependencies'
}
return 'dependencies'
}

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

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

return { npmVersion, nuxtVersion }
return { npmPackages, nuxtVersion }
}

async function getRequiredNewVersion(channel: string): Promise<{ npmVersion: string, nuxtVersion: string }> {
async function getRequiredNewVersion(packageNames: string[], channel: string): Promise<{ npmPackages: string[], nuxtVersion: string }> {
if (channel === 'nightly') {
return getNightlyVersion()
return getNightlyVersion(packageNames)
}

return { npmVersion: 'nuxt@latest', nuxtVersion: '3' }
return { npmPackages: packageNames.map(p => `${p}@latest`), nuxtVersion: '3' }
}

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

const pkg = await readPackageJSON(cwd).catch(() => null)

// Check if Nuxt is a dependency or devDependency
const nuxtDependencyType = await checkNuxtDependencyType(cwd)
const nuxtDependencyType = pkg ? await checkNuxtDependencyType(pkg) : 'dependencies'

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

const packagesToUpdate = pkg ? ['@nuxt/kit', '@nuxt/schema', '@nuxt/vite-builder', '@nuxt/webpack-builder', '@nuxt/rspack-builder'].filter(p => pkg.dependencies?.[p] || pkg.devDependencies?.[p]) : []

// Install latest version
const { npmVersion, nuxtVersion } = await getRequiredNewVersion(ctx.args.channel)
const { npmPackages, nuxtVersion } = await getRequiredNewVersion(['nuxt', ...packagesToUpdate], ctx.args.channel)

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

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

0 comments on commit 40877ae

Please sign in to comment.