fix(config): global dir location (#3623)

This commit is contained in:
Zoltan Kochan
2021-07-28 03:57:39 +03:00
committed by GitHub
parent af263cc325
commit 73c1f802e2
2 changed files with 20 additions and 6 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/config": patch
---
Choose the right location for global dir.

View File

@@ -25,6 +25,8 @@ export { Config, UniversalOptions }
const npmDefaults = loadNpmConf.defaults
const PNPM_GLOBAL = 'pnpm-global'
async function which (cmd: string) {
return new Promise<string>((resolve, reject) => {
whichcb(cmd, (err, resolvedPath) => (err != null) ? reject(err) : resolve(resolvedPath!))
@@ -254,22 +256,29 @@ export default async (
: pnpmConfig['sharedWorkspaceLockfile']
if (cliOptions['global']) {
const npmGlobalPrefix: string = findBestGlobalPrefix(npmConfig.globalPrefix, process.env)
const knownGlobalBinDirCandidates: string[] = []
let npmGlobalPrefix: string = findBestGlobalPrefix(npmConfig.globalPrefix, process.env)
const globalDirName = `${path.sep}${PNPM_GLOBAL}${path.sep}`
if (npmGlobalPrefix.includes(globalDirName)) {
npmGlobalPrefix = npmGlobalPrefix.substring(0, npmGlobalPrefix.indexOf(globalDirName))
} else {
const npmGlobalBinDir = process.platform === 'win32'
? npmGlobalPrefix
: path.resolve(npmGlobalPrefix, 'bin')
knownGlobalBinDirCandidates.push(npmGlobalBinDir)
}
const globalDirRoot = pnpmConfig['globalDir']
? pnpmConfig['globalDir']
: path.join(firstWithWriteAccess([npmGlobalPrefix, os.homedir()]), 'pnpm-global')
: path.join(firstWithWriteAccess([npmGlobalPrefix, os.homedir()]), PNPM_GLOBAL)
pnpmConfig.dir = path.join(globalDirRoot, LAYOUT_VERSION.toString())
const npmGlobalBinDir = process.platform === 'win32'
? npmGlobalPrefix
: path.resolve(npmGlobalPrefix, 'bin')
pnpmConfig.bin = cliOptions.dir
? (
process.platform === 'win32'
? cliOptions.dir
: path.resolve(cliOptions.dir, 'bin')
)
: globalBinDir([npmGlobalBinDir], { shouldAllowWrite: opts.globalDirShouldAllowWrite === true })
: globalBinDir(knownGlobalBinDirCandidates, { shouldAllowWrite: opts.globalDirShouldAllowWrite === true })
pnpmConfig.save = true
pnpmConfig.allowNew = true
pnpmConfig.ignoreCurrentPrefs = true