Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/tools/impl/chromium.ts
12925 views
1
/*
2
* chromium.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import {
8
chromiumInstallDir,
9
fetcher,
10
getPuppeteer,
11
} from "../../core/puppeteer.ts";
12
import {
13
clearLine,
14
progressBar,
15
spinner,
16
withSpinner,
17
} from "../../core/console.ts";
18
19
import { InstallableTool, InstallContext, PackageInfo } from "../types.ts";
20
21
async function installDir() {
22
if (await installed()) {
23
return Promise.resolve(chromiumInstallDir());
24
} else {
25
return Promise.resolve(undefined);
26
}
27
}
28
29
export const chromiumInstallable: InstallableTool = {
30
name: "Chromium",
31
prereqs: [],
32
installed,
33
installedVersion,
34
latestRelease,
35
preparePackage,
36
install,
37
afterInstall,
38
uninstall,
39
installDir,
40
};
41
42
export async function installed(): Promise<boolean> {
43
const fetcherObj = await fetcher();
44
const version = await supportedRevision();
45
const localRevisions = await fetcherObj.localRevisions();
46
return localRevisions.includes(version);
47
}
48
49
async function installedVersion(): Promise<string | undefined> {
50
const fetcherObj = await fetcher();
51
const localRevisions = await fetcherObj.localRevisions();
52
if (localRevisions.length) {
53
return localRevisions[localRevisions.length - 1];
54
}
55
}
56
57
async function latestRelease() {
58
const fetcherObj = await fetcher();
59
const version = await supportedRevision();
60
const revisionInfo = await fetcherObj.revisionInfo(version);
61
62
return Promise.resolve({
63
url: revisionInfo.url,
64
version,
65
assets: [{ name: "", url: "" }],
66
});
67
}
68
69
async function preparePackage(_ctx: InstallContext): Promise<PackageInfo> {
70
const revision = await supportedRevision();
71
72
const progress = progressBar(
73
100,
74
`Downloading Chromium ${revision}`,
75
);
76
// const spin = spinner("Installing");
77
let spinnerStatus: (() => void) | undefined;
78
const fetcherObj = await fetcher();
79
const revisionInfo = await fetcherObj.download(
80
revision,
81
(x: number, total: number) => {
82
const percent = x / total * 100;
83
if (percent < 100) {
84
progress.update(percent);
85
} else {
86
progress.complete(true);
87
clearLine();
88
spinnerStatus = spinner(`Installing Chromium ${revision}`);
89
}
90
},
91
);
92
if (spinnerStatus !== undefined) {
93
spinnerStatus();
94
}
95
96
return {
97
filePath: revisionInfo.folderPath,
98
version: revisionInfo.revision,
99
};
100
}
101
102
function install(_pkg: PackageInfo, _ctx: InstallContext): Promise<void> {
103
return Promise.resolve();
104
}
105
106
function afterInstall(_ctx: InstallContext): Promise<boolean> {
107
return Promise.resolve(false);
108
}
109
110
async function uninstall(_ctx: InstallContext): Promise<void> {
111
await withSpinner({
112
message: "Removing Chromium...",
113
}, async () => {
114
const fetcherObj = await fetcher();
115
const version = await supportedRevision();
116
return fetcherObj.remove(version);
117
});
118
return Promise.resolve();
119
}
120
121
// TODO: https://github.com/puppeteer/puppeteer/blob/main/versions.js
122
async function supportedRevision(): Promise<string> {
123
return (await getPuppeteer())._preferredRevision;
124
}
125
126