UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

178 lines (175 loc) 7.59 kB
import { b as __awaiter, c as __generator, _ as __assign, a as __spreadArray } from '../tslib.es6-096fffdd.js'; import path__default from 'path'; import { formatBite } from '../bite/format-bite.mjs'; import { readFileSync } from '../fs/fs.mjs'; import { getGitCommitInfo } from '../git/git.mjs'; import { getComponentInfo, getPageTotal } from '../mp/page-total.mjs'; import { timeStampFormat } from '../time/time.mjs'; import { batchSendWxRobotBase64Img, batchSendWxRobotMarkdown } from '../wecom-robot/batch-send.mjs'; import 'fs'; import '../node/node-command.mjs'; import '@babel/runtime/helpers/typeof'; import '../node/fs-util.mjs'; import '../wecom-robot/base.mjs'; import '../wecom-robot/helper.mjs'; import 'axios'; import '../wecom-robot/send-img.mjs'; import '../node-img/img.mjs'; function parsePreviewResult(subPackageInfo, maxSubPackageNum) { if (maxSubPackageNum === void 0) { maxSubPackageNum = 10; } var subPackagesList = []; try { subPackagesList = JSON.parse(subPackageInfo); } catch (err) {} var previewMap = subPackagesList.reduce(function (acc, item) { var _a; return __assign(__assign({}, acc), (_a = {}, _a[item.name] = item.size, _a)); }, {}); var keyList = Object.keys(previewMap).filter(function (item) { return !['__APP__', '__FULL__'].includes(item); }); keyList.sort(function (a, b) { return previewMap[b] - previewMap[a]; }); keyList = keyList.slice(0, maxSubPackageNum); var list = __spreadArray(["\u4E3B\u5305: `".concat(formatBite(previewMap.__APP__), "`"), "\u603B\u5305: `".concat(formatBite(previewMap.__FULL__), "`")], keyList.map(function (key) { return "".concat(key.replace(/^\/|\/$/g, ''), ": `").concat(formatBite(previewMap[key]), "`"); }), true); return list.join(','); } var getRobotMessageItem = function getRobotMessageItem(key, value, isLight) { if (isLight === void 0) { isLight = false; } if (isLight) { return "".concat(key, "\uFF1A`").concat(value, "`"); } return "".concat(key, "\uFF1A").concat(value); }; function getDescMap(_a) { var robot = _a.robot, root = _a.root, env = _a.env, branch = _a.branch; return __awaiter(this, void 0, void 0, function () { var commitInfo, version, time, descMap; return __generator(this, function (_b) { commitInfo = getGitCommitInfo(root, false, false, false); version = ''; try { // eslint-disable-next-line @typescript-eslint/no-require-imports version = require(path__default.resolve(root, './package.json')).version; } catch (err) {} time = timeStampFormat(Date.now(), 'yyyy-MM-dd hh:mm:ss'); descMap = { branch: { title: '分支', content: "".concat(branch || commitInfo.branch) }, env: { title: '环境', content: "".concat(env) }, robot: { title: '机器人', content: "".concat(robot) }, version: { title: '版本', content: "".concat(version) || '' }, lastCommit: { title: '最后提交', content: "".concat(commitInfo.author, " - ").concat(commitInfo.message, " - ").concat(commitInfo.hash || '') }, time: { title: '时间', content: "".concat(time) } }; console.log('[CI] descMap: ', descMap); return [2 /*return*/, descMap]; }); }); } function getDescList(_a) { var bundleResult = _a.bundleResult, subPackageInfo = _a.subPackageInfo, robot = _a.robot, root = _a.root, env = _a.env, branch = _a.branch; return __awaiter(this, void 0, void 0, function () { var descMap, branchInfo, envInfo, robotInfo, versionInfo, lastCommitInfo, componentInfo, pageInfo, _b, componentTotal, _c, wxmlSizeTotal, _d, wxssSizeTotal, _e, jsSizeTotal, _f, vendorJsSize, _g, pageTotal, _h, subPackageTotal; return __generator(this, function (_j) { switch (_j.label) { case 0: return [4 /*yield*/, getDescMap({ robot: robot, root: root, env: env, branch: branch })]; case 1: descMap = _j.sent(); branchInfo = descMap.branch, envInfo = descMap.env, robotInfo = descMap.robot, versionInfo = descMap.version, lastCommitInfo = descMap.lastCommit; componentInfo = getComponentInfo(bundleResult); pageInfo = getPageTotal(bundleResult); _b = componentInfo.componentTotal, componentTotal = _b === void 0 ? 0 : _b, _c = componentInfo.wxmlSizeTotal, wxmlSizeTotal = _c === void 0 ? 0 : _c, _d = componentInfo.wxssSizeTotal, wxssSizeTotal = _d === void 0 ? 0 : _d, _e = componentInfo.jsSizeTotal, jsSizeTotal = _e === void 0 ? 0 : _e, _f = componentInfo.vendorJsSize, vendorJsSize = _f === void 0 ? 0 : _f; _g = pageInfo.pageTotal, pageTotal = _g === void 0 ? 0 : _g, _h = pageInfo.subPackageTotal, subPackageTotal = _h === void 0 ? 0 : _h; return [2 /*return*/, [[getRobotMessageItem(branchInfo.title, branchInfo.content, true), getRobotMessageItem(envInfo.title, envInfo.content, true), getRobotMessageItem(robotInfo.title, robotInfo.content, true), getRobotMessageItem(versionInfo.title, versionInfo.content, true), "[\u6784\u5EFA\u5730\u5740](".concat(process.env.BK_CI_BUILD_URL || '', ")"), getRobotMessageItem(lastCommitInfo.title, lastCommitInfo.content, false)].join(','), "- ".concat([getRobotMessageItem('页面数', pageTotal, true), getRobotMessageItem('组件数', componentTotal, true), getRobotMessageItem('分包数', subPackageTotal, true)].join(',')), "- ".concat([getRobotMessageItem('JS总大小', formatBite(jsSizeTotal), true), getRobotMessageItem('Vendor大小', formatBite(vendorJsSize), true), getRobotMessageItem('样式总大小', formatBite(wxssSizeTotal), true), getRobotMessageItem('WXML总大小', formatBite(wxmlSizeTotal), true)].join(',')), "- ".concat(parsePreviewResult(subPackageInfo, 5))]]; } }); }); } function sendWxMpCIMessageAfterPlugin(_a) { var appName = _a.appName, webhookUrl = _a.webhookUrl, previewCodeBase64Path = _a.previewCodeBase64Path, bundleResult = _a.bundleResult, subPackageInfo = _a.subPackageInfo, robot = _a.robot, root = _a.root, env = _a.env, branch = _a.branch; return __awaiter(this, void 0, void 0, function () { var descList, content, previewCodeBase64P; return __generator(this, function (_b) { switch (_b.label) { case 0: if (!webhookUrl) return [2 /*return*/]; return [4 /*yield*/, getDescList({ bundleResult: bundleResult, subPackageInfo: subPackageInfo, robot: robot, root: root, env: env, branch: branch })]; case 1: descList = _b.sent(); content = "> \u2705\u3010".concat(appName, " WX MP\u3011").concat(descList.join('\n')); return [4 /*yield*/, batchSendWxRobotMarkdown({ webhookUrl: webhookUrl, chatId: 'ALL', content: content })]; case 2: _b.sent(); previewCodeBase64P = readFileSync(previewCodeBase64Path); if (!previewCodeBase64P) return [2 /*return*/]; return [4 /*yield*/, batchSendWxRobotBase64Img({ img: previewCodeBase64P, chatId: 'ALL', webhookUrl: webhookUrl })]; case 3: _b.sent(); return [2 /*return*/]; } }); }); } export { sendWxMpCIMessageAfterPlugin };