refactor(nuxi): info output package manager version (#1564)

This commit is contained in:
Anthony Fu 2021-10-29 16:41:04 +08:00 committed by GitHub
parent d623fc0419
commit 5eb8e072ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 36 deletions

View File

@ -1,11 +1,13 @@
import os from 'os'
import { existsSync, readFileSync } from 'fs'
import { createRequire } from 'module'
import { resolve, dirname } from 'pathe'
import { resolve } from 'pathe'
import jiti from 'jiti'
import destr from 'destr'
import { splitByCase } from 'scule'
import clipboardy from 'clipboardy'
import { getPackageManager, getPackageManagerVersion } from '../utils/packageManagers'
import { findup } from '../utils/fs'
import { defineNuxtCommand } from './index'
export default defineNuxtCommand({
@ -44,11 +46,18 @@ export default defineNuxtCommand({
? nuxtConfig.vite !== false
: (nuxtConfig?.buildModules?.find(m => m === 'nuxt-vite'))
let packageManager = getPackageManager(rootDir)
if (packageManager) {
packageManager += '@' + getPackageManagerVersion(packageManager)
} else {
packageManager = 'unknown'
}
const infoObj = {
OperatingSystem: os.type(),
NodeVersion: process.version,
NuxtVersion: nuxtVersion,
PackageManager: getPackageManager(rootDir),
PackageManager: packageManager,
Bundler: useVite ? 'Vite' : 'Webpack',
UserConfig: Object.keys(nuxtConfig).map(key => '`' + key + '`').join(', '),
RuntimeModules: listModules(nuxtConfig.modules),
@ -100,29 +109,6 @@ function normalizeConfigModule (module, rootDir) {
}
}
function findup (rootDir, fn) {
let dir = rootDir
while (dir !== dirname(dir)) {
const res = fn(dir)
if (res) {
return res
}
dir = dirname(dir)
}
return null
}
function getPackageManager (rootDir) {
return findup(rootDir, (dir) => {
if (existsSync(resolve(dir, 'yarn.lock'))) {
return 'Yarn'
}
if (existsSync(resolve(dir, 'package-lock.json'))) {
return 'npm'
}
}) || 'unknown'
}
function getNuxtConfig (rootDir) {
try {
return jiti(rootDir, { interopDefault: true })('./nuxt.config')

View File

@ -1,8 +1,9 @@
import { execSync } from 'child_process'
import { promises as fsp, existsSync } from 'fs'
import { promises as fsp } from 'fs'
import consola from 'consola'
import { resolve } from 'pathe'
import { resolveModule } from '../utils/cjs'
import { getPackageManager, packageManagerLocks } from '../utils/packageManagers'
import { defineNuxtCommand } from './index'
async function getNuxtVersion (paths: string | string[]) {
@ -23,14 +24,7 @@ export default defineNuxtCommand({
async invoke (args) {
const rootDir = resolve(args._[0] || '.')
const yarnLock = 'yarn.lock'
const npmLock = 'package-lock.json'
const pnpmLock = 'pnpm-lock.yaml'
const isYarn = existsSync(resolve(rootDir, yarnLock))
const isNpm = existsSync(resolve(rootDir, npmLock))
const isPnpm = existsSync(resolve(rootDir, pnpmLock))
const packageManager = isPnpm ? 'pnpm' : isYarn ? 'yarn' : isNpm ? 'npm' : null
const packageManager = getPackageManager(rootDir)
if (!packageManager) {
console.error('Cannot detect Package Manager in', rootDir)
process.exit(1)
@ -44,13 +38,13 @@ export default defineNuxtCommand({
if (args.force || args.f) {
consola.info('Removing lock-file and node_modules...')
await Promise.all([
fsp.rm(isPnpm ? pnpmLock : isYarn ? yarnLock : npmLock),
fsp.rm(packageManagerLocks[packageManager]),
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' })
execSync(`${packageManager} ${packageManager === 'yarn' ? 'add' : 'install'} nuxt3@latest`, { stdio: 'inherit' })
}
const upgradedVersion = await getNuxtVersion(rootDir)

View File

@ -1,6 +1,7 @@
import { promises as fsp } from 'fs'
import { promisify } from 'util'
import rimraf from 'rimraf'
import { dirname } from 'pathe'
// Check if a file exists
export async function exists (path: string) {
@ -16,3 +17,15 @@ export async function clearDir (path: string) {
await promisify(rimraf)(path)
await fsp.mkdir(path, { recursive: true })
}
export function findup<T> (rootDir: string, fn: (dir: string) => T | undefined): T | null {
let dir = rootDir
while (dir !== dirname(dir)) {
const res = fn(dir)
if (res) {
return res
}
dir = dirname(dir)
}
return null
}

View File

@ -0,0 +1,24 @@
import { execSync } from 'child_process'
import { existsSync } from 'fs'
import { resolve } from 'pathe'
import { findup } from './fs'
export const packageManagerLocks = {
yarn: 'yarn.lock',
npm: 'package-lock.json',
pnpm: 'pnpm-lock.yaml'
}
export function getPackageManager (rootDir: string) {
return findup(rootDir, (dir) => {
for (const name in packageManagerLocks) {
if (existsSync(resolve(dir, packageManagerLocks[name]))) {
return name
}
}
})
}
export function getPackageManagerVersion (name: string) {
return execSync(`${name} --version`).toString('utf8').trim()
}