feat: pnpm setup should move the CLI to the pnpm home dir (#3677)

ref #3633
This commit is contained in:
Zoltan Kochan
2021-08-18 02:06:47 +03:00
committed by GitHub
parent ed184a9c5b
commit 8d038f8f1c
4 changed files with 43 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-setup": minor
---
pnpm setup moves the CLI to the pnpm home directory.

View File

@@ -33,7 +33,11 @@
"render-help": "^1.0.1"
},
"funding": "https://opencollective.com/pnpm",
"peerDependencies": {
"@pnpm/logger": "^4.0.0"
},
"devDependencies": {
"@pnpm/logger": "^4.0.0",
"@pnpm/prepare": "workspace:0.0.26"
}
}

View File

@@ -2,6 +2,7 @@ import fs from 'fs'
import os from 'os'
import path from 'path'
import { docsUrl } from '@pnpm/cli-utils'
import logger from '@pnpm/logger'
import renderHelp from 'render-help'
export const rcOptionsTypes = () => ({})
@@ -22,12 +23,43 @@ export function help () {
})
}
function getExecPath () {
if (process['pkg'] != null) {
// If the pnpm CLI was bundled by vercel/pkg then we cannot use the js path for npm_execpath
// because in that case the js is in a virtual filesystem inside the executor.
// Instead, we use the path to the exe file.
return process.execPath
}
return (require.main != null) ? require.main.filename : process.cwd()
}
function moveCli (currentLocation: string, targetDir: string) {
const newExecPath = path.join(targetDir, path.basename(currentLocation))
if (path.relative(newExecPath, currentLocation) === '') return
logger.info({
message: `Moving pnpm CLI from ${currentLocation} to ${newExecPath}`,
prefix: process.cwd(),
})
try {
fs.renameSync(currentLocation, newExecPath)
} catch (err) {
fs.copyFileSync(currentLocation, newExecPath)
try {
fs.unlinkSync(currentLocation)
} catch (err) {}
}
}
export async function handler (
opts: {
pnpmHomeDir: string
}
) {
const currentShell = process.env.SHELL ? path.basename(process.env.SHELL) : null
const execPath = getExecPath()
if (execPath.match(/\.[cm]?js$/) == null) {
moveCli(execPath, opts.pnpmHomeDir)
}
switch (currentShell) {
case 'bash': {
const configFile = path.join(os.homedir(), '.bashrc')

2
pnpm-lock.yaml generated
View File

@@ -2366,6 +2366,7 @@ importers:
packages/plugin-commands-setup:
specifiers:
'@pnpm/cli-utils': workspace:0.6.16
'@pnpm/logger': ^4.0.0
'@pnpm/plugin-commands-setup': 'link:'
'@pnpm/prepare': workspace:0.0.26
render-help: ^1.0.1
@@ -2373,6 +2374,7 @@ importers:
'@pnpm/cli-utils': link:../cli-utils
render-help: 1.0.2
devDependencies:
'@pnpm/logger': 4.0.0
'@pnpm/plugin-commands-setup': 'link:'
'@pnpm/prepare': link:../../privatePackages/prepare