feat(nuxi): nuxi upgrade command (#1468)

Co-authored-by: Pooya Parsa <pyapar@gmail.com>
This commit is contained in:
Mgs. M. Rizqi Fadhlurrahman 2021-10-27 00:36:22 +07:00 committed by GitHub
parent 83139d07e9
commit 1f0cea6e7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 2 deletions

View File

@ -42,3 +42,11 @@ npm run build
::
Nuxt will create a [`.output`](/docs/directory-structure/output) directory with all your application, server and dependencies ready to be deployed. Checkout the [deployment](/docs/deployment) section to learn where and how you can deploy a Nuxt application using Nitro.
## Upgrade Nuxt3 version
To upgrade Nuxt3 version:
```bash
npx nuxi upgrade
```

View File

@ -2,4 +2,4 @@
> Nuxt Command Line Interface
Learn more about this package: https://v3.nuxtjs.org
Learn more about this package: https://v3.nuxtjs.org/getting-started/commands

View File

@ -11,7 +11,8 @@ export const commands = {
usage: () => import('./usage').then(_rDefault),
info: () => import('./info').then(_rDefault),
init: () => import('./init').then(_rDefault),
create: () => import('./init').then(_rDefault)
create: () => import('./init').then(_rDefault),
upgrade: () => import('./upgrade').then(_rDefault)
}
export type Command = keyof typeof commands

View File

@ -0,0 +1,68 @@
import { execSync } from 'child_process'
import { promises as fsp, existsSync } from 'fs'
import consola from 'consola'
import { resolve } from 'pathe'
import { resolveModule } from '../utils/cjs'
import { defineNuxtCommand } from './index'
async function getNuxtVersion (paths: string | string[]) {
const pkgJson = resolveModule('nuxt3/package.json', paths)
const pkg = pkgJson && JSON.parse(await fsp.readFile(pkgJson, 'utf8'))
if (!pkg.version) {
consola.warn('Cannot find any installed nuxt3 versions in ', paths)
}
return pkg.version || '0.0.0'
}
export default defineNuxtCommand({
meta: {
name: 'upgrade',
usage: 'npx nuxi upgrade [--force|-f]',
description: 'Upgrade nuxt3'
},
async invoke (args) {
const rootDir = resolve(args._[0] || '.')
const yarnLock = 'yarn.lock'
const npmLock = 'package-lock.json'
const isYarn = existsSync(resolve(rootDir, yarnLock))
const isNpm = existsSync(resolve(rootDir, npmLock))
const packageManager = isYarn ? 'yarn' : isNpm ? 'npm' : null
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)
const currentVersion = await getNuxtVersion(rootDir)
consola.info('Current nuxt version:', currentVersion)
if (args.force || args.f) {
consola.info('Removing lock-file and node_modules...')
await Promise.all([
fsp.rm(isYarn ? yarnLock : npmLock),
fsp.rmdir('node_modules', { recursive: true })
])
execSync(`${packageManager} install`, { stdio: 'inherit' })
} else {
consola.info('Upgrading nuxt...')
execSync(`${packageManager} ${isYarn ? 'add' : 'install'} nuxt3@latest`, { stdio: 'inherit' })
}
const upgradedVersion = await getNuxtVersion(rootDir)
consola.info('Upgraded nuxt version:', upgradedVersion)
if (upgradedVersion === currentVersion) {
consola.success('You\'re already using the latest version of nuxt3.')
} else {
consola.success('Successfully upgraded nuxt from', currentVersion, 'to', upgradedVersion)
const commitA = currentVersion.split('.').pop()
const commitB = upgradedVersion.split('.').pop()
if (commitA && commitB) {
consola.info('Changelog:', `https://github.com/nuxt/framework/compare/${commitA}...${commitB}`)
}
}
}
})