fix(nuxi): fix version search path (#7133)

This commit is contained in:
pooya parsa 2022-09-01 12:56:18 +02:00 committed by GitHub
parent 68d12b468b
commit 62e38cd7e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 7 deletions

View File

@ -1,7 +1,7 @@
import { createRequire } from 'node:module'
import clear from 'clear' import clear from 'clear'
import { bold, gray, green } from 'colorette' import { bold, gray, green } from 'colorette'
import { version } from '../../package.json' import { version } from '../../package.json'
import { tryRequireModule } from './cjs'
export function showBanner (_clear?: boolean) { export function showBanner (_clear?: boolean) {
if (_clear) { clear() } if (_clear) { clear() }
@ -9,13 +9,8 @@ export function showBanner (_clear?: boolean) {
} }
export function showVersions (cwd: string) { export function showVersions (cwd: string) {
const _require = createRequire(cwd)
const getPkgVersion = (pkg: string) => { const getPkgVersion = (pkg: string) => {
try { return tryRequireModule(`${pkg}/package.json`, cwd)?.version || ''
const { version } = _require(`${pkg}/package.json`)
return version || ''
} catch { /* not found */ }
return ''
} }
const nuxtVersion = getPkgVersion('nuxt') || getPkgVersion('nuxt-edge') const nuxtVersion = getPkgVersion('nuxt') || getPkgVersion('nuxt-edge')
const nitroVersion = getPkgVersion('nitropack') const nitroVersion = getPkgVersion('nitropack')

View File

@ -29,6 +29,10 @@ export function requireModule (id: string, paths?: string | string[]) {
return _require(resolveModule(id, paths)) return _require(resolveModule(id, paths))
} }
export function tryRequireModule (id: string, paths?: string | string[]) {
try { return requireModule(id, paths) } catch { return null }
}
export function importModule (id: string, paths?: string | string[]) { export function importModule (id: string, paths?: string | string[]) {
const resolvedPath = resolveModule(id, paths) const resolvedPath = resolveModule(id, paths)
return import(pathToFileURL(resolvedPath).href) return import(pathToFileURL(resolvedPath).href)