mirror of
https://github.com/ZZZure/ZZZ-Plugin.git
synced 2025-12-17 05:37:46 +00:00
update.js改为调用云崽自带update功能
This commit is contained in:
parent
b4958b9146
commit
d3e95d7ce2
1 changed files with 29 additions and 242 deletions
271
apps/update.js
271
apps/update.js
|
|
@ -1,27 +1,17 @@
|
||||||
import common from '../../../lib/common/common.js';
|
import _ from 'lodash'
|
||||||
import _ from 'lodash';
|
import { rulePrefix } from '../lib/common.js'
|
||||||
import { exec } from 'child_process';
|
import { pluginName } from '../lib/path.js'
|
||||||
import { ZZZPlugin } from '../lib/plugin.js';
|
import settings from '../lib/settings.js'
|
||||||
import { rulePrefix } from '../lib/common.js';
|
|
||||||
import { pluginPath, pluginName } from '../lib/path.js';
|
|
||||||
import settings from '../lib/settings.js';
|
|
||||||
|
|
||||||
let Restart = null;
|
|
||||||
|
|
||||||
|
let Update = null
|
||||||
try {
|
try {
|
||||||
Restart = (await import('../../other/restart.js').catch(e => null))?.Restart;
|
Update = (await import("../../other/update.js").catch(e => null))?.update
|
||||||
Restart ||= (await import('../../system/apps/restart.ts')).Restart;
|
Update ||= (await import("../../system/apps/update.ts")).update
|
||||||
} catch {
|
} catch (e) {
|
||||||
logger.error(`[${pluginName}]未获取到重启js,重启将无法使用`);
|
logger.error(`[${pluginName}]未获取到更新js ${logger.yellow("更新功能")} 将无法使用`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 是否在更新中
|
export class update extends plugin {
|
||||||
let uping = false;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理插件更新
|
|
||||||
*/
|
|
||||||
export class Update extends ZZZPlugin {
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super({
|
super({
|
||||||
name: '[ZZZ-Plugin]Update',
|
name: '[ZZZ-Plugin]Update',
|
||||||
|
|
@ -30,236 +20,33 @@ export class Update extends ZZZPlugin {
|
||||||
priority: _.get(settings.getConfig('priority'), 'update', 70),
|
priority: _.get(settings.getConfig('priority'), 'update', 70),
|
||||||
rule: [
|
rule: [
|
||||||
{
|
{
|
||||||
reg: `^${rulePrefix}(插件)?(强制)?更新$`,
|
reg: `^${rulePrefix}(插件)?(强制)?更新(插件)?$`,
|
||||||
fnc: 'update',
|
fnc: 'update',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
reg: `^${rulePrefix}(插件)?更新日志$`,
|
||||||
|
fnc: 'update_log',
|
||||||
|
}
|
||||||
],
|
],
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
async update(e = this.e) {
|
||||||
* rule - 更新ZZZ插件
|
if (!e.isMaster) return
|
||||||
* @returns
|
e.msg = `#${e.msg.includes("强制") ? "强制" : ""}更新${pluginName}`
|
||||||
*/
|
const up = new Update(e)
|
||||||
async update() {
|
up.e = e
|
||||||
if (!this.e.isMaster) return false;
|
return up.update()
|
||||||
|
|
||||||
/** 检查是否正在更新中 */
|
|
||||||
if (uping) {
|
|
||||||
await this.reply(`[${pluginName}]已有命令更新中..请勿重复操作`);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 检查git安装 */
|
async update_log() {
|
||||||
if (!(await this.checkGit())) return;
|
let Update_Plugin = new Update()
|
||||||
|
Update_Plugin.e = this.e
|
||||||
|
Update_Plugin.reply = this.reply
|
||||||
|
|
||||||
const isForce = this.e.msg.includes('强制');
|
if (Update_Plugin.getPlugin(pluginName)) {
|
||||||
|
this.e.reply(await Update_Plugin.getLog(pluginName))
|
||||||
/** 执行更新 */
|
|
||||||
await this.runUpdate(isForce);
|
|
||||||
|
|
||||||
/** 是否需要重启 */
|
|
||||||
if (this.isUp) {
|
|
||||||
// await this.reply("更新完毕,请重启云崽后生效")
|
|
||||||
setTimeout(() => this.restart(), 2000);
|
|
||||||
}
|
}
|
||||||
}
|
return true
|
||||||
|
|
||||||
restart() {
|
|
||||||
new Restart(this.e).restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ZZZ-Plugin更新函数
|
|
||||||
* @param {boolean} isForce 是否为强制更新
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async runUpdate(isForce) {
|
|
||||||
let command = `git -C ${pluginPath} pull --no-rebase`;
|
|
||||||
if (isForce) {
|
|
||||||
command = `git -C ${pluginPath} fetch --all && git -C ${pluginPath} reset --hard HEAD`;
|
|
||||||
this.e.reply(`[${pluginName}]正在执行强制更新操作,请稍等`);
|
|
||||||
} else {
|
|
||||||
this.e.reply(`[${pluginName}]正在执行更新操作,请稍等`);
|
|
||||||
}
|
|
||||||
/** 获取上次提交的commitId,用于获取日志时判断新增的更新日志 */
|
|
||||||
this.oldCommitId = await this.getcommitId(pluginName);
|
|
||||||
uping = true;
|
|
||||||
let ret = await this.execSync(command);
|
|
||||||
uping = false;
|
|
||||||
|
|
||||||
if (ret.error) {
|
|
||||||
logger.mark(`[${pluginName}] ${this.e.logFnc} 更新失败:${pluginName}`);
|
|
||||||
this.gitErr(ret.error, ret.stdout);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 获取插件提交的最新时间 */
|
|
||||||
let time = await this.getTime(pluginName);
|
|
||||||
|
|
||||||
if (/(Already up[ -]to[ -]date|已经是最新的)/.test(ret.stdout)) {
|
|
||||||
await this.reply(`${pluginName}已经是最新版本\n最后更新时间:${time}`);
|
|
||||||
} else {
|
|
||||||
await this.reply(`${pluginName}\n最后更新时间:${time}`);
|
|
||||||
this.isUp = true;
|
|
||||||
/** 获取ZZZ-Plugin的更新日志 */
|
|
||||||
let log = await this.getLog(pluginName);
|
|
||||||
await this.reply(log);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.mark(`[${pluginName}] ${this.e.logFnc} 最后更新时间:${time}`);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取绝区零插件的更新日志
|
|
||||||
* @param {string} plugin 插件名称
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async getLog(plugin = '') {
|
|
||||||
let cm = `cd ./plugins/${plugin}/ && git log -20 --oneline --pretty=format:"%h||[%cd] %s" --date=format:"%m-%d %H:%M"`;
|
|
||||||
|
|
||||||
let logAll;
|
|
||||||
try {
|
|
||||||
logAll = await this.execSync(cm, { encoding: 'utf-8' });
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(error.toString());
|
|
||||||
this.reply(error.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!logAll.stdout) return false;
|
|
||||||
|
|
||||||
logAll = logAll.stdout.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\n');
|
|
||||||
|
|
||||||
if (log.length <= 0) return '';
|
|
||||||
|
|
||||||
const end =
|
|
||||||
'更多详细信息,请前往gitee查看\nhttps://gitHub.com/ZZZure/ZZZ-Plugin';
|
|
||||||
|
|
||||||
log = await common.makeForwardMsg(
|
|
||||||
this.e,
|
|
||||||
[log, end],
|
|
||||||
`${pluginName}更新日志,共${line}条`
|
|
||||||
);
|
|
||||||
|
|
||||||
return log;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取上次提交的commitId
|
|
||||||
* @param {string} plugin 插件名称
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async getcommitId(plugin = '') {
|
|
||||||
let cm = `git -C ./plugins/${plugin}/ rev-parse --short HEAD`;
|
|
||||||
|
|
||||||
let commitId = await this.execSync(cm, { encoding: 'utf-8' });
|
|
||||||
|
|
||||||
if (!commitId.stdout) return '';
|
|
||||||
|
|
||||||
commitId = _.trim(commitId.stdout);
|
|
||||||
|
|
||||||
return commitId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取本次更新插件的最后一次提交时间
|
|
||||||
* @param {string} plugin 插件名称
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async getTime(plugin = '') {
|
|
||||||
let cm = `cd ./plugins/${plugin}/ && git log -1 --oneline --pretty=format:"%cd" --date=format:"%m-%d %H:%M"`;
|
|
||||||
|
|
||||||
let time = '';
|
|
||||||
try {
|
|
||||||
time = await this.execSync(cm, { encoding: 'utf-8' });
|
|
||||||
time = _.trim(time.stdout);
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(error.toString());
|
|
||||||
time = '获取时间失败';
|
|
||||||
}
|
|
||||||
return time;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理更新失败的相关函数
|
|
||||||
* @param {string} err
|
|
||||||
* @param {string} stdout
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async gitErr(err, stdout) {
|
|
||||||
let msg = `[${pluginName}]更新失败!`;
|
|
||||||
let errMsg = err.toString();
|
|
||||||
stdout = stdout.toString();
|
|
||||||
|
|
||||||
if (errMsg.includes('Timed out')) {
|
|
||||||
let remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, '');
|
|
||||||
await this.reply(msg + `\n连接超时:${remote}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (/Failed to connect|unable to access/g.test(errMsg)) {
|
|
||||||
let remote = errMsg.match(/'(.+?)'/g)[0].replace(/'/g, '');
|
|
||||||
await this.reply(msg + `\n连接失败:${remote}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (errMsg.includes('be overwritten by merge')) {
|
|
||||||
await this.reply(
|
|
||||||
msg +
|
|
||||||
`存在冲突:\n${errMsg}\n` +
|
|
||||||
'请解决冲突后再更新,或者执行%强制更新,放弃本地修改'
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stdout.includes('CONFLICT')) {
|
|
||||||
await this.reply([
|
|
||||||
msg + '存在冲突\n',
|
|
||||||
errMsg,
|
|
||||||
stdout,
|
|
||||||
'\n请解决冲突后再更新,或者执行%强制更新,放弃本地修改',
|
|
||||||
]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.reply([errMsg, stdout]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异步执行git相关命令
|
|
||||||
* @param {string} cmd git命令
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async execSync(cmd) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
exec(cmd, { windowsHide: true }, (error, stdout, stderr) => {
|
|
||||||
resolve({ error, stdout, stderr });
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查git是否安装
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async checkGit() {
|
|
||||||
let ret = await this.execSync('git --version', { encoding: 'utf-8' });
|
|
||||||
if (!ret.stdout || !ret.stdout.includes('git version')) {
|
|
||||||
await this.reply('请先安装git');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue