feat: create pnpx for bundled pnpm (#8241)

close #8230
This commit is contained in:
Khải
2024-06-26 20:52:05 +07:00
committed by GitHub
parent ae4c4ee99a
commit 3beb895afe
2 changed files with 33 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-setup": minor
"pnpm": minor
---
Bundled `pnpm setup` now creates `pnpx` script [#8230](https://github.com/pnpm/pnpm/issues/8230).

View File

@@ -62,6 +62,32 @@ function copyCli (currentLocation: string, targetDir: string): void {
fs.copyFileSync(currentLocation, newExecPath)
}
function createPnpxScripts (targetDir: string): void {
// Why script files instead of aliases?
// 1. Aliases wouldn't work on all platform, such as Windows Command Prompt or POSIX `sh`.
// 2. Aliases wouldn't work on all environments, such as non-interactive shells and CI environments.
// 3. Aliases must be set for different shells while script files are limited to only 2 types: POSIX and Windows.
// 4. Aliases cannot be located with the `which` or `where` command.
// 5. Editing rc files is more error-prone than just write new files to the filesystem.
fs.mkdirSync(targetDir, { recursive: true })
const shellScript = [
'#!/bin/sh',
'exec pnpm dlx "$@"',
].join('\n')
fs.writeFileSync(path.join(targetDir, 'pnpx'), shellScript, { mode: 0o755 })
const batchScript = [
'@echo off',
'pnpm dlx %*',
].join('\n')
fs.writeFileSync(path.join(targetDir, 'pnpx.cmd'), batchScript)
const powershellScript = 'pnpm dlx $args'
fs.writeFileSync(path.join(targetDir, 'pnpx.ps1'), powershellScript)
}
export async function handler (
opts: {
force?: boolean
@@ -71,6 +97,7 @@ export async function handler (
const execPath = getExecPath()
if (execPath.match(/\.[cm]?js$/) == null) {
copyCli(execPath, opts.pnpmHomeDir)
createPnpxScripts(opts.pnpmHomeDir)
}
try {
const report = await addDirToEnvPath(opts.pnpmHomeDir, {