fix(nuxi): fix issues with nuxi upgrade (#6514)

This commit is contained in:
pooya parsa 2022-08-11 13:41:53 +02:00 committed by GitHub
parent 5ea0474225
commit 4f9337adcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 22 deletions

View File

@ -4,16 +4,20 @@ import consola from 'consola'
import { resolve } from 'pathe' import { resolve } from 'pathe'
import { resolveModule } from '../utils/cjs' import { resolveModule } from '../utils/cjs'
import { getPackageManager, packageManagerLocks } from '../utils/packageManagers' import { getPackageManager, packageManagerLocks } from '../utils/packageManagers'
import { cleanupNuxtDirs, rmRecursive } from '../utils/fs' import { cleanupNuxtDirs, rmRecursive, touchFile } from '../utils/fs'
import { defineNuxtCommand } from './index' import { defineNuxtCommand } from './index'
async function getNuxtVersion (paths: string | string[]) { async function getNuxtVersion (paths: string | string[]): Promise<string|null> {
try {
const pkgJson = resolveModule('nuxt/package.json', paths) const pkgJson = resolveModule('nuxt/package.json', paths)
const pkg = pkgJson && JSON.parse(await fsp.readFile(pkgJson, 'utf8')) const pkg = pkgJson && JSON.parse(await fsp.readFile(pkgJson, 'utf8'))
if (!pkg.version) { if (!pkg.version) {
consola.warn('Cannot find any installed nuxt versions in ', paths) consola.warn('Cannot find any installed nuxt versions in ', paths)
} }
return pkg.version || '0.0.0' return pkg.version || null
} catch {
return null
}
} }
export default defineNuxtCommand({ export default defineNuxtCommand({
@ -25,6 +29,7 @@ export default defineNuxtCommand({
async invoke (args) { async invoke (args) {
const rootDir = resolve(args._[0] || '.') const rootDir = resolve(args._[0] || '.')
// Check package manager
const packageManager = getPackageManager(rootDir) const packageManager = getPackageManager(rootDir)
if (!packageManager) { if (!packageManager) {
console.error('Cannot detect Package Manager in', rootDir) console.error('Cannot detect Package Manager in', rootDir)
@ -33,25 +38,27 @@ export default defineNuxtCommand({
const packageManagerVersion = execSync(`${packageManager} --version`).toString('utf8').trim() const packageManagerVersion = execSync(`${packageManager} --version`).toString('utf8').trim()
consola.info('Package Manager:', packageManager, packageManagerVersion) consola.info('Package Manager:', packageManager, packageManagerVersion)
const currentVersion = await getNuxtVersion(rootDir) // Check currently installed nuxt version
const currentVersion = await getNuxtVersion(rootDir) || '[unknown]'
consola.info('Current nuxt version:', currentVersion) consola.info('Current nuxt version:', currentVersion)
// Force install
if (args.force || args.f) { if (args.force || args.f) {
consola.info('Removing lock-file and node_modules...') consola.info('Removing lock-file and node_modules...')
await rmRecursive([ const pmLockFile = resolve(rootDir, packageManagerLocks[packageManager])
packageManagerLocks[packageManager], await rmRecursive([pmLockFile, resolve(rootDir, 'node_modules')])
fsp.rm('node_modules', { recursive: true }) await touchFile(pmLockFile)
])
await cleanupNuxtDirs(rootDir)
consola.info('Installing nuxt...')
execSync(`${packageManager} install`, { stdio: 'inherit' })
} else {
await cleanupNuxtDirs(rootDir)
consola.info('Upgrading nuxt...')
execSync(`${packageManager} ${packageManager === 'yarn' ? 'add' : 'install'} -D nuxt@rc`, { stdio: 'inherit' })
} }
const upgradedVersion = await getNuxtVersion(rootDir) // Install latest rc
consola.info('Installing latest Nuxt 3 RC...')
execSync(`${packageManager} ${packageManager === 'yarn' ? 'add' : 'install'} -D nuxt@rc`, { stdio: 'inherit' })
// Cleanup after upgrade
await cleanupNuxtDirs(rootDir)
// Check installed nuxt version again
const upgradedVersion = await getNuxtVersion(rootDir) || '[unknown]'
consola.info('Upgraded nuxt version:', upgradedVersion) consola.info('Upgraded nuxt version:', upgradedVersion)
if (upgradedVersion === currentVersion) { if (upgradedVersion === currentVersion) {

View File

@ -18,11 +18,17 @@ export async function clearDir (path: string) {
} }
export async function rmRecursive (paths: string[]) { export async function rmRecursive (paths: string[]) {
await Promise.all(paths.map(async (path) => { await Promise.all(paths.filter(p => typeof p === 'string').map(async (path) => {
await fsp.rm(path, { recursive: true, force: true }) consola.debug('Removing recursive path', path)
await fsp.rm(path, { recursive: true, force: true }).catch(() => {})
})) }))
} }
export async function touchFile (path: string) {
const time = new Date()
await fsp.utimes(path, time, time).catch(() => {})
}
export async function cleanupNuxtDirs (rootDir: string) { export async function cleanupNuxtDirs (rootDir: string) {
consola.info('Cleaning up generated nuxt files and caches...') consola.info('Cleaning up generated nuxt files and caches...')