blob: a9df98f5444813a14b417422f47f581ecb53016c [file] [log] [blame]
Randolf Jungbcb3bc82023-06-26 16:30:141/**
2 * Copyright 2023 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Randolf Jungbcb3bc82023-06-26 16:30:1416import fs from 'fs';
17import path from 'path';
18import { Browser } from './browser-data/browser-data.js';
Randolf Jung3e526312023-08-08 06:20:3919import { computeExecutablePath } from './launch.js';
20/**
21 * @public
22 */
23export class InstalledBrowser {
24 browser;
25 buildId;
26 platform;
27 #cache;
28 /**
29 * @internal
30 */
31 constructor(cache, browser, buildId, platform) {
32 this.#cache = cache;
33 this.browser = browser;
34 this.buildId = buildId;
35 this.platform = platform;
36 }
37 /**
38 * Path to the root of the installation folder. Use
39 * {@link computeExecutablePath} to get the path to the executable binary.
40 */
41 get path() {
42 return this.#cache.installationDir(this.browser, this.platform, this.buildId);
43 }
44 get executablePath() {
45 return computeExecutablePath({
46 cacheDir: this.#cache.rootDir,
47 platform: this.platform,
48 browser: this.browser,
49 buildId: this.buildId,
50 });
51 }
52}
Randolf Jungbcb3bc82023-06-26 16:30:1453/**
54 * The cache used by Puppeteer relies on the following structure:
55 *
56 * - rootDir
57 * -- <browser1> | browserRoot(browser1)
58 * ---- <platform>-<buildId> | installationDir()
59 * ------ the browser-platform-buildId
60 * ------ specific structure.
61 * -- <browser2> | browserRoot(browser2)
62 * ---- <platform>-<buildId> | installationDir()
63 * ------ the browser-platform-buildId
64 * ------ specific structure.
65 * @internal
66 */
67export class Cache {
Randolf Jung3e526312023-08-08 06:20:3968 #rootDir;
Randolf Jungbcb3bc82023-06-26 16:30:1469 constructor(rootDir) {
Randolf Jung3e526312023-08-08 06:20:3970 this.#rootDir = rootDir;
71 }
72 /**
73 * @internal
74 */
75 get rootDir() {
76 return this.#rootDir;
Randolf Jungbcb3bc82023-06-26 16:30:1477 }
78 browserRoot(browser) {
Randolf Jung3e526312023-08-08 06:20:3979 return path.join(this.#rootDir, browser);
Randolf Jungbcb3bc82023-06-26 16:30:1480 }
81 installationDir(browser, platform, buildId) {
82 return path.join(this.browserRoot(browser), `${platform}-${buildId}`);
83 }
84 clear() {
Randolf Jung3e526312023-08-08 06:20:3985 fs.rmSync(this.#rootDir, {
Randolf Jungbcb3bc82023-06-26 16:30:1486 force: true,
87 recursive: true,
88 maxRetries: 10,
89 retryDelay: 500,
90 });
91 }
92 uninstall(browser, platform, buildId) {
93 fs.rmSync(this.installationDir(browser, platform, buildId), {
94 force: true,
95 recursive: true,
96 maxRetries: 10,
97 retryDelay: 500,
98 });
99 }
100 getInstalledBrowsers() {
Randolf Jung3e526312023-08-08 06:20:39101 if (!fs.existsSync(this.#rootDir)) {
Randolf Jungbcb3bc82023-06-26 16:30:14102 return [];
103 }
Randolf Jung3e526312023-08-08 06:20:39104 const types = fs.readdirSync(this.#rootDir);
Randolf Jungbcb3bc82023-06-26 16:30:14105 const browsers = types.filter((t) => {
106 return Object.values(Browser).includes(t);
107 });
108 return browsers.flatMap(browser => {
109 const files = fs.readdirSync(this.browserRoot(browser));
110 return files
111 .map(file => {
112 const result = parseFolderPath(path.join(this.browserRoot(browser), file));
113 if (!result) {
114 return null;
115 }
Randolf Jung3e526312023-08-08 06:20:39116 return new InstalledBrowser(this, browser, result.buildId, result.platform);
Randolf Jungbcb3bc82023-06-26 16:30:14117 })
118 .filter((item) => {
119 return item !== null;
120 });
121 });
122 }
123}
Randolf Jungbcb3bc82023-06-26 16:30:14124function parseFolderPath(folderPath) {
125 const name = path.basename(folderPath);
126 const splits = name.split('-');
127 if (splits.length !== 2) {
128 return;
129 }
130 const [platform, buildId] = splits;
131 if (!buildId || !platform) {
132 return;
133 }
134 return { platform, buildId };
135}
136//# sourceMappingURL=Cache.js.map