1 const isWindows = process.platform === 'win32' ||
2 process.env.OSTYPE === 'cygwin' ||
3 process.env.OSTYPE === 'msys'
5 const path = require('path')
6 const COLON = isWindows ? ';' : ':'
7 const isexe = require('isexe')
9 const getNotFoundError = (cmd) =>
10 Object.assign(new Error(`not found: ${cmd}`), { code: 'ENOENT' })
12 const getPathInfo = (cmd, opt) => {
13 const colon = opt.colon || COLON
15 // If it has a slash, then we don't bother searching the pathenv.
16 // just check the file itself, and that's it.
17 const pathEnv = cmd.match(/\//) || isWindows && cmd.match(/\\/) ? ['']
20 // windows always checks the cwd first
21 ...(isWindows ? [process.cwd()] : []),
22 ...(opt.path || process.env.PATH ||
23 /* istanbul ignore next: very unusual */ '').split(colon),
26 const pathExtExe = isWindows
27 ? opt.pathExt || process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM'
29 const pathExt = isWindows ? pathExtExe.split(colon) : ['']
32 if (cmd.indexOf('.') !== -1 && pathExt[0] !== '')
43 const which = (cmd, opt, cb) => {
44 if (typeof opt === 'function') {
51 const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt)
54 const step = i => new Promise((resolve, reject) => {
55 if (i === pathEnv.length)
56 return opt.all && found.length ? resolve(found)
57 : reject(getNotFoundError(cmd))
59 const ppRaw = pathEnv[i]
60 const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw
62 const pCmd = path.join(pathPart, cmd)
63 const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd
66 resolve(subStep(p, i, 0))
69 const subStep = (p, i, ii) => new Promise((resolve, reject) => {
70 if (ii === pathExt.length)
71 return resolve(step(i + 1))
72 const ext = pathExt[ii]
73 isexe(p + ext, { pathExt: pathExtExe }, (er, is) => {
78 return resolve(p + ext)
80 return resolve(subStep(p, i, ii + 1))
84 return cb ? step(0).then(res => cb(null, res), cb) : step(0)
87 const whichSync = (cmd, opt) => {
90 const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt)
93 for (let i = 0; i < pathEnv.length; i ++) {
94 const ppRaw = pathEnv[i]
95 const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw
97 const pCmd = path.join(pathPart, cmd)
98 const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd
101 for (let j = 0; j < pathExt.length; j ++) {
102 const cur = p + pathExt[j]
104 const is = isexe.sync(cur, { pathExt: pathExtExe })
115 if (opt.all && found.length)
121 throw getNotFoundError(cmd)
124 module.exports = which
125 which.sync = whichSync