-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
70 lines (61 loc) · 1.81 KB
/
index.js
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
69
70
var fs = require('fs');
var exec = require('child_process').exec;
var Promise = require('es6-promise').Promise;
var userhome = require('userhome');
var queue = require('queue-async');
module.exports = function(cb) {
return new Promise(function(resolve) {
var finisher = cb || function(r) {
resolve(r);
};
if (process.platform === 'darwin') {
getOSXPath(finisher);
} else if (process.platform === 'win32') {
getWinPath(finisher);
} else {
getLinuxPath(finisher);
}
});
}
function getOSXPath(finisher) {
var toExec = '/Contents/MacOS/Google Chrome';
var regPath = '/Applications/Google Chrome.app' + toExec;
var altPath = userhome(regPath.slice(1));
var mdFindCmd = 'mdfind \'kMDItemDisplayName == "Google Chrome" && kMDItemKind == Application\'';
queue(1)
.defer(tryLocation, regPath, finisher)
.defer(tryLocation, altPath, finisher)
.defer(tryMd)
.awaitAll(function() { finisher(null); });
function tryMd(next) {
exec(mdFindCmd, function(err, stdout) {
if (err || !stdout) next();
else finisher(stdout.trim() + toExec);
})
}
}
function getWinPath(finisher) {
var winSuffix = '\\Google\\Chrome\\Application\\chrome.exe';
var prefixes = [
process.env.LOCALAPPDATA,
process.env.PROGRAMFILES,
process.env['PROGRAMFILES(X86)']
];
queue(1)
.defer(tryLocation, prefixes[0] + winSuffix, finisher)
.defer(tryLocation, prefixes[1] + winSuffix, finisher)
.defer(tryLocation, prefixes[2] + winSuffix, finisher)
.awaitAll(function() { finisher(null); });
}
function getLinuxPath(finisher) {
exec('which google-chrome', function(err, r) {
if (err) throw err;
finisher(r.trim());
});
}
function tryLocation(p, success, next) {
fs.exists(p, function(exists) {
if (exists) success(p);
else next();
});
}