2022-04-15 15:19:05 +00:00
|
|
|
import { execSync } from 'node:child_process'
|
2021-10-26 17:36:22 +00:00
|
|
|
import consola from 'consola'
|
|
|
|
import { resolve } from 'pathe'
|
2022-08-17 13:44:10 +00:00
|
|
|
import { readPackageJSON } from 'pkg-types'
|
2021-10-29 08:41:04 +00:00
|
|
|
import { getPackageManager, packageManagerLocks } from '../utils/packageManagers'
|
2022-08-16 13:14:26 +00:00
|
|
|
import { rmRecursive, touchFile } from '../utils/fs'
|
2022-08-17 13:45:45 +00:00
|
|
|
import { cleanupNuxtDirs, nuxtVersionToGitIdentifier } from '../utils/nuxt'
|
2021-10-26 17:36:22 +00:00
|
|
|
import { defineNuxtCommand } from './index'
|
|
|
|
|
2022-08-17 13:44:10 +00:00
|
|
|
async function getNuxtVersion (path: string): Promise<string|null> {
|
2022-08-11 11:41:53 +00:00
|
|
|
try {
|
2022-08-17 13:44:10 +00:00
|
|
|
const pkg = await readPackageJSON('nuxt', { url: path })
|
2022-08-11 11:41:53 +00:00
|
|
|
if (!pkg.version) {
|
2022-08-17 13:44:10 +00:00
|
|
|
consola.warn('Cannot find any installed nuxt versions in ', path)
|
2022-08-11 11:41:53 +00:00
|
|
|
}
|
|
|
|
return pkg.version || null
|
|
|
|
} catch {
|
|
|
|
return null
|
2021-10-26 17:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defineNuxtCommand({
|
|
|
|
meta: {
|
|
|
|
name: 'upgrade',
|
|
|
|
usage: 'npx nuxi upgrade [--force|-f]',
|
2022-04-20 08:52:39 +00:00
|
|
|
description: 'Upgrade nuxt'
|
2021-10-26 17:36:22 +00:00
|
|
|
},
|
|
|
|
async invoke (args) {
|
|
|
|
const rootDir = resolve(args._[0] || '.')
|
|
|
|
|
2022-08-11 11:41:53 +00:00
|
|
|
// Check package manager
|
2021-10-29 08:41:04 +00:00
|
|
|
const packageManager = getPackageManager(rootDir)
|
2021-10-26 17:36:22 +00:00
|
|
|
if (!packageManager) {
|
|
|
|
console.error('Cannot detect Package Manager in', rootDir)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
const packageManagerVersion = execSync(`${packageManager} --version`).toString('utf8').trim()
|
|
|
|
consola.info('Package Manager:', packageManager, packageManagerVersion)
|
|
|
|
|
2022-08-11 11:41:53 +00:00
|
|
|
// Check currently installed nuxt version
|
|
|
|
const currentVersion = await getNuxtVersion(rootDir) || '[unknown]'
|
2021-10-26 17:36:22 +00:00
|
|
|
consola.info('Current nuxt version:', currentVersion)
|
|
|
|
|
2022-08-11 11:41:53 +00:00
|
|
|
// Force install
|
2021-10-26 17:36:22 +00:00
|
|
|
if (args.force || args.f) {
|
|
|
|
consola.info('Removing lock-file and node_modules...')
|
2022-08-11 11:41:53 +00:00
|
|
|
const pmLockFile = resolve(rootDir, packageManagerLocks[packageManager])
|
|
|
|
await rmRecursive([pmLockFile, resolve(rootDir, 'node_modules')])
|
|
|
|
await touchFile(pmLockFile)
|
2021-10-26 17:36:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 14:07:40 +00:00
|
|
|
// Install latest version
|
|
|
|
consola.info('Installing latest Nuxt 3 release...')
|
|
|
|
execSync(`${packageManager} ${packageManager === 'yarn' ? 'add' : 'install'} -D nuxt`, { stdio: 'inherit', cwd: rootDir })
|
2022-08-11 11:41:53 +00:00
|
|
|
|
|
|
|
// Cleanup after upgrade
|
|
|
|
await cleanupNuxtDirs(rootDir)
|
|
|
|
|
|
|
|
// Check installed nuxt version again
|
|
|
|
const upgradedVersion = await getNuxtVersion(rootDir) || '[unknown]'
|
2021-10-26 17:36:22 +00:00
|
|
|
consola.info('Upgraded nuxt version:', upgradedVersion)
|
|
|
|
|
|
|
|
if (upgradedVersion === currentVersion) {
|
2022-04-20 08:52:39 +00:00
|
|
|
consola.success('You\'re already using the latest version of nuxt.')
|
2021-10-26 17:36:22 +00:00
|
|
|
} else {
|
|
|
|
consola.success('Successfully upgraded nuxt from', currentVersion, 'to', upgradedVersion)
|
2022-08-17 13:45:45 +00:00
|
|
|
const commitA = nuxtVersionToGitIdentifier(currentVersion)
|
|
|
|
const commitB = nuxtVersionToGitIdentifier(upgradedVersion)
|
2021-10-26 17:36:22 +00:00
|
|
|
if (commitA && commitB) {
|
|
|
|
consola.info('Changelog:', `https://github.com/nuxt/framework/compare/${commitA}...${commitB}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|