-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
index.ts
182 lines (151 loc) · 4.18 KB
/
index.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import fse from 'fs-extra'
import os from 'os'
import { join, normalize } from 'path'
import { tap, trim, prop } from 'ramda'
import { get } from 'lodash'
import { notInstalledErr } from '../errors'
import { log } from '../log'
import { Browser, FoundBrowser } from '../types'
import { utils } from '../utils'
function formFullAppPath (name: string) {
const prefix = 'C:/Program Files (x86)/Google/Chrome/Application'
return [normalize(join(prefix, `${name}.exe`))]
}
function formChromiumAppPath () {
const exe = 'C:/Program Files (x86)/Google/chrome-win32/chrome.exe'
return [normalize(exe)]
}
function formChromeCanaryAppPath () {
const home = os.homedir()
const exe = join(
home,
'AppData',
'Local',
'Google',
'Chrome SxS',
'Application',
'chrome.exe',
)
return [normalize(exe)]
}
function getFirefoxPaths (editionFolder) {
return () => {
return (['Program Files', 'Program Files (x86)'])
.map((programFiles) => {
return normalize(`C:/${programFiles}/${editionFolder}/firefox.exe`)
})
}
}
function formEdgeCanaryAppPath () {
const home = os.homedir()
const exe = join(
home,
'AppData',
'Local',
'Microsoft',
'Edge SxS',
'Application',
'msedge.exe',
)
return [normalize(exe)]
}
type NameToPath = (name: string) => string[]
type WindowsBrowserPaths = {
[name: string]: {
[channel: string]: NameToPath
}
}
const formPaths: WindowsBrowserPaths = {
chrome: {
stable: formFullAppPath,
canary: formChromeCanaryAppPath,
},
chromium: {
stable: formChromiumAppPath,
},
firefox: {
stable: getFirefoxPaths('Mozilla Firefox'),
dev: getFirefoxPaths('Firefox Developer Edition'),
nightly: getFirefoxPaths('Firefox Nightly'),
},
edge: {
stable: () => {
return [normalize('C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe')]
},
beta: () => {
return [normalize('C:/Program Files (x86)/Microsoft/Edge Beta/Application/msedge.exe')]
},
dev: () => {
return [normalize('C:/Program Files (x86)/Microsoft/Edge Dev/Application/msedge.exe')]
},
canary: formEdgeCanaryAppPath,
},
}
function getWindowsBrowser (browser: Browser): Promise<FoundBrowser> {
const getVersion = (stdout: string): string => {
// result from wmic datafile
// "Version=61.0.3163.100"
const wmicVersion = /^Version=(\S+)$/
const m = wmicVersion.exec(stdout)
if (m) {
return m[1]
}
log('Could not extract version from %s using regex %s', stdout, wmicVersion)
throw notInstalledErr(browser.name)
}
const formFullAppPathFn: NameToPath = get(formPaths, [browser.name, browser.channel], formFullAppPath)
const exePaths = formFullAppPathFn(browser.name)
log('looking at possible paths... %o', { browser, exePaths })
// shift and try paths 1-by-1 until we find one that works
const tryNextExePath = async () => {
const exePath = exePaths.shift()
if (!exePath) {
// exhausted available paths
throw notInstalledErr(browser.name)
}
return fse.pathExists(exePath)
.then((exists) => {
log('found %s ?', exePath, exists)
if (!exists) {
return tryNextExePath()
}
return getVersionString(exePath)
.then(tap(log))
.then(getVersion)
.then((version: string) => {
log('browser %s at \'%s\' version %s', browser.name, exePath, version)
return {
name: browser.name,
version,
path: exePath,
} as FoundBrowser
})
})
.catch((err) => {
log('error while looking up exe, trying next exePath %o', { exePath, exePaths, err })
return tryNextExePath()
})
}
return tryNextExePath()
}
export function getVersionString (path: string) {
const doubleEscape = (s: string) => {
return s.replace(/\\/g, '\\\\')
}
// on Windows using "--version" seems to always start the full
// browser, no matter what one does.
const args = [
'datafile',
'where',
`name="${doubleEscape(path)}"`,
'get',
'Version',
'/value',
]
return utils.execa('wmic', args)
.then(prop('stdout'))
.then(trim)
}
export function detect (browser: Browser) {
return getWindowsBrowser(browser)
}