Files
2025-08-04 10:46:00 +08:00

76 lines
2.9 KiB
TypeScript

import fs from 'fs';
import path from 'path';
import Robot from './Robot';
import { readGitLog, readGitVersion, readSvnVersion } from './Utils';
const BUILD_MESSAGE_CACHE_FILE: string = '.build_message_cache.log';
type Cache = { git: string, svn: string };
const sendStartMessage = (key: string, projectName: string, projectDir: string): void => {
// 开始构建时记录当前工程目录Git提交版本
const cacheFilePath = path.join(projectDir, BUILD_MESSAGE_CACHE_FILE);
//如果版本记录文件存在,证明上一次构建失败或停止。不再写入新版本数据
if (!fs.existsSync(cacheFilePath)) {
const gitVersion = readGitVersion(projectDir);
const svnVersion = '0';//readSvnVersion(tableDir);
console.log(`记录当前版本: git = ${gitVersion}, svn = ${svnVersion}`);
const cache: Cache = { git: gitVersion, svn: svnVersion };
fs.writeFileSync(cacheFilePath, JSON.stringify(cache));
} else {
console.log(`存在版本记录: ${fs.readFileSync(cacheFilePath).toString()}`);
}
Robot.sendMessage(key, 'text', `${projectName}开始构建`);
}
const sendFinishedMessage = async (key: string, projectName: string, projectDir: string): Promise<void> => {
// 读取工程构建前记录的提交版本
const cacheFilePath = path.join(projectDir, BUILD_MESSAGE_CACHE_FILE);
if (!fs.existsSync(cacheFilePath)) {
console.log(`版本记录不存在`);
Robot.sendMessage(key, 'text', `${projectName}构建完成`);
return;
}
const cacheString = fs.readFileSync(cacheFilePath).toString();
fs.unlinkSync(cacheFilePath);
const cache: Cache = JSON.parse(cacheString);
const logs = await readGitLog(projectDir, cache.git);
// const logs = readGitLog(projectDir, 'e858739336f65b33d1fe36c7b78e3738567e8ee3');
if (!logs || !logs.length) {
console.log(`此次构建无差异`);
Robot.sendMessage(key, 'text', `${projectName}构建完成`);
return;
}
let logString: string = '';
logs.forEach(({ name, title }, index: number) => {
if (index !== 0) logString += '\n';
const fixTitle = title.replace(new RegExp('\\#', 'gm'), '\\#').replace(new RegExp('\\*', 'gm'), '\\*');;
logString += `>**${name}** - ${fixTitle}`
});
//获取构建前提交版本之后的git log
console.log(`此次构建差异:\n${logs.map(value => (`${value.name}-${value.title}`)).join('\n')}`);
let message = `${projectName}构建完成\n${logString}`
Robot.sendMessage(key, 'markdown', message);
}
let args = process.argv;
let type = args[2];
let projectName = args[3];
let projectDir = args[4];
let key = args[5];
//TODO windows下处理脚本与工程不在同一盘符
switch (type) {
case 'start':
sendStartMessage(key, projectName, projectDir);
break;
case 'finish':
sendFinishedMessage(key, projectName, projectDir);
break;
}