UNPKG

multi-automator

Version:
136 lines (135 loc) 4.76 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkChromium = exports.isWin11 = exports.getSystemPlatform = exports.SystemPlatform = void 0; /** * @desc: Browser Operate * @author: john_chen * @date: 2023.04.03 */ const os_1 = __importDefault(require("os")); const axios_1 = __importDefault(require("axios")); const compressing_1 = require("compressing"); const fs_1 = require("fs"); const config_1 = require("../config"); /** * 浏览器配置集合 */ const browserConfigMap = { mac_arm: { type: 'zip', path: `${config_1.dir.bin}/chrome-mac/Chromium.app/Contents/MacOS/Chromium`, downloadUrl: 'https://storage.googleapis.com/chromium-browser-snapshots/Mac_Arm/1108766/chrome-mac.zip', }, win64: { type: 'zip', path: `${config_1.dir.bin}/chrome-win/chrome.exe`, downloadUrl: 'https://storage.googleapis.com/chromium-browser-snapshots/Win_x64/1108766/chrome-win.zip', } }; /** * 系统平台类型 */ var SystemPlatform; (function (SystemPlatform) { SystemPlatform["LINUX"] = "linux"; SystemPlatform["MAC"] = "mac"; SystemPlatform["MAC_ARM"] = "mac_arm"; SystemPlatform["WIN32"] = "win32"; SystemPlatform["WIN64"] = "win64"; })(SystemPlatform = exports.SystemPlatform || (exports.SystemPlatform = {})); /** * 获取系统平台类型 * * @returns {SystemPlatform | undefined} */ function getSystemPlatform() { const platform = os_1.default.platform(); switch (platform) { case 'darwin': return os_1.default.arch() === 'arm64' ? SystemPlatform.MAC_ARM : SystemPlatform.MAC; case 'linux': return SystemPlatform.LINUX; case 'win32': return os_1.default.arch() === 'x64' || (os_1.default.arch() === 'arm64' && isWin11(os_1.default.release())) ? SystemPlatform.WIN64 : SystemPlatform.WIN32; default: return undefined; } } exports.getSystemPlatform = getSystemPlatform; /** * 判断是否是 windows11 系统 * * @param {string} version 系统版本号 * @returns {boolean} */ function isWin11(version) { const parts = version.split('.'); if (parts.length > 2) { const major = parseInt(parts[0], 10); const minor = parseInt(parts[1], 10); const patch = parseInt(parts[2], 10); return (major > 10 || (major === 10 && minor > 0) || (major === 10 && minor === 0 && patch >= 22000)); } return false; } exports.isWin11 = isWin11; /** * 检测 Chromium 安装 * * @returns {Promise<string>} */ async function checkChromium() { config_1.logger.info('[browser.checkChromium]'); // 获取系统平台 let platform = getSystemPlatform(); platform = platform || SystemPlatform.MAC_ARM; config_1.logger.info(`[browser.checkChromium] system platform: ${platform}`); // @ts-ignore let browserConfig = browserConfigMap[platform]; if (undefined === browserConfig) { throw new Error(`暂不支持该 ${platform} 系统自动安装浏览器`); } let { type, downloadUrl, path } = browserConfig; if (!(0, fs_1.existsSync)(path)) { config_1.logger.warn('[browser.checkChromium] 未识别到指定浏览器,开始安装 Chromium'); // 拉取 Chromium 压缩包 let chromiumTarName = type === 'zip' ? 'Chromium.zip' : 'Chromium.tar.gz'; let chromiumTarPath = `${config_1.dir.bin}/${chromiumTarName}`; await (0, axios_1.default)({ method: 'get', url: downloadUrl, responseType: 'arraybuffer', }) .then((res) => { (0, fs_1.writeFileSync)(chromiumTarPath, res.data); }) .catch((err) => { throw new Error(`获取 Chromium.tar.gz 失败:${err.message}`); }); config_1.logger.info('[browser.checkChromium] Chromium 下载完成'); // 解压 if ('zip' === type) { await compressing_1.zip.uncompress(chromiumTarPath, config_1.dir.bin).catch((err) => { throw new Error(`解压 ${chromiumTarName} 失败:${err.message}`); }); } else { await compressing_1.tgz.uncompress(chromiumTarPath, config_1.dir.bin).catch((err) => { throw new Error(`解压 ${chromiumTarName} 失败:${err.message}`); }); } config_1.logger.info('[browser.checkChromium] Chromium 安装完成'); } return path; } exports.checkChromium = checkChromium;