rc-plugin/apps/update.js
2023-02-15 14:21:47 +08:00

129 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 主库
import Version from "../model/version.js";
import config from "../model/index.js";
import puppeteer from "../../../lib/puppeteer/puppeteer.js";
import lodash from 'lodash'
import {exec, execSync} from "node:child_process";
/**
* 处理插件更新
*/
export class update extends plugin {
constructor() {
super({
name: "更新插件",
dsc: "更新插件代码",
event: "message",
priority: 4000,
rule: [
{
reg: "^#*R(插件)?版本$",
fnc: "version",
},
{
/** 命令正则匹配 */
reg: "^#*R(插件)?更新$",
/** 执行方法 */
fnc: "rconsoleUpdate",
},
],
});
this.versionData = config.getConfig("version");
}
/**
* rule - 插件版本信息
*/
async version() {
const data = await new Version(this.e).getData(this.versionData.slice(0, 3));
let img = await puppeteer.screenshot("version", data);
this.e.reply(img);
}
/**
* 更新主程序
* @param e
* @returns {Promise<boolean>}
*/
async rconsoleUpdate(e) {
if (!this.e.isMaster) {
await this.e.reply("您无权操作");
return true;
}
const pluginName = 'rconsole-plugin'
let command = `git -C ./plugins/${pluginName}/ pull --no-rebase`;
this.oldCommitId = await this.getcommitId(pluginName)
await e.reply("正在执行更新操作,请稍等");
let ret = await this.execSync(command)
if (ret.error) {
e.reply(`更新失败!重试一下!`)
await this.gitErr(ret.error, ret.stdout)
return false
}
const time = await this.getTime(pluginName)
if (/Already up|已经是最新/g.test(ret.stdout)) {
e.reply(`R插件已经是最新: ${this.versionData[0].version}`)
} else {
this.isUp = true
e.reply(`R插件更新成功最后更新时间${time}`)
e.reply(await this.getLog(pluginName))
}
return true
}
async getcommitId(pluginName) {
// let cm = 'git rev-parse --short HEAD'
const command = `git -C ./plugins/${pluginName}/ rev-parse --short HEAD`
let commitId = execSync(command, {encoding: 'utf-8'})
commitId = lodash.trim(commitId)
return commitId
}
async execSync(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, {windowsHide: true}, (error, stdout, stderr) => {
resolve({error, stdout, stderr})
})
})
}
async getTime(pluginName) {
const cm = `cd ./plugins/${pluginName}/ && git log -1 --oneline --pretty=format:"%cd" --date=format:"%m-%d %H:%M"`
let time = ''
try {
time = execSync(cm, {encoding: 'utf-8'})
time = lodash.trim(time)
} catch (error) {
time = '获取时间失败'
}
return time
}
async getLog (pluginName) {
let cm = 'git log -20 --oneline --pretty=format:"%h||[%cd] %s" --date=format:"%m-%d %H:%M"'
if (pluginName) { cm = `cd ./plugins/${pluginName}/ && ${cm}` }
let logAll
try { logAll = execSync(cm, { encoding: 'utf-8' }) } catch (error) { this.reply(error.toString(), true) }
if (!logAll) return false
logAll = logAll.split('\n')
let log = []
for (let str of logAll) {
str = str.split('||')
if (str[0] === this.oldCommitId) break
if (str[1].includes('Merge branch')) continue
log.push(str[1])
}
let line = log.length
log = log.join('\n')
if (log.length <= 0) return ''
logger.info(`${pluginName || 'Yunzai-Bot'}更新日志,共${line}\n${log}`)
return log
}
}