-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathedgePath.ts
230 lines (199 loc) · 5.18 KB
/
edgePath.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
import { join } from "https://deno.land/std@0.182.0/path/mod.ts"
import exists from "./exists.ts"
const platform = Deno.build.os
// Caller will ensure that the platform is linux, otherwise null will be returned
async function getEdgeLinux(
name:
| "microsoft-edge-dev"
| "microsoft-edge-beta"
| "microsoft-edge-stable",
): Promise<string|null> {
try {
let path = await new Deno.Command('which', { args: [name] }).output()
return new TextDecoder().decode(path.stdout);
} catch (e) {}
return null
}
// Caller will ensure that the platform is windows, otherwise null will be returned
async function getEdgeWindows(
edgeDirName: "Edge" | "Edge Dev" | "Edge Beta" | "Edge SxS",
): Promise<string|null> {
const paths = []
const suffix = `\\Microsoft\\${edgeDirName}\\Application\\msedge.exe`
const prefixes = [
'LOCALAPPDATA',
'PROGRAMFILES',
"PROGRAMFILES(X86)",
].map(string => Deno.env.get(string)).filter(Boolean)
for (const prefix of prefixes) {
const edgePath = join(prefix!, suffix)
paths.push(edgePath)
if (await exists(edgePath)) {
return edgePath
}
}
return null
}
// Caller will ensure that the platform is macos, otherwise null will be returned
async function getEdgeDarwin(defaultPath: string): Promise<string|null> {
if (await exists(defaultPath)) {
return defaultPath
}
return null
}
const edgePaths = {
edge: {
linux: async () => await getEdgeLinux("microsoft-edge-stable"),
darwin: async () =>
await getEdgeDarwin(
"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
),
windows: async () => await getEdgeWindows("Edge"),
},
dev: {
linux: async () => await getEdgeLinux("microsoft-edge-dev"),
darwin: async () =>
await getEdgeDarwin(
"/Applications/Microsoft Edge Dev.app/Contents/MacOS/Microsoft Edge Dev",
),
windows: async () => await getEdgeWindows("Edge Dev"),
},
beta: {
linux: async () => await getEdgeLinux("microsoft-edge-beta"),
darwin: async () =>
await getEdgeDarwin(
"/Applications/Microsoft Edge Beta.app/Contents/MacOS/Microsoft Edge Beta",
),
windows: async () => await getEdgeWindows("Edge Beta"),
},
canary: {
// linux: getEdgeLinux("microsoft-edge-beta"),
darwin: async () =>
await getEdgeDarwin(
"/Applications/Microsoft Edge Canary.app/Contents/MacOS/Microsoft Edge Canary",
),
windows: async () => await getEdgeWindows("Edge SxS"),
},
}
// returns edge path
export async function getEdgePath(): Promise<string> {
const edge = edgePaths.edge
if (platform && platform in edgePaths.edge) {
const pth = await edge[platform as keyof typeof edge]()
if (pth) {
return pth
}
}
throwInvalidPlatformError("Edge Stable", edgePaths)
}
// Returns edge dev path
export async function getEdgeDevPath(): Promise<string> {
const edgeDev = edgePaths.dev
if (platform && platform in edgeDev) {
let pth = await edgeDev[platform as keyof typeof edgeDev]()
if (pth) {
return pth
}
}
throwInvalidPlatformError("Edge Dev", edgePaths)
}
// Returns edge beta path if it is available
export async function getEdgeBetaPath(): Promise<string> {
const edgeBeta = edgePaths.beta
if (platform && platform in edgeBeta) {
const pth = await edgeBeta[platform as keyof typeof edgeBeta]()
if (pth) {
return pth
}
}
throwInvalidPlatformError("Edge Beta", edgePaths)
}
// Returns edge canary paths.
export async function getEdgeCanaryPath(): Promise<string> {
const edgeCanary = edgePaths.canary
if (platform && platform in edgeCanary) {
const pth = await 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 async function getAnyEdgeLatest(): Promise<string> {
try {
return await getEdgeCanaryPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return await getEdgeDevPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return await getEdgeBetaPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return await 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 async function getAnyEdgeStable(): Promise<string> {
try {
return await getEdgePath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return await getEdgeBetaPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return await getEdgeDevPath()
} catch (e) {
throwIfNotEdgePathIssue(e)
}
try {
return await 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
}