diff --git a/src/commands/upgrade.ts b/src/commands/upgrade.ts index 85cfec6b..f1a9c961 100644 --- a/src/commands/upgrade.ts +++ b/src/commands/upgrade.ts @@ -13,11 +13,19 @@ import { defineCommand } from 'citty' import { legacyRootDirArgs, sharedArgs } from './_shared' -async function getNuxtVersion(path: string): Promise { +async function getPackageVersion( + packageName: string, + path: string, +): Promise { try { - const pkg = await readPackageJSON('nuxt', { url: path }) + const pkg = await readPackageJSON(packageName, { url: path }) if (!pkg.version) { - consola.warn('Cannot find any installed nuxt versions in ', path) + consola.warn( + 'Cannot find any installed', + packageName, + 'versions in ', + path, + ) } return pkg.version || null } catch { @@ -28,7 +36,7 @@ async function getNuxtVersion(path: string): Promise { export default defineCommand({ meta: { name: 'upgrade', - description: 'Upgrade nuxt', + description: 'Upgrade nuxt monorepo packages', }, args: { ...sharedArgs, @@ -53,64 +61,93 @@ export default defineCommand({ .trim() consola.info('Package Manager:', packageManager, packageManagerVersion) - // Check currently installed nuxt version - const currentVersion = (await getNuxtVersion(cwd)) || '[unknown]' - consola.info('Current nuxt version:', currentVersion) + let nuxtDirsClean = false - // Force install - const pmLockFile = resolve(cwd, packageManagerLocks[packageManager]) - const forceRemovals = ['node_modules', relative(process.cwd(), pmLockFile)] - .map((p) => colors.cyan(p)) - .join(' and ') - if (ctx.args.force === undefined) { - ctx.args.force = await consola.prompt( - `Would you like to recreate ${forceRemovals} to fix problems with hoisted dependency versions and ensure you have the most up-to-date dependencies?`, - { - type: 'confirm', - default: true, - }, - ) - } - if (ctx.args.force) { - consola.info( - `Recreating ${forceRemovals}. If you encounter any issues, revert the changes and try with \`--no-force\``, - ) - await rmRecursive([pmLockFile, resolve(cwd, 'node_modules')]) - await touchFile(pmLockFile) - } - - // Install latest version - consola.info('Installing latest Nuxt 3 release...') - execSync( - `${packageManager} ${ - packageManager === 'yarn' ? 'add' : 'install' - } -D nuxt`, - { stdio: 'inherit', cwd }, - ) + for (const packageName of [ + 'nuxt', + '@nuxt/kit', + '@nuxt/schema', + '@nuxt/test-utils', + ]) { + // Check currently installed package version + const currentVersion = + (await getPackageVersion(packageName, cwd)) || '[unknown]' + if (currentVersion === '[unknown]' && packageName != 'nuxt') { + consola.log('Skipping', packageName) + continue + } + consola.info('Current', packageName, 'version:', currentVersion) - // Cleanup after upgrade - await cleanupNuxtDirs(cwd) + // Force install + const pmLockFile = resolve(cwd, packageManagerLocks[packageManager]) + const forceRemovals = [ + 'node_modules', + relative(process.cwd(), pmLockFile), + ] + .map((p) => colors.cyan(p)) + .join(' and ') + if (ctx.args.force === undefined) { + ctx.args.force = await consola.prompt( + `Would you like to recreate ${forceRemovals} to fix problems with hoisted dependency versions and ensure you have the most up-to-date dependencies?`, + { + type: 'confirm', + default: true, + }, + ) + } + if (ctx.args.force) { + consola.info( + `Recreating ${forceRemovals}. If you encounter any issues, revert the changes and try with \`--no-force\``, + ) + await rmRecursive([pmLockFile, resolve(cwd, 'node_modules')]) + await touchFile(pmLockFile) + } - // Check installed nuxt version again - const upgradedVersion = (await getNuxtVersion(cwd)) || '[unknown]' - consola.info('Upgraded nuxt version:', upgradedVersion) + // Cleanup after upgrade + if (!nuxtDirsClean) { + await cleanupNuxtDirs(cwd) + nuxtDirsClean = true + } - if (upgradedVersion === currentVersion) { - consola.success("You're already using the latest version of nuxt.") - } else { - consola.success( - 'Successfully upgraded nuxt from', - currentVersion, - 'to', - upgradedVersion, + // Install latest version + consola.info('Installing latest', packageName, 'release...') + execSync( + `${packageManager} ${ + packageManager === 'yarn' ? 'add' : 'install' + } -D ${packageName}`, + { stdio: 'inherit', cwd }, ) - const commitA = nuxtVersionToGitIdentifier(currentVersion) - const commitB = nuxtVersionToGitIdentifier(upgradedVersion) - if (commitA && commitB) { - consola.info( - 'Changelog:', - `https://github.com/nuxt/nuxt/compare/${commitA}...${commitB}`, + + // Check installed package version again + const upgradedVersion = + (await getPackageVersion(packageName, cwd)) || '[unknown]' + consola.info('Upgraded', packageName, 'version:', upgradedVersion) + + if (upgradedVersion === currentVersion) { + consola.success( + "You're already using the latest version of", + packageName, + '.', + ) + } else { + consola.success( + 'Successfully upgraded', + packageName, + 'from', + currentVersion, + 'to', + upgradedVersion, ) + if (packageName == 'nuxt') { + const commitA = nuxtVersionToGitIdentifier(currentVersion) + const commitB = nuxtVersionToGitIdentifier(upgradedVersion) + if (commitA && commitB) { + consola.info( + 'Changelog:', + `https://github.com/nuxt/nuxt/compare/${commitA}...${commitB}`, + ) + } + } } } },