|
1 | 1 | import { execSync } from "node:child_process"; |
2 | 2 | import process from "node:process"; |
3 | | -import { isLinux } from "std-env"; |
4 | | -import { logger } from "./log.js"; |
| 3 | +import { isDebug, isLinux } from "std-env"; |
| 4 | +import { LOG_LEVEL, logger } from "./log.js"; |
5 | 5 |
|
6 | | -export async function installXvfb() { |
7 | | - logger.debug("Installing xvfb..."); |
8 | | - if (!isLinux) { |
9 | | - logger.error("Unsupported platform. Please install Xvfb manually."); |
10 | | - process.exit(1); |
11 | | - } |
| 6 | +function isPackageInstalled(packageName: string): boolean { |
12 | 7 | try { |
13 | | - execSync("which xvfb", { stdio: "ignore" }); |
| 8 | + execSync(`dpkg-query -W ${packageName}`, { stdio: "ignore" }); |
| 9 | + return true; |
14 | 10 | } |
15 | 11 | catch { |
16 | | - try { |
17 | | - const osId = execSync("cat /etc/os-release | grep '^ID='").toString(); |
18 | | - if (osId.includes("ubuntu") || osId.includes("debian")) { |
19 | | - logger.debug("Detected Ubuntu/Debian. Installing Xvfb..."); |
20 | | - execSync("sudo apt-get update && sudo apt-get install -y xvfb", { stdio: "pipe" }); |
21 | | - } |
22 | | - else if (osId.includes("centos") || osId.includes("rhel")) { |
23 | | - logger.debug("Detected CentOS/RHEL. Installing Xvfb..."); |
24 | | - execSync("sudo yum install -y xorg-x11-server-Xvfb", { stdio: "pipe" }); |
25 | | - } |
26 | | - else { |
27 | | - throw new Error("Unsupported Linux distribution."); |
28 | | - } |
29 | | - logger.debug("Xvfb installation completed."); |
| 12 | + return false; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +function installPackage(packageName: string): void { |
| 17 | + const debug = isDebug || logger.level <= LOG_LEVEL.debug; |
| 18 | + try { |
| 19 | + logger.debug(`Installing ${packageName}...`); |
| 20 | + execSync(`sudo apt update && sudo apt install -y ${packageName}`, { stdio: debug ? "inherit" : "pipe" }); |
| 21 | + logger.debug(`${packageName} installed successfully.`); |
| 22 | + } |
| 23 | + catch (error) { |
| 24 | + logger.fail(`Failed to install ${packageName}.`, error); |
| 25 | + throw error; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function checkAndInstallDependencies(packages: string[]): void { |
| 30 | + packages.forEach((pkg) => { |
| 31 | + if (isPackageInstalled(pkg)) { |
| 32 | + logger.debug(`${pkg} is already installed.`); |
30 | 33 | } |
31 | | - catch (error) { |
32 | | - logger.error("Failed to install Xvfb:", error); |
33 | | - process.exit(1); |
| 34 | + else { |
| 35 | + installPackage(pkg); |
34 | 36 | } |
| 37 | + }); |
| 38 | +} |
| 39 | + |
| 40 | +export async function installXvfb() { |
| 41 | + if (!isLinux) { |
| 42 | + logger.error("Unsupported platform. Please install Xvfb manually."); |
| 43 | + return; |
35 | 44 | } |
| 45 | + |
| 46 | + const osId = execSync("cat /etc/os-release | grep '^ID='").toString(); |
| 47 | + if (!(osId.includes("ubuntu") || osId.includes("debian"))) { |
| 48 | + logger.error("Unsupported Linux distribution."); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + checkAndInstallDependencies(["xvfb", "x11-xkb-utils", "xkb-data"]); |
| 53 | +} |
| 54 | + |
| 55 | +export async function installDepsForUbuntu24() { |
| 56 | + checkAndInstallDependencies(["libasound2t64", "libdbus-glib-1-2"]); |
36 | 57 | } |
37 | 58 |
|
38 | 59 | export async function installZoteroLinux() { |
39 | | - logger.debug("Installing Zotero..."); |
40 | | - try { |
41 | | - execSync("wget -O zotero.tar.bz2 'https://www.zotero.org/download/client/dl?platform=linux-x86_64&channel=beta'", { stdio: "pipe" }); |
42 | | - execSync("tar -xvf zotero.tar.bz2", { stdio: "pipe" }); |
43 | | - } |
44 | | - catch (e) { |
45 | | - logger.error(e); |
46 | | - throw new Error("Zotero extracted failed"); |
| 60 | + if (process.env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH) { |
| 61 | + logger.debug("Local Zotero found, skip to download."); |
| 62 | + return; |
47 | 63 | } |
| 64 | + |
| 65 | + logger.debug("Installing Zotero..."); |
| 66 | + execSync("wget -O zotero.tar.bz2 'https://www.zotero.org/download/client/dl?platform=linux-x86_64&channel=beta'", { stdio: "pipe" }); |
| 67 | + execSync("tar -xvf zotero.tar.bz2", { stdio: "pipe" }); |
| 68 | + |
| 69 | + // Set Environment Variable for Zotero Bin Path |
| 70 | + process.env.ZOTERO_PLUGIN_ZOTERO_BIN_PATH = `${process.cwd()}/Zotero_linux-x86_64/zotero`; |
48 | 71 | } |
0 commit comments