Miao-Yunzai/plugins/other/install.js

128 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-06-29 10:41:02 +08:00
import { exec, execSync } from "child_process"
import plugin from "../../lib/plugins/plugin.js"
import fs from "node:fs"
import { Restart } from "./restart.js"
2023-05-11 16:03:18 +08:00
let insing = false
const list = {
2023-06-29 10:41:02 +08:00
"Atlas" :"https://gitee.com/Nwflower/atlas",
"TRSS-Plugin" :"https://Yunzai.TRSS.me",
"yenai-plugin" :"https://gitee.com/yeyang52/yenai-plugin",
"expand-plugin" :"https://gitee.com/SmallK111407/expand-plugin",
"flower-plugin" :"https://gitee.com/Nwflower/flower-plugin",
"earth-k-plugin" :"https://gitee.com/SmallK111407/earth-k-plugin",
"xiaofei-plugin" :"https://gitee.com/xfdown/xiaofei-plugin",
"xiaoyao-cvs-plugin":"https://gitee.com/Ctrlcvs/xiaoyao-cvs-plugin",
2023-07-23 19:04:24 +08:00
"mysVilla-Plugin" :"https://gitee.com/TimeRainStarSky/Yunzai-mysVilla-Plugin",
2023-06-29 10:41:02 +08:00
"Telegram-Plugin" :"https://gitee.com/TimeRainStarSky/Yunzai-Telegram-Plugin",
"Discord-Plugin" :"https://gitee.com/TimeRainStarSky/Yunzai-Discord-Plugin",
"QQGuild-Plugin" :"https://gitee.com/TimeRainStarSky/Yunzai-QQGuild-Plugin",
"WeChat-Plugin" :"https://gitee.com/TimeRainStarSky/Yunzai-WeChat-Plugin",
2023-07-23 19:04:24 +08:00
"Proxy-Plugin" :"https://gitee.com/TimeRainStarSky/Yunzai-Proxy-Plugin",
2023-06-29 10:41:02 +08:00
"ICQQ-Plugin" :"https://gitee.com/TimeRainStarSky/Yunzai-ICQQ-Plugin",
"KOOK-Plugin" :"https://gitee.com/TimeRainStarSky/Yunzai-KOOK-Plugin",
2023-05-11 16:03:18 +08:00
}
export class install extends plugin {
2023-07-23 19:04:24 +08:00
constructor() {
2023-05-11 16:03:18 +08:00
super({
2023-06-29 10:41:02 +08:00
name: "安装插件",
dsc: "#安装插件 #安装TRSS-Plugin",
event: "message",
2023-05-11 16:03:18 +08:00
rule: [
{
2023-06-29 10:41:02 +08:00
reg: `^#安装(插件|${Object.keys(list).join("|")})$`,
fnc: "install",
permission: "master"
2023-05-11 16:03:18 +08:00
}
]
})
}
2023-07-23 19:04:24 +08:00
async install() {
2023-05-11 16:03:18 +08:00
if (insing) {
2023-06-29 10:41:02 +08:00
await this.reply("已有命令安装中..请勿重复操作")
2023-05-11 16:03:18 +08:00
return false
}
2023-06-29 10:41:02 +08:00
const name = this.e.msg.replace(/^#安装/, "").trim()
if (name == "插件") {
let msg = "\n"
2023-05-11 16:03:18 +08:00
for (const name in list)
if (!fs.existsSync(`plugins/${name}`))
msg += `${name}\n`
2023-06-29 10:41:02 +08:00
if (msg == "\n")
msg = "暂无可安装插件"
2023-05-11 16:03:18 +08:00
else
msg = `可安装插件列表:${msg}发送 #安装+插件名 进行安装`
await this.reply(msg)
return true
}
const path = `plugins/${name}`
if (fs.existsSync(path)) {
await this.reply(`${name} 插件已安装`)
return false
}
await this.runInstall(name, list[name], path)
this.restart()
}
2023-07-23 19:04:24 +08:00
async execSync(cmd) {
2023-05-11 16:03:18 +08:00
return new Promise(resolve => {
exec(cmd, (error, stdout, stderr) => {
resolve({ error, stdout, stderr })
})
})
}
2023-07-23 19:04:24 +08:00
async runInstall(name, url, path) {
2023-05-11 16:03:18 +08:00
this.isNowUp = false
2023-06-29 10:41:02 +08:00
let cm = `git clone --depth 1 --single-branch "${url}" "${path}"`
2023-05-11 16:03:18 +08:00
logger.mark(`${this.e.logFnc} 开始安装:${name} 插件`)
await this.reply(`开始安装:${name} 插件`)
insing = true
let ret = await this.execSync(cm)
if (fs.existsSync(`${path}/package.json`))
2023-06-29 10:41:02 +08:00
await this.execSync("pnpm install")
2023-05-11 16:03:18 +08:00
insing = false
if (ret.error) {
logger.mark(`${this.e.logFnc} 插件安装失败:${name}`)
this.gitErr(ret.error, ret.stdout)
return false
}
return true
}
2023-07-23 19:04:24 +08:00
async gitErr(err, stdout) {
2023-06-29 10:41:02 +08:00
let msg = "安装失败!"
2023-05-11 16:03:18 +08:00
let errMsg = err.toString()
stdout = stdout.toString()
2023-06-29 10:41:02 +08:00
if (errMsg.includes("Timed out")) {
let remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, "")
2023-05-11 16:03:18 +08:00
await this.reply(msg + `\n连接超时:${remote}`)
return
}
if (/Failed to connect|unable to access/g.test(errMsg)) {
2023-06-29 10:41:02 +08:00
let remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, "")
2023-05-11 16:03:18 +08:00
await this.reply(msg + `\n连接失败:${remote}`)
return
}
await this.reply([errMsg, stdout])
}
2023-07-23 19:04:24 +08:00
restart() {
2023-05-11 16:03:18 +08:00
new Restart(this.e).restart()
}
}