refactor: add a function for corepack context detection

This commit is contained in:
Zoltan Kochan
2024-08-02 23:01:04 +02:00
parent cb006df38c
commit 64e2e4f86f
4 changed files with 13 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/cli-meta": minor
---
Added isExecutedByCorepack.

View File

@@ -39,3 +39,7 @@ export const packageManager = {
export function detectIfCurrentPkgIsExecutable (proc: NodeJS.Process = process): boolean {
return 'pkg' in proc && proc.pkg != null
}
export function isExecutedByCorepack (env: NodeJS.ProcessEnv = process.env): boolean {
return env.COREPACK_ROOT != null
}

View File

@@ -1,5 +1,5 @@
import { PnpmError } from '@pnpm/error'
import { packageManager } from '@pnpm/cli-meta'
import { isExecutedByCorepack, packageManager } from '@pnpm/cli-meta'
import { logger, globalWarn } from '@pnpm/logger'
import { checkPackage, UnsupportedEngineError, type WantedEngine } from '@pnpm/package-is-installable'
import { type SupportedArchitectures } from '@pnpm/types'
@@ -24,7 +24,7 @@ export function packageIsInstallable (
const currentPnpmVersion = packageManager.name === 'pnpm'
? packageManager.version
: undefined
if (pkg.packageManager && !process.env.COREPACK_ROOT) {
if (pkg.packageManager && !isExecutedByCorepack()) {
const [pmName, pmReference] = pkg.packageManager.split('@')
if (pmName && pmName !== 'pnpm') {
const msg = `This project is configured to use ${pmName}`

View File

@@ -1,5 +1,5 @@
import { type UpdateCheckLog } from '@pnpm/core-loggers'
import { detectIfCurrentPkgIsExecutable } from '@pnpm/cli-meta'
import { detectIfCurrentPkgIsExecutable, isExecutedByCorepack } from '@pnpm/cli-meta'
import boxen from 'boxen'
import chalk from 'chalk'
import * as Rx from 'rxjs'
@@ -54,7 +54,7 @@ function renderUpdateMessage (opts: UpdateMessageOptions): string {
}
function renderUpdateCommand (opts: UpdateMessageOptions): string {
if (opts.env.COREPACK_ROOT) {
if (isExecutedByCorepack(opts.env)) {
return `corepack install -g pnpm@${opts.latestVersion}`
}
const pkgName = opts.currentPkgIsExecutable ? '@pnpm/exe' : 'pnpm'