fix: global-bin-dir should look only for files inside exec dirs

PR #2793
This commit is contained in:
Zoltan Kochan
2020-08-22 13:32:22 +03:00
committed by GitHub
parent 4df806c7b2
commit 4d4d22b636
3 changed files with 37 additions and 5 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/global-bin-dir": patch
---
A directory is considered a valid global executable directory for pnpm, if it contains a node, or npm, or pnpm executable, not directory.

View File

@@ -61,8 +61,9 @@ const NODE_RELATED_COMMANDS = new Set(['pnpm', 'npm', 'node'])
function dirHasNodeRelatedCommand (dir: string) {
try {
const files = fs.readdirSync(dir)
return files.map((file) => file.toLowerCase())
return fs.readdirSync(dir, { withFileTypes: true })
.filter((entry) => entry.isFile())
.map(({ name }) => name.toLowerCase())
.some((file) => NODE_RELATED_COMMANDS.has(file.split('.')[0]))
} catch (err) {
return false

View File

@@ -12,9 +12,17 @@ const makePath =
: (...paths: string[]) => `/${path.join(...paths)}`
let canWriteToDir!: typeof _canWriteToDir
let readdirSync = (dir: string) => [] as string[]
let readdirSync = (dir: string) => [] as Array<{ name: string, isFile: () => boolean }>
const FAKE_PATH = 'FAKE_PATH'
function makeFileEntry (name: string) {
return { name, isFile: () => true }
}
function makeDirEntry (name: string) {
return { name, isFile: () => false }
}
const globalBinDir = proxiquire('../lib/index.js', {
'can-write-to-dir': {
sync: (dir: string) => canWriteToDir(dir),
@@ -152,13 +160,31 @@ test('select a directory that has a node command in it', (t) => {
].join(path.delimiter)
canWriteToDir = () => true
readdirSync = (dir) => dir === dir2 ? ['node'] : []
readdirSync = (dir) => dir === dir2 ? [makeFileEntry('node')] : []
t.equal(globalBinDir(), dir2)
process.env[FAKE_PATH] = pathEnv
t.end()
})
test('do not select a directory that has a node directory in it', (t) => {
const dir1 = makePath('foo')
const dir2 = makePath('bar')
const pathEnv = process.env[FAKE_PATH]
process.env[FAKE_PATH] = [
dir1,
dir2,
].join(path.delimiter)
canWriteToDir = () => true
readdirSync = (dir) => dir === dir2 ? [makeDirEntry('node')] : []
t.throws(() => globalBinDir(), /Couldn't find a suitable/)
process.env[FAKE_PATH] = pathEnv
t.end()
})
test('select a directory that has a node.bat command in it', (t) => {
const dir1 = makePath('foo')
const dir2 = makePath('bar')
@@ -169,7 +195,7 @@ test('select a directory that has a node.bat command in it', (t) => {
].join(path.delimiter)
canWriteToDir = () => true
readdirSync = (dir) => dir === dir2 ? ['node.bat'] : []
readdirSync = (dir) => dir === dir2 ? [makeFileEntry('node.bat')] : []
t.equal(globalBinDir(), dir2)
process.env[FAKE_PATH] = pathEnv