Skip to content

Commit

Permalink
Promise interface
Browse files Browse the repository at this point in the history
Close #65
  • Loading branch information
isaacs committed Oct 3, 2019
1 parent 9f8d3b3 commit 3982ac0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ which('node', function (er, resolvedPath) {
// if it is found, then the absolute path to the exec is returned
})

// or promise
which('node').then(resolvedPath => { ... }).catch(er => { ... not found ... })

// sync usage
// throws if not found
var resolved = which.sync('node')
Expand Down
16 changes: 6 additions & 10 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ t.test('setup', function (t) {
})

t.test('does not find missed', function(t) {
t.plan(4)
t.plan(3)

which(fixture + '/foobar.sh', function (er) {
t.isa(er, Error)
t.equal(er.code, 'ENOENT')
t.rejects(which(fixture + '/foobar.sh'), {
code: 'ENOENT',
})

t.throws(function () {
Expand Down Expand Up @@ -181,13 +180,10 @@ t.test('find all', t => {
colon: ':',
all: true,
}
which('x.cmd', opt, (er, all) => {
if (er)
throw er
const allsync = which.sync('x.cmd', opt)
t.same(allsync, [`${fixture}/all/a/x.cmd`, `${fixture}/all/b/x.cmd`])
return which('x.cmd', opt).then(all => {
t.same(all, [`${fixture}/all/a/x.cmd`, `${fixture}/all/b/x.cmd`])
const allsync = which.sync('x.cmd', opt)
t.same(allsync, [`${fixture}/all/a/x.cmd`, `${fixture}/all/b/x.cmd`])
t.end()
})
})

Expand Down
49 changes: 25 additions & 24 deletions which.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@ const which = (cmd, opt, cb) => {
cb = opt
opt = {}
}
if (!opt)
opt = {}

const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt)
const found = []

const F = i => {
if (i === pathEnv.length) {
if (opt.all && found.length)
return cb(null, found)
else
return cb(getNotFoundError(cmd))
}
const step = i => new Promise((resolve, reject) => {
if (i === pathEnv.length)
return opt.all && found.length ? resolve(found)
: reject(getNotFoundError(cmd))

const ppRaw = pathEnv[i]
const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw
Expand All @@ -64,23 +63,25 @@ const which = (cmd, opt, cb) => {
const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd
: pCmd

const E = (ii) => {
if (ii === pathExt.length)
return F(i + 1)
const ext = pathExt[ii]
isexe(p + ext, { pathExt: pathExtExe }, (er, is) => {
if (!er && is) {
if (opt.all)
found.push(p + ext)
else
return cb(null, p + ext)
}
return E(ii + 1, pathExt.length)
})
}
return E(0)
}
return F(0)
resolve(subStep(p, i, 0))
})

const subStep = (p, i, ii) => new Promise((resolve, reject) => {
if (ii === pathExt.length)
return resolve(step(i + 1))
const ext = pathExt[ii]
isexe(p + ext, { pathExt: pathExtExe }, (er, is) => {
if (!er && is) {
if (opt.all)
found.push(p + ext)
else
return resolve(p + ext)
}
return resolve(subStep(p, i, ii + 1))
})
})

return cb ? step(0).then(res => cb(null, res), cb) : step(0)
}

const whichSync = (cmd, opt) => {
Expand Down

1 comment on commit 3982ac0

@GitSquared
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.