Feat: 新增Linux下依赖库自动安装

This commit is contained in:
Suwings 2022-07-16 14:00:26 +08:00
parent c16b9b4cc7
commit e06d1b2db4
2 changed files with 90 additions and 0 deletions

View File

@ -53,6 +53,10 @@ import * as router from "./service/router";
import * as koa from "./service/http";
import * as protocol from "./service/protocol";
import InstanceSubsystem from "./service/system_instance";
import { initDependent } from "./service/install";
// 异步初始化可选依赖库
initDependent();
// 初始化全局配置服务
globalConfiguration.load();

86
src/service/install.ts Normal file
View File

@ -0,0 +1,86 @@
/*
Copyright (C) 2022 Suwings <Suwings@outlook.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
According to the AGPL, it is forbidden to delete all copyright notices,
and if you modify the source code, you must open source the
modified source code.
(C) 2022 Suwings <Suwings@outlook.com>
/ GNU Affero
3
AGPL
https://mcsmanager.com/ 阅读用户协议,申请闭源开发授权等。
*/
import os from "os";
import fs from "fs-extra";
import https from "https";
import path from "path";
import logger from "./log";
import { Logger } from "log4js";
const PTY_PATH = path.normalize(path.join(process.cwd(), "lib", "pty"));
const PTY_DIR_PATH = path.join(process.cwd(), "lib");
function installPtyForLinux(url: string) {
return new Promise<number>((resolve, reject) => {
// if (os.platform() !== "linux") resolve(-1);
if (!fs.existsSync(PTY_DIR_PATH)) fs.mkdirsSync(PTY_DIR_PATH);
if (fs.existsSync(PTY_PATH) && fs.statSync(PTY_PATH)?.size > 1024) {
logger.info("识别到可选依赖库安装,仿真终端功能已可用");
return resolve(1);
}
const file = fs.createWriteStream(PTY_PATH);
https.get(url, (res) => {
if (res.statusCode !== 200) {
return reject(new Error("code!=200"));
}
res.on("error", (err) => {
if (fs.existsSync(PTY_PATH)) fs.removeSync(PTY_PATH);
reject(err);
});
file
.on("finish", () => {
file.close();
resolve(0);
})
.on("error", (err) => {
reject(err);
});
res.pipe(file);
});
});
}
// 自动安装依赖库
// 依赖程序基于 Go 语言开发开源地址https://github.com/MCSManager/pty
export function initDependent() {
const ptyUrls = ["https://mcsmanager.com/download/pty_linux", "https://mcsmanager.oss-cn-guangzhou.aliyuncs.com/pty_linux"];
function setup(index = 0) {
installPtyForLinux(ptyUrls[index])
.then(() => {
logger.info("可选依赖程序已自动安装,仿真终端和部分高级功能已自动启用");
logger.info("依赖程序参考https://github.com/mcsmanager/pty");
})
.catch((err) => {
fs.remove(PTY_PATH, () => {});
if (index === ptyUrls.length - 1) {
logger.warn(`安装可选依赖库失败,全仿真终端和部分可选功能将无法使用,不影响正常功能,将在下次启动时再尝试安装`);
logger.warn(err);
return;
}
return setup(index + 1);
});
}
setup(0);
}