feat: 版本机制; fix: fix: 绑定设备提示

This commit is contained in:
bietiaop 2024-08-20 13:29:19 +08:00
parent d34c1d8be1
commit 8425503797
16 changed files with 820 additions and 59 deletions

105
lib/update.js Normal file
View file

@ -0,0 +1,105 @@
import _ from 'lodash';
import { pluginName } from '../lib/path.js';
import { mdLogLineToHTML } from '../utils/data.js';
let Update = null;
try {
Update = (await import('../../other/update.js').catch(e => null))?.update;
Update ||= (await import('../../system/apps/update.ts')).update;
} catch (e) {
logger.error(
`[${pluginName}]未获取到更新js ${logger.yellow('更新功能')} 将无法使用`
);
}
let ZZZUpdate = null;
/**
* @typedef {Object} CommitLog
* @property {string} commit 提交ID
* @property {string} date 提交时间
* @property {string} msg 提交信息
* @property {boolean} local 是否本地记录
* @property {current} boolean 是否当前版本
*/
/**
* @typedef {Object} UpdateInfo
* @property {boolean} hasUpdate 是否有更新
* @property {CommitLog[]} logs 更新日志
*/
if (Update) {
ZZZUpdate = class ZZZUpdate extends Update {
async handleLog(remote = false) {
let cmdStr = 'git log -100 --pretty="%h||%cd||%s" --date=format:"%F %T"';
if (remote) {
cmdStr =
'git log -100 --pretty="%h||%cd||%s" --date=format:"%F %T" origin/main';
}
const cm = await this.exec(cmdStr, pluginName);
if (cm.error) {
throw new Error(cm.error.message);
}
const logAll = cm.stdout.split('\n');
if (!logAll.length) {
throw new Error('未获取到更新日志');
}
/** @type CommitLog[] */
const log = [];
let current = true;
for (let str of logAll) {
str = str.split('||');
if (str[0] === this.oldCommitId) break;
if (str[2].includes('Merge')) continue;
/** @type CommitLog */
const commit = {
commit: str[0],
date: str[1],
msg: mdLogLineToHTML(str[2]),
local: !remote,
current: false,
};
if (!remote && current) {
commit.current = true;
current = false;
}
log.push(commit);
}
return log;
}
async getZZZLog() {
const log = await this.handleLog();
return log;
}
async getZZZRemoteLog() {
const log = await this.handleLog(true);
return log;
}
async getZZZAllLog() {
const localLog = await this.getZZZLog();
const remoteLog = await this.getZZZRemoteLog();
const logs = _.unionBy(localLog, remoteLog, 'commit');
logs.sort((a, b) => {
return new Date(b.date) - new Date(a.date);
});
return logs;
}
async hasUpdate() {
const logs = await this.getZZZAllLog();
const newLogs = logs.filter(log => !log.local);
/** @type UpdateInfo */
let result = {
hasUpdate: false,
logs: [],
};
if (newLogs.length) {
result.hasUpdate = true;
result.logs = newLogs;
}
return result;
}
};
}
export { ZZZUpdate };

View file

@ -2,6 +2,7 @@ import fs from 'fs';
import lodash from 'lodash';
import path from 'path';
import { pluginPath } from './path.js';
import { mdLogLineToHTML } from '../utils/data.js';
// 更新日志文件位置
const _logPath = path.join(pluginPath, 'CHANGELOG.md');
@ -21,26 +22,6 @@ let versionCount = 4;
// 读取 package.json此处为读取Yunzai-Bot的package.json
let packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
/**
* Markdown 行转换为 HTML
* @param {*} line
* @returns
*/
const getLine = function (line) {
// 去除行首空格和换行符
line = line.replace(/(^\s*\*|\r)/g, '');
// 替换行内代码块
line = line.replace(/\s*`([^`]+`)/g, '<span class="cmd">$1');
line = line.replace(/`\s*/g, '</span>');
// 替换行内加粗
line = line.replace(/\s*\*\*([^\*]+\*\*)/g, '<span class="strong">$1');
line = line.replace(/\*\*\s*/g, '</span>');
// 替换行内表示更新内容
line = line.replace(/ⁿᵉʷ/g, '<span class="new"></span>');
// 返回转换后的行内容HTML
return line;
};
// 尝试读取更新日志文件
try {
if (fs.existsSync(_logPath)) {
@ -54,9 +35,9 @@ try {
let lastLine = {};
lodash.forEach(logs, line => {
// 如果版本数量小于0返回false
if (versionCount <= -1) {
return false;
}
// if (versionCount <= -1) {
// return false;
// }
// 匹配版本号
const versionRet = /^#\s*([0-9a-zA-Z\\.~\s]+?)\s*$/.exec(line);
if (versionRet && versionRet[1]) {
@ -86,12 +67,12 @@ try {
// 如果行以 * 开头,表示更新内容
if (/^\*/.test(line)) {
lastLine = {
title: getLine(line),
title: mdLogLineToHTML(line),
logs: [],
};
temp.logs.push(lastLine);
} else if (/^\s{2,}\*/.test(line)) {
lastLine.logs.push(getLine(line));
lastLine.logs.push(mdLogLineToHTML(line));
}
}
});