-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathinstall.ts
More file actions
103 lines (96 loc) · 3.08 KB
/
install.ts
File metadata and controls
103 lines (96 loc) · 3.08 KB
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
import puppeteer from "./mod.ts";
import { PUPPETEER_REVISIONS } from "./vendor/puppeteer-core/puppeteer/revisions.js";
import ProgressBar from "https://deno.land/x/progress@v1.1.4/mod.ts";
/** Logging verbosity. */
export type LogLevel = "default" | "minimal";
/**
* Options to use when downloading.
*/
export interface InstallOptions {
/** Print log messages. */
enableLog?: boolean;
/** Which logs to print. */
logLevel?: LogLevel;
/** Browser to install. */
product?: "chrome" | "firefox";
/** Chrome or Firefox version to install. */
revision?: string;
}
const DEFAULT_OPTIONS: InstallOptions = {
enableLog: true,
logLevel: "default",
product: "chrome",
};
/**
* Install and cache a suitable browser to use for puppeteer.
*
* @param options Install options.
*/
export async function installPuppeteer(
options: InstallOptions = DEFAULT_OPTIONS,
) {
const logLevel = options.logLevel || "default";
let product = Deno.env.get("PUPPETEER_PRODUCT") || options.product;
if (product != "chrome" && product != "firefox") {
if (product != undefined && options.enableLog && logLevel === "default") {
console.warn(`Unknown product '${product}', falling back to 'chrome'.`);
}
product = "chrome";
}
const fetcher = puppeteer.createBrowserFetcher({ product });
let revision;
if (product == "chrome") {
revision = Deno.env.get("PUPPETEER_CHROMIUM_REVISION") ||
options.revision ||
PUPPETEER_REVISIONS.chromium;
} else if (product == "firefox") {
puppeteer._preferredRevision = options.revision ||
PUPPETEER_REVISIONS.firefox;
const req = await fetch(
"https://product-details.mozilla.org/1.0/firefox_versions.json",
);
const versions = await req.json();
revision = versions.FIREFOX_NIGHTLY;
if (!versions.FIREFOX_NIGHTLY) {
throw new Error("Firefox version not found");
}
}
const revisionInfo = fetcher.revisionInfo(revision);
if (revisionInfo.local) {
if (options.enableLog && logLevel === "default") {
console.log(`Already downloaded at ${revisionInfo.executablePath}`);
}
} else {
if (options.enableLog && logLevel === "minimal") {
console.log(`Downloading ${product}`);
}
let progressBar: ProgressBar;
const newRevisionInfo = await fetcher.download(
revisionInfo.revision,
(current, total) => {
if (!options.enableLog || (options.enableLog && logLevel === "minimal")) {
return;
}
if (!progressBar) {
progressBar = new ProgressBar({
total,
});
}
if (!(progressBar as any).isCompleted) {
progressBar.render(current);
} else if (options.enableLog && logLevel === "default") {
console.log("Done downloading. Installing now.");
}
},
);
if (options.enableLog && logLevel === "default") {
console.log(
`Downloaded ${newRevisionInfo.product} ${newRevisionInfo.revision} to ${newRevisionInfo.executablePath} from ${newRevisionInfo.url}`,
);
}
}
}
if (import.meta.main) {
await installPuppeteer();
Deno.exit(0);
}