马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
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('node:path')
const { stat } = require('node: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,
}