Skip to content

Commit

Permalink
fix(exec): check if globalPath exists before passing to arborist
Browse files Browse the repository at this point in the history
  • Loading branch information
milaninfy committed Jul 11, 2024
1 parent dbe7d98 commit bee942e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions workspaces/libnpmexec/lib/file-exists.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ const fileExists = async (file) => {
}
}

const directoryExists = async (path) => {
try {
const res = await stat(path)
return res.isDirectory()
} catch {
return false
}
}

const localFileExists = async (dir, binName, root) => {
for (const path of walkUp(dir)) {
const binDir = resolve(path, 'node_modules', '.bin')
Expand All @@ -30,4 +39,5 @@ const localFileExists = async (dir, binName, root) => {
module.exports = {
fileExists,
localFileExists,
directoryExists,
}
4 changes: 2 additions & 2 deletions workspaces/libnpmexec/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const npa = require('npm-package-arg')
const pacote = require('pacote')
const { read } = require('read')
const semver = require('semver')
const { fileExists, localFileExists } = require('./file-exists.js')
const { fileExists, localFileExists, directoryExists } = require('./file-exists.js')
const getBinFromManifest = require('./get-bin-from-manifest.js')
const noTTY = require('./no-tty.js')
const runScript = require('./run-script.js')
Expand Down Expand Up @@ -201,7 +201,7 @@ const exec = async (opts) => {

args[0] = getBinFromManifest(commandManifest)

if (needInstall.length > 0 && globalPath) {
if (needInstall.length > 0 && await directoryExists(globalPath)) {
// See if the package is installed globally, and run the translated bin
const globalArb = new Arborist({ ...flatOptions, path: globalPath, global: true })
const globalTree = await globalArb.loadActual()
Expand Down

0 comments on commit bee942e

Please sign in to comment.