-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (56 loc) · 1.79 KB
/
index.js
File metadata and controls
68 lines (56 loc) · 1.79 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict'
var env = require('windows-env')
var path = require('path')
var which = require('which')
var existent = require('existent')
var cache_ = Object.create(null)
// Find an executable. If we're on 64-bit Windows
// prefer the native "%SystemRoot%\Sysnative\*.exe",
// because if Node.js is 32-bit we would be redirected to
// "%SystemRoot%\SysWow64\*.exe".
module.exports = function winbin (name, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
} else if (!opts) {
opts = {}
}
if (path.extname(name) !== '.exe') name += '.exe'
var preferNative = opts.native !== false && process.arch !== 'x64'
var cache = opts.cache !== false ? cache_ : Object.create(null)
var id = preferNative ? '64;' + name : '32;' + name
var search = cache[id] || (cache[id] = Object.create(null))
// Search was completed earlier.
if (search.result != null) return process.nextTick(emit.bind(null, cb))
// Search has already started
if (search.waiting != null) return search.waiting.push(cb)
else search.waiting = [cb]
// Usually "C:\Windows". Prefer SYSTEMROOT over WINDIR,
// because SYSTEMROOT is a read-only built-in variable.
var root = env.SYSTEMROOT
var def = path.join(root, 'system32', name)
if (env.X64 && preferNative) {
find(path.join(root, 'Sysnative', name), def)
} else {
find(def)
}
function find (bin, alt) {
existent(bin, function (err) {
if (!err) done(null, bin)
else if (alt) find(alt)
else which(path.basename(name, '.exe'), done)
})
}
function done (err, bin) {
search.result = [err, bin]
search.waiting.forEach(emit)
search.waiting = null
}
function emit (cb) {
cb.apply(null, search.result)
}
}
// For test purposes
module.exports.reset = function () {
cache_ = Object.create(null)
}