Skip to content

Commit

Permalink
feat: #37
Browse files Browse the repository at this point in the history
  • Loading branch information
HigherOrderLogic committed Sep 13, 2023
1 parent 765ea8c commit f3a1836
Showing 1 changed file with 93 additions and 56 deletions.
149 changes: 93 additions & 56 deletions src/commands/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ import { defineCommand } from 'citty'

import { legacyRootDirArgs, sharedArgs } from './_shared'

async function getNuxtVersion(path: string): Promise<string | null> {
async function getPackageVersion(
packageName: string,
path: string,
): Promise<string | null> {
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 {
Expand All @@ -28,7 +36,7 @@ async function getNuxtVersion(path: string): Promise<string | null> {
export default defineCommand({
meta: {
name: 'upgrade',
description: 'Upgrade nuxt',
description: 'Upgrade nuxt monorepo packages',
},
args: {
...sharedArgs,
Expand All @@ -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}`,
)
}
}
}
}
},
Expand Down

0 comments on commit f3a1836

Please # to comment.