Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions packages/nuxi/src/commands/module/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { parseINI } from 'confbox'
import { $fetch } from 'ofetch'
import { readPackageJSON } from 'pkg-types'
import { coerce, satisfies } from 'semver'
import { satisfies } from 'semver'

export const categories = [
'Analytics',
Expand Down Expand Up @@ -116,16 +115,6 @@ export function checkNuxtCompatibility(
})
}

export async function getNuxtVersion(cwd: string) {
const nuxtPkg = await readPackageJSON('nuxt', { url: cwd }).catch(() => null)
if (nuxtPkg) {
return nuxtPkg.version!
}
const pkg = await readPackageJSON(cwd)
const pkgDep = pkg?.dependencies?.nuxt || pkg?.devDependencies?.nuxt
return (pkgDep && coerce(pkgDep)?.version) || '3.0.0'
}

export function getRegistryFromContent(content: string, scope: string | null) {
try {
const npmConfig = parseINI<Record<string, string | undefined>>(content)
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxi/src/commands/module/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import { joinURL } from 'ufo'

import { runCommand } from '../../run'
import { logger } from '../../utils/logger'
import { getNuxtVersion } from '../../utils/versions'
import { cwdArgs, logLevelArgs } from '../_shared'
import { checkNuxtCompatibility, fetchModules, getNuxtVersion, getRegistryFromContent } from './_utils'
import { checkNuxtCompatibility, fetchModules, getRegistryFromContent } from './_utils'

interface RegistryMeta {
registry: string
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxi/src/commands/module/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Fuse from 'fuse.js'
import { kebabCase, upperFirst } from 'scule'

import { logger } from '../../utils/logger'
import { getNuxtVersion } from '../../utils/versions'
import { cwdArgs } from '../_shared'
import { checkNuxtCompatibility, fetchModules, getNuxtVersion } from './_utils'
import { checkNuxtCompatibility, fetchModules } from './_utils'

const { format: formatNumber } = Intl.NumberFormat('en-GB', {
notation: 'compact',
Expand Down
20 changes: 4 additions & 16 deletions packages/nuxi/src/commands/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,9 @@ import { loadKit } from '../utils/kit'
import { logger } from '../utils/logger'
import { cleanupNuxtDirs, nuxtVersionToGitIdentifier } from '../utils/nuxt'
import { getPackageManagerVersion } from '../utils/packageManagers'
import { getNuxtVersion } from '../utils/versions'
import { cwdArgs, legacyRootDirArgs, logLevelArgs } from './_shared'

async function getNuxtVersion(path: string): Promise<string | null> {
try {
const pkg = await readPackageJSON('nuxt', { url: path })
if (!pkg.version) {
logger.warn('Cannot find any installed Nuxt versions in ', path)
}
return pkg.version || null
}
catch {
return null
}
}

function checkNuxtDependencyType(pkg: PackageJson): 'dependencies' | 'devDependencies' {
if (pkg.dependencies?.nuxt) {
return 'dependencies'
Expand Down Expand Up @@ -109,7 +97,7 @@ export default defineCommand({
logger.info('Package manager:', packageManagerName, packageManagerVersion)

// Check currently installed Nuxt version
const currentVersion = (await getNuxtVersion(cwd)) || '[unknown]'
const currentVersion = (await getNuxtVersion(cwd, false)) || '[unknown]'
logger.info('Current Nuxt version:', currentVersion)

const pkg = await readPackageJSON(cwd).catch(() => null)
Expand Down Expand Up @@ -195,15 +183,15 @@ export default defineCommand({
await cleanupNuxtDirs(cwd, buildDir)

// Check installed Nuxt version again
const upgradedVersion = (await getNuxtVersion(cwd)) || '[unknown]'
const upgradedVersion = (await getNuxtVersion(cwd, false)) || '[unknown]'
logger.info('Upgraded Nuxt version:', upgradedVersion)

if (upgradedVersion === '[unknown]') {
return
}

if (upgradedVersion === currentVersion) {
logger.success('You\'re already using the latest version of Nuxt.')
logger.success('You\'re using the latest version of Nuxt.')
}
else {
logger.success(
Expand Down
12 changes: 12 additions & 0 deletions packages/nuxi/src/utils/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { readPackageJSON } from 'pkg-types'
import { coerce } from 'semver'

export async function getNuxtVersion(cwd: string, cache = true) {
const nuxtPkg = await readPackageJSON('nuxt', { url: cwd, try: true, cache })
if (nuxtPkg) {
return nuxtPkg.version!
}
const pkg = await readPackageJSON(cwd)
const pkgDep = pkg?.dependencies?.nuxt || pkg?.devDependencies?.nuxt
return (pkgDep && coerce(pkgDep)?.version) || '3.0.0'
}
3 changes: 2 additions & 1 deletion packages/nuxi/test/unit/commands/module/add.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { beforeAll, describe, expect, it, vi } from 'vitest'
import commands from '../../../../src/commands/module'
import * as utils from '../../../../src/commands/module/_utils'
import * as runCommands from '../../../../src/run'
import * as versions from '../../../../src/utils/versions'

const updateConfig = vi.fn(() => Promise.resolve())
const addDependency = vi.fn(() => Promise.resolve())
Expand Down Expand Up @@ -80,7 +81,7 @@ describe('module add', () => {
})
applyMocks()
vi.spyOn(runCommands, 'runCommand').mockImplementation(vi.fn())
vi.spyOn(utils, 'getNuxtVersion').mockResolvedValue('3.0.0')
vi.spyOn(versions, 'getNuxtVersion').mockResolvedValue('3.0.0')
vi.spyOn(utils, 'fetchModules').mockResolvedValue([
{
name: 'content',
Expand Down
Loading