summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/libnpmexec/lib/file-exists.js
blob: 0a8d79e83b2569b0fe8850f317576694876afb42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const { resolve } = require('path')
const { stat } = require('fs/promises')
const walkUp = require('walk-up-path')

const fileExists = async (file) => {
  try {
    const res = await stat(file)
    return res.isFile()
  } catch {
    return false
  }
}

const localFileExists = async (dir, binName, root) => {
  for (const path of walkUp(dir)) {
    const binDir = resolve(path, 'node_modules', '.bin')

    if (await fileExists(resolve(binDir, binName))) {
      return binDir
    }

    if (path.toLowerCase() === resolve(root).toLowerCase()) {
      return false
    }
  }

  return false
}

module.exports = {
  fileExists,
  localFileExists,
}