-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
browserPaths.ts
183 lines (167 loc) · 7.14 KB
/
browserPaths.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
/**
* Copyright 2017 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { execSync } from 'child_process';
import * as os from 'os';
import * as path from 'path';
import { getUbuntuVersionSync } from './ubuntuVersion';
import { getFromENV } from './utils';
export type BrowserName = 'chromium'|'webkit'|'firefox'|'ffmpeg';
export type BrowserPlatform = 'win32'|'win64'|'mac10.13'|'mac10.14'|'mac10.15'|'mac11'|'mac11-arm64'|'ubuntu18.04'|'ubuntu20.04';
export type BrowserDescriptor = {
name: BrowserName,
revision: string,
download: boolean,
};
export const hostPlatform = ((): BrowserPlatform => {
const platform = os.platform();
if (platform === 'darwin') {
const [major, minor] = execSync('sw_vers -productVersion', {
stdio: ['ignore', 'pipe', 'ignore']
}).toString('utf8').trim().split('.').map(x => parseInt(x, 10));
let arm64 = false;
// BigSur is the first version that might run on Apple Silicon.
if (major >= 11) {
arm64 = execSync('sysctl -in hw.optional.arm64', {
stdio: ['ignore', 'pipe', 'ignore']
}).toString().trim() === '1';
}
// We do not want to differentiate between minor big sur releases
// since they don't change core APIs so far.
const macVersion = major === 10 ? `${major}.${minor}` : `${major}`;
const archSuffix = arm64 ? '-arm64' : '';
return `mac${macVersion}${archSuffix}` as BrowserPlatform;
}
if (platform === 'linux') {
const ubuntuVersion = getUbuntuVersionSync();
if (parseInt(ubuntuVersion, 10) <= 19)
return 'ubuntu18.04';
return 'ubuntu20.04';
}
if (platform === 'win32')
return os.arch() === 'x64' ? 'win64' : 'win32';
return platform as BrowserPlatform;
})();
export function linuxLddDirectories(browserPath: string, browser: BrowserDescriptor): string[] {
if (browser.name === 'chromium')
return [path.join(browserPath, 'chrome-linux')];
if (browser.name === 'firefox')
return [path.join(browserPath, 'firefox')];
if (browser.name === 'webkit') {
return [
path.join(browserPath, 'minibrowser-gtk'),
path.join(browserPath, 'minibrowser-gtk', 'bin'),
path.join(browserPath, 'minibrowser-gtk', 'lib'),
path.join(browserPath, 'minibrowser-wpe'),
path.join(browserPath, 'minibrowser-wpe', 'bin'),
path.join(browserPath, 'minibrowser-wpe', 'lib'),
];
}
return [];
}
export function windowsExeAndDllDirectories(browserPath: string, browser: BrowserDescriptor): string[] {
if (browser.name === 'chromium')
return [path.join(browserPath, 'chrome-win')];
if (browser.name === 'firefox')
return [path.join(browserPath, 'firefox')];
if (browser.name === 'webkit')
return [browserPath];
return [];
}
export function executablePath(browserPath: string, browser: BrowserDescriptor): string | undefined {
let tokens: string[] | undefined;
if (browser.name === 'chromium') {
tokens = new Map<BrowserPlatform, string[]>([
['ubuntu18.04', ['chrome-linux', 'chrome']],
['ubuntu20.04', ['chrome-linux', 'chrome']],
['mac10.13', ['chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium']],
['mac10.14', ['chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium']],
['mac10.15', ['chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium']],
['mac11', ['chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium']],
['mac11-arm64', ['chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium']],
['win32', ['chrome-win', 'chrome.exe']],
['win64', ['chrome-win', 'chrome.exe']],
]).get(hostPlatform);
}
if (browser.name === 'firefox') {
tokens = new Map<BrowserPlatform, string[]>([
['ubuntu18.04', ['firefox', 'firefox']],
['ubuntu20.04', ['firefox', 'firefox']],
['mac10.13', ['firefox', 'Nightly.app', 'Contents', 'MacOS', 'firefox']],
['mac10.14', ['firefox', 'Nightly.app', 'Contents', 'MacOS', 'firefox']],
['mac10.15', ['firefox', 'Nightly.app', 'Contents', 'MacOS', 'firefox']],
['mac11', ['firefox', 'Nightly.app', 'Contents', 'MacOS', 'firefox']],
['mac11-arm64', ['firefox', 'Nightly.app', 'Contents', 'MacOS', 'firefox']],
['win32', ['firefox', 'firefox.exe']],
['win64', ['firefox', 'firefox.exe']],
]).get(hostPlatform);
}
if (browser.name === 'webkit') {
tokens = new Map<BrowserPlatform, string[] | undefined>([
['ubuntu18.04', ['pw_run.sh']],
['ubuntu20.04', ['pw_run.sh']],
['mac10.13', undefined],
['mac10.14', ['pw_run.sh']],
['mac10.15', ['pw_run.sh']],
['mac11', ['pw_run.sh']],
['mac11-arm64', ['pw_run.sh']],
['win32', ['Playwright.exe']],
['win64', ['Playwright.exe']],
]).get(hostPlatform);
}
if (browser.name === 'ffmpeg') {
tokens = new Map<BrowserPlatform, string[] | undefined>([
['ubuntu18.04', ['ffmpeg-linux']],
['ubuntu20.04', ['ffmpeg-linux']],
['mac10.13', ['ffmpeg-mac']],
['mac10.14', ['ffmpeg-mac']],
['mac10.15', ['ffmpeg-mac']],
['mac11', ['ffmpeg-mac']],
['mac11-arm64', ['ffmpeg-mac']],
['win32', ['ffmpeg-win32.exe']],
['win64', ['ffmpeg-win64.exe']],
]).get(hostPlatform);
}
return tokens ? path.join(browserPath, ...tokens) : undefined;
}
function cacheDirectory() {
if (process.platform === 'linux')
return process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
if (process.platform === 'darwin')
return path.join(os.homedir(), 'Library', 'Caches');
if (process.platform === 'win32')
return process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
throw new Error('Unsupported platform: ' + process.platform);
}
const defaultBrowsersPath = ((): string | undefined => {
const envDefined = getFromENV('PLAYWRIGHT_BROWSERS_PATH');
if (envDefined === '0')
return undefined;
return envDefined || path.join(cacheDirectory(), 'ms-playwright');
})();
export function browsersPath(packagePath: string): string {
return defaultBrowsersPath || path.join(packagePath, '.local-browsers');
}
export function browserDirectory(browsersPath: string, browser: BrowserDescriptor): string {
return path.join(browsersPath, `${browser.name}-${browser.revision}`);
}
export function markerFilePath(browsersPath: string, browser: BrowserDescriptor): string {
return path.join(browserDirectory(browsersPath, browser), 'INSTALLATION_COMPLETE');
}
export function isBrowserDirectory(browserPath: string): boolean {
const baseName = path.basename(browserPath);
return baseName.startsWith('chromium-') || baseName.startsWith('firefox-') || baseName.startsWith('webkit-') || baseName.startsWith('ffmpeg-');
}