"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const Robot_1 = __importDefault(require("./Robot")); const Utils_1 = require("../Utils"); const BUILD_MESSAGE_CACHE_FILE = '.build_message_cache.log'; const sendStartMessage = (key, projectName, projectDir) => { // 开始构建时记录当前工程目录Git提交版本 const cacheFilePath = path_1.default.join(projectDir, BUILD_MESSAGE_CACHE_FILE); //如果版本记录文件存在,证明上一次构建失败或停止。不再写入新版本数据 if (!fs_1.default.existsSync(cacheFilePath)) { const gitVersion = Utils_1.readGitVersion(projectDir); const svnVersion = '0'; //readSvnVersion(tableDir); console.log(`记录当前版本: git = ${gitVersion}, svn = ${svnVersion}`); const cache = { git: gitVersion, svn: svnVersion }; fs_1.default.writeFileSync(cacheFilePath, JSON.stringify(cache)); } else { console.log(`存在版本记录: ${fs_1.default.readFileSync(cacheFilePath).toString()}`); } Robot_1.default.sendMessage(key, 'text', `${projectName}开始构建`); }; const sendFinishedMessage = (key, projectName, projectDir) => __awaiter(void 0, void 0, void 0, function* () { // 读取工程构建前记录的提交版本 const cacheFilePath = path_1.default.join(projectDir, BUILD_MESSAGE_CACHE_FILE); if (!fs_1.default.existsSync(cacheFilePath)) { console.log(`版本记录不存在`); Robot_1.default.sendMessage(key, 'text', `${projectName}构建完成`); return; } const cacheString = fs_1.default.readFileSync(cacheFilePath).toString(); fs_1.default.unlinkSync(cacheFilePath); const cache = JSON.parse(cacheString); const logs = yield Utils_1.readGitLog(projectDir, cache.git); // const logs = readGitLog(projectDir, 'e858739336f65b33d1fe36c7b78e3738567e8ee3'); if (!logs || !logs.length) { console.log(`此次构建无差异`); Robot_1.default.sendMessage(key, 'text', `${projectName}构建完成`); return; } let logString = ''; logs.forEach(({ name, title }, index) => { 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_1.default.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; }