Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aec59b9f6c | ||
|
|
76bfe9d01c | ||
|
|
188c8307ce | ||
|
|
b179ac1ba6 | ||
|
|
51e56d41e9 | ||
|
|
11687bb3d2 | ||
|
|
747414e7da |
Vendored
+152
-146
File diff suppressed because one or more lines are too long
+3
-1
@@ -3,6 +3,7 @@
|
||||
"scripts": {
|
||||
"build:bundle": "esbuild src/index.ts --bundle --platform=node --target=node24 --format=cjs --minify --outfile=dist/index.js --loader:.json=json",
|
||||
"build": "pnpm run build:bundle",
|
||||
"test": "vitest run",
|
||||
"start": "pnpm run build && sh ./run.sh"
|
||||
},
|
||||
"dependencies": {
|
||||
@@ -18,6 +19,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"esbuild": "^0.27.4",
|
||||
"typescript": "^5.3.3"
|
||||
"typescript": "^5.3.3",
|
||||
"vitest": "^4.1.2"
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+727
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,175 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { ensureAliasLinks } from './ensureAliasLinks'
|
||||
import { mkdtemp, mkdir, writeFile, readFile, readlink } from 'fs/promises'
|
||||
import { existsSync } from 'fs'
|
||||
import path from 'path'
|
||||
import os from 'os'
|
||||
|
||||
async function createTempDir (): Promise<string> {
|
||||
return mkdtemp(path.join(os.tmpdir(), 'alias-links-test-'))
|
||||
}
|
||||
|
||||
async function setupStandaloneFixture (binDir: string): Promise<void> {
|
||||
const exeDir = path.join(binDir, '..', '@pnpm', 'exe')
|
||||
await mkdir(exeDir, { recursive: true })
|
||||
// Only the pnpm binary exists — pn/pnpx/pnx may not exist after self-update
|
||||
await writeFile(path.join(exeDir, 'pnpm'), '#!/bin/sh\necho pnpm\n', { mode: 0o755 })
|
||||
}
|
||||
|
||||
async function setupNonStandaloneFixture (binDir: string): Promise<void> {
|
||||
const pnpmBinDir = path.join(binDir, '..', 'pnpm', 'bin')
|
||||
await mkdir(pnpmBinDir, { recursive: true })
|
||||
await writeFile(path.join(pnpmBinDir, 'pnpm.cjs'), 'console.log("pnpm")\n')
|
||||
}
|
||||
|
||||
describe('ensureAliasLinks', () => {
|
||||
let binDir: string
|
||||
|
||||
beforeEach(async () => {
|
||||
const tmpDir = await createTempDir()
|
||||
binDir = path.join(tmpDir, 'node_modules', '.bin')
|
||||
await mkdir(binDir, { recursive: true })
|
||||
})
|
||||
|
||||
describe('standalone mode', () => {
|
||||
it('creates pn as symlink to pnpm binary on unix', async () => {
|
||||
await setupStandaloneFixture(binDir)
|
||||
|
||||
await ensureAliasLinks(binDir, true, 'linux')
|
||||
|
||||
const pnTarget = await readlink(path.join(binDir, 'pn'))
|
||||
expect(pnTarget).toBe(path.join('..', '@pnpm', 'exe', 'pnpm'))
|
||||
})
|
||||
|
||||
it('creates pnpx and pnx as shell scripts calling pnpm dlx on unix', async () => {
|
||||
await setupStandaloneFixture(binDir)
|
||||
|
||||
await ensureAliasLinks(binDir, true, 'linux')
|
||||
|
||||
for (const name of ['pnpx', 'pnx']) {
|
||||
const content = await readFile(path.join(binDir, name), 'utf8')
|
||||
expect(content).toContain('pnpm')
|
||||
expect(content).toContain('dlx')
|
||||
expect(content).toContain('exec')
|
||||
}
|
||||
})
|
||||
|
||||
it('creates .cmd and .ps1 shims on windows', async () => {
|
||||
await setupStandaloneFixture(binDir)
|
||||
|
||||
await ensureAliasLinks(binDir, true, 'win32')
|
||||
|
||||
// pn shims
|
||||
const pnCmd = await readFile(path.join(binDir, 'pn.cmd'), 'utf8')
|
||||
expect(pnCmd).toContain('pnpm')
|
||||
expect(pnCmd).toContain('%*')
|
||||
expect(pnCmd).not.toContain('dlx')
|
||||
|
||||
const pnPs1 = await readFile(path.join(binDir, 'pn.ps1'), 'utf8')
|
||||
expect(pnPs1).toContain('pnpm')
|
||||
expect(pnPs1).toContain('@args')
|
||||
|
||||
// pnpx/pnx shims call pnpm dlx
|
||||
const pnpxCmd = await readFile(path.join(binDir, 'pnpx.cmd'), 'utf8')
|
||||
expect(pnpxCmd).toContain('pnpm')
|
||||
expect(pnpxCmd).toContain('dlx')
|
||||
|
||||
// Should not create extensionless files on windows
|
||||
expect(existsSync(path.join(binDir, 'pn'))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('non-standalone mode', () => {
|
||||
it('creates pn as symlink to pnpm.cjs on unix', async () => {
|
||||
await setupNonStandaloneFixture(binDir)
|
||||
|
||||
await ensureAliasLinks(binDir, false, 'linux')
|
||||
|
||||
const pnTarget = await readlink(path.join(binDir, 'pn'))
|
||||
expect(pnTarget).toBe(path.join('..', 'pnpm', 'bin', 'pnpm.cjs'))
|
||||
})
|
||||
|
||||
it('creates pnpx/pnx scripts on unix', async () => {
|
||||
await setupNonStandaloneFixture(binDir)
|
||||
|
||||
await ensureAliasLinks(binDir, false, 'linux')
|
||||
|
||||
const content = await readFile(path.join(binDir, 'pnpx'), 'utf8')
|
||||
expect(content).toContain('pnpm.cjs')
|
||||
expect(content).toContain('dlx')
|
||||
})
|
||||
|
||||
it('creates .cmd shims on windows', async () => {
|
||||
await setupNonStandaloneFixture(binDir)
|
||||
|
||||
await ensureAliasLinks(binDir, false, 'win32')
|
||||
|
||||
const cmdContent = await readFile(path.join(binDir, 'pn.cmd'), 'utf8')
|
||||
expect(cmdContent).toContain(path.join('pnpm', 'bin', 'pnpm.cjs'))
|
||||
})
|
||||
})
|
||||
|
||||
describe('skips when pnpm binary does not exist', () => {
|
||||
it('creates no links on unix', async () => {
|
||||
await ensureAliasLinks(binDir, true, 'linux')
|
||||
|
||||
expect(existsSync(path.join(binDir, 'pn'))).toBe(false)
|
||||
expect(existsSync(path.join(binDir, 'pnpx'))).toBe(false)
|
||||
expect(existsSync(path.join(binDir, 'pnx'))).toBe(false)
|
||||
})
|
||||
|
||||
it('creates no shims on windows', async () => {
|
||||
await ensureAliasLinks(binDir, true, 'win32')
|
||||
|
||||
expect(existsSync(path.join(binDir, 'pn.cmd'))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('self-update bin directory (pnpm shim in same dir)', () => {
|
||||
it('creates aliases using pnpm shim in the same directory on unix', async () => {
|
||||
// self-update creates a pnpm shim in $PNPM_HOME/bin/ — no package dir
|
||||
await writeFile(path.join(binDir, 'pnpm'), '#!/bin/sh\nexec /path/to/real/pnpm "$@"\n', { mode: 0o755 })
|
||||
|
||||
await ensureAliasLinks(binDir, true, 'linux')
|
||||
|
||||
const pnTarget = await readlink(path.join(binDir, 'pn'))
|
||||
expect(pnTarget).toBe('pnpm')
|
||||
|
||||
const pnxContent = await readFile(path.join(binDir, 'pnx'), 'utf8')
|
||||
expect(pnxContent).toContain('pnpm')
|
||||
expect(pnxContent).toContain('dlx')
|
||||
})
|
||||
|
||||
it('creates .cmd shims using pnpm in same dir on windows', async () => {
|
||||
await writeFile(path.join(binDir, 'pnpm'), 'pnpm binary')
|
||||
|
||||
await ensureAliasLinks(binDir, true, 'win32')
|
||||
|
||||
const cmdContent = await readFile(path.join(binDir, 'pn.cmd'), 'utf8')
|
||||
expect(cmdContent).toContain('pnpm')
|
||||
})
|
||||
})
|
||||
|
||||
describe('overwrites existing broken shims', () => {
|
||||
it('replaces npm broken shim with symlink on unix', async () => {
|
||||
await setupStandaloneFixture(binDir)
|
||||
// Simulate npm's broken shim pointing to .tools/ placeholder
|
||||
await writeFile(path.join(binDir, 'pn'), '#!/bin/sh\nexec .tools/broken "$@"\n')
|
||||
|
||||
await ensureAliasLinks(binDir, true, 'linux')
|
||||
|
||||
const target = await readlink(path.join(binDir, 'pn'))
|
||||
expect(target).toBe(path.join('..', '@pnpm', 'exe', 'pnpm'))
|
||||
})
|
||||
|
||||
it('replaces existing .cmd shims on windows', async () => {
|
||||
await setupStandaloneFixture(binDir)
|
||||
await writeFile(path.join(binDir, 'pn.cmd'), 'broken shim')
|
||||
|
||||
await ensureAliasLinks(binDir, true, 'win32')
|
||||
|
||||
const content = await readFile(path.join(binDir, 'pn.cmd'), 'utf8')
|
||||
expect(content).toContain('pnpm')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,84 @@
|
||||
import { unlink, writeFile, symlink } from 'fs/promises'
|
||||
import { existsSync } from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
function shScript (command: string): string {
|
||||
return `#!/bin/sh\nexec ${command} "$@"\n`
|
||||
}
|
||||
|
||||
function cmdShim (command: string): string {
|
||||
return `@ECHO off\r\n${command} %*\r\n`
|
||||
}
|
||||
|
||||
function pwshShim (command: string): string {
|
||||
return `#!/usr/bin/env pwsh\n${command} @args\n`
|
||||
}
|
||||
|
||||
async function forceSymlink (target: string, linkPath: string): Promise<void> {
|
||||
try { await unlink(linkPath) } catch {}
|
||||
await symlink(target, linkPath)
|
||||
}
|
||||
|
||||
async function forceWriteFile (filePath: string, content: string, mode?: number): Promise<void> {
|
||||
try { await unlink(filePath) } catch {}
|
||||
await writeFile(filePath, content, { mode })
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the pnpm binary/shim relative to binDir.
|
||||
* Checks the package directory first (node_modules/.bin/../@pnpm/exe/pnpm),
|
||||
* then falls back to a pnpm shim in binDir itself (e.g. self-update's bin/).
|
||||
*/
|
||||
function findPnpmTarget (binDir: string, standalone: boolean): string | undefined {
|
||||
const packageTarget = standalone
|
||||
? path.join('..', '@pnpm', 'exe', 'pnpm')
|
||||
: path.join('..', 'pnpm', 'bin', 'pnpm.cjs')
|
||||
|
||||
if (existsSync(path.resolve(binDir, packageTarget))) {
|
||||
return packageTarget
|
||||
}
|
||||
|
||||
// self-update creates a pnpm shim in $PNPM_HOME/bin/ — use it directly
|
||||
if (existsSync(path.join(binDir, 'pnpm'))) {
|
||||
return 'pnpm'
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Create pn/pnpx/pnx alias links in the bin directory.
|
||||
*
|
||||
* pn is an alias for pnpm, so it symlinks (or shims) to the pnpm binary.
|
||||
* pnpx/pnx are aliases for "pnpm dlx", created as shell scripts.
|
||||
*
|
||||
* This does NOT rely on the @pnpm/exe package having pn/pnx files, because
|
||||
* pnpm self-update only replaces the pnpm binary — it doesn't update other
|
||||
* files in the package. The aliases are created by pointing pn directly to
|
||||
* the pnpm binary, and pnpx/pnx as scripts that exec "pnpm dlx".
|
||||
*/
|
||||
export async function ensureAliasLinks (binDir: string, standalone: boolean, platform: NodeJS.Platform = process.platform): Promise<void> {
|
||||
const isWindows = platform === 'win32'
|
||||
|
||||
const pnpmTarget = findPnpmTarget(binDir, standalone)
|
||||
if (!pnpmTarget) return
|
||||
|
||||
if (isWindows) {
|
||||
// pn → calls pnpm directly
|
||||
await writeFile(path.join(binDir, 'pn.cmd'), cmdShim(`"%~dp0\\${pnpmTarget}"`))
|
||||
await writeFile(path.join(binDir, 'pn.ps1'), pwshShim(`& "$PSScriptRoot\\${pnpmTarget}"`))
|
||||
// pnpx/pnx → calls pnpm dlx
|
||||
for (const name of ['pnpx', 'pnx']) {
|
||||
await writeFile(path.join(binDir, `${name}.cmd`), cmdShim(`"%~dp0\\${pnpmTarget}" dlx`))
|
||||
await writeFile(path.join(binDir, `${name}.ps1`), pwshShim(`& "$PSScriptRoot\\${pnpmTarget}" dlx`))
|
||||
}
|
||||
} else {
|
||||
// pn → symlink to pnpm binary
|
||||
await forceSymlink(pnpmTarget, path.join(binDir, 'pn'))
|
||||
// pnpx/pnx → shell scripts that exec pnpm dlx
|
||||
for (const name of ['pnpx', 'pnx']) {
|
||||
const pnpmPath = `"$(dirname "$0")/${pnpmTarget}"`
|
||||
await forceWriteFile(path.join(binDir, name), shScript(`${pnpmPath} dlx`), 0o755)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import { Inputs } from '../inputs'
|
||||
import { parse as parseYaml } from 'yaml'
|
||||
import pnpmLock from './bootstrap/pnpm-lock.json'
|
||||
import exeLock from './bootstrap/exe-lock.json'
|
||||
import { ensureAliasLinks } from './ensureAliasLinks'
|
||||
|
||||
const BOOTSTRAP_PNPM_PACKAGE_JSON = JSON.stringify({ private: true, dependencies: { pnpm: pnpmLock.packages['node_modules/pnpm'].version } })
|
||||
const BOOTSTRAP_EXE_PACKAGE_JSON = JSON.stringify({ private: true, dependencies: { '@pnpm/exe': exeLock.packages['node_modules/@pnpm/exe'].version } })
|
||||
@@ -60,6 +61,16 @@ export async function runSelfInstaller(inputs: Inputs): Promise<number> {
|
||||
}
|
||||
}
|
||||
|
||||
// Create pn/pnx alias bin links if the installed version supports them
|
||||
// (pnpm v11+ adds pn and pnx as short aliases).
|
||||
// self-update links bins to $PNPM_HOME/bin/ which is also on PATH,
|
||||
// so we must create aliases in both directories.
|
||||
await ensureAliasLinks(pnpmHome, standalone)
|
||||
const pnpmBinDir = path.join(pnpmHome, 'bin')
|
||||
if (existsSync(pnpmBinDir)) {
|
||||
await ensureAliasLinks(pnpmBinDir, standalone)
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user