-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
231 lines (200 loc) · 4.74 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { existsSync } from "fs"
import path from "path"
import which from "which"
let platform = process.platform
// Caller will ensure that the platform is linux, otherwise null will be returned
function getEdgeLinux(
name:
| "microsoft-edge-dev"
| "microsoft-edge-beta"
| "microsoft-edge-stable",
): string | null {
try {
let path = which.sync(name)
return path
} catch (e) {}
return null
}
// Caller will ensure that the platform is windows, otherwise null will be returned
function getEdgeWindows(
edgeDirName: "Edge" | "Edge Dev" | "Edge Beta" | "Edge SxS",
): string | null {
let paths = []
let suffix = `\\Microsoft\\${edgeDirName}\\Application\\msedge.exe`
let prefixes = [
process.env.LOCALAPPDATA,
process.env.PROGRAMFILES,
process.env["PROGRAMFILES(X86)"],
].filter((v) => !!v)
for (let prefix of prefixes) {
let edgePath = path.join(prefix!, suffix)
paths.push(edgePath)
if (existsSync(edgePath)) {
return edgePath
}
}
return null
}
// Caller will ensure that the platform is macos, otherwise null will be returned
function getEdgeDarwin(defaultPath: string): string | null {
if (existsSync(defaultPath)) {
return defaultPath
}
return null
}
const edgePaths = {
edge: {
linux: () => getEdgeLinux("microsoft-edge-stable"),
darwin: () =>
getEdgeDarwin(
"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
),
win32: () => getEdgeWindows("Edge"),
},
dev: {
linux: () => getEdgeLinux("microsoft-edge-dev"),
darwin: () =>
getEdgeDarwin(
"/Applications/Microsoft Edge Dev.app/Contents/MacOS/Microsoft Edge Dev",
),
win32: () => getEdgeWindows("Edge Dev"),
},
beta: {
linux: () => getEdgeLinux("microsoft-edge-beta"),
darwin: () =>
getEdgeDarwin(
"/Applications/Microsoft Edge Beta.app/Contents/MacOS/Microsoft Edge Beta",
),
win32: () => getEdgeWindows("Edge Beta"),
},
canary: {
// linux: getEdgeLinux("microsoft-edge-beta"),
darwin: () =>
getEdgeDarwin(
"/Applications/Microsoft Edge Canary.app/Contents/MacOS/Microsoft Edge Canary",
),
win32: () => getEdgeWindows("Edge SxS"),
},
}
// returns edge path
export function getEdgePath(): string {
let edge = edgePaths.edge
if (platform && platform in edgePaths.edge) {
let pth = edge[platform as keyof typeof edge]()
if (pth) {
return pth
}
}
throwInvalidPlatformError("Edge Stable", edgePaths)
}
// Returns edge dev path
export function getEdgeDevPath(): string {
let edgeDev = edgePaths.dev
if (platform && platform in edgeDev) {
let pth = edgeDev[platform as keyof typeof edgeDev]()
if (pth) {
return pth
}
}
throwInvalidPlatformError("Edge Dev", edgePaths)
}
// Returns edge beta path if it is available
export function getEdgeBetaPath(): string {
let edgeBeta = edgePaths.beta
if (platform && platform in edgeBeta) {
let pth = edgeBeta[platform as keyof typeof edgeBeta]()
if (pth) {
return pth
}
}
throwInvalidPlatformError("Edge Beta", edgePaths)
}
// Returns edge canary paths.
export function getEdgeCanaryPath(): string {
let edgeCanary = edgePaths.canary
if (platform && platform in edgeCanary) {
let pth = edgeCanary[platform as keyof typeof edgeCanary]()
if (pth) {
return pth
}
}
throwInvalidPlatformError("Edge Canary", edgePaths)
}
// This will try to get any edge from bleeding edge to most stable version
export function getAnyEdgeLatest(): string {
try {
return getEdgeCanaryPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return getEdgeDevPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return getEdgeBetaPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return getEdgePath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
throw {
name: "edge-paths",
message: `Unable to find any ms-edge-browser`,
}
}
// This will try to get edge from stable version to bleeding version
// Useful for playwright, puppeteer related stuff
export function getAnyEdgeStable(): string {
try {
return getEdgePath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return getEdgeBetaPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return getEdgeDevPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return getEdgeCanaryPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
throw {
name: "edge-paths",
message: `Unable to find any ms-edge-browser.`,
}
}
// Helpers
function throwInvalidPlatformError(
additionalInfo: string = "",
otherDetails?: any,
): never {
throw {
name: "edge-paths",
message: `Couldn't find the edge browser. ${additionalInfo}`,
additionalInfo,
otherDetails,
}
}
function throwIfNotEdgePathIssue(obj: any) {
if (
Object.prototype.toString.call(obj) === "[object Object]" &&
obj &&
obj.name &&
obj.name === "edge-paths"
) {
return
}
throw obj
}