UNPKG

t-comm

Version:

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

213 lines (208 loc) 9.77 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var bite_formatBite = require('../bite/format-bite.js'); var nodejs_fs = require('../nodejs/fs.js'); var wecomRobot_optimizeRobotContent = require('../wecom-robot/optimize-robot-content.js'); var versionTip_config = require('./config.js'); var string_replaceAll = require('../string/replace-all.js'); function fmtDiff(diff) { var sign = ''; if (diff > 0) sign = '+';else if (diff < 0) sign = '-'; return "".concat(sign).concat(bite_formatBite.formatBite(Math.abs(diff))); } function generatePackageSizeSection(packageSize) { if (!(packageSize === null || packageSize === void 0 ? void 0 : packageSize.newSize)) return ''; var _packageSize$oldSize = packageSize.oldSize, oldSize = _packageSize$oldSize === void 0 ? 0 : _packageSize$oldSize, newSize = packageSize.newSize, _packageSize$oldUnpac = packageSize.oldUnpacked, oldUnpacked = _packageSize$oldUnpac === void 0 ? 0 : _packageSize$oldUnpac, _packageSize$newUnpac = packageSize.newUnpacked, newUnpacked = _packageSize$newUnpac === void 0 ? 0 : _packageSize$newUnpac, _packageSize$fileCoun = packageSize.fileCount, fileCount = _packageSize$fileCoun === void 0 ? 0 : _packageSize$fileCoun; var lines = ['', '', '**📦 包体积(tarball)**']; if (oldSize > 0) { var diff = newSize - oldSize; var ratio = diff / oldSize * 100; var ratioSign = ratio >= 0 ? '+' : ''; lines.push("- \u65E7\u5305\uFF1A".concat(bite_formatBite.formatBite(oldSize)), "- \u65B0\u5305\uFF1A".concat(bite_formatBite.formatBite(newSize)), "- \u5DEE\u5F02\uFF1A".concat(fmtDiff(diff), "\uFF08").concat(ratioSign).concat(ratio.toFixed(2), "%\uFF09")); } else { lines.push("- \u65B0\u5305\uFF1A".concat(bite_formatBite.formatBite(newSize))); } // 解包大小辅助展示:腾讯源拿不到旧包 unpackedSize 时,仅展示新包 if (newUnpacked > 0) { var fileCountStr = fileCount > 0 ? "\uFF0C\u6587\u4EF6\u6570\uFF1A".concat(fileCount) : ''; if (oldUnpacked > 0) { lines.push("- \u89E3\u5305\u5927\u5C0F\uFF1A".concat(bite_formatBite.formatBite(oldUnpacked), " \u2192 ").concat(bite_formatBite.formatBite(newUnpacked)).concat(fileCountStr)); } else { lines.push("- \u65B0\u5305\u89E3\u5305\u5927\u5C0F\uFF1A".concat(bite_formatBite.formatBite(newUnpacked)).concat(fileCountStr)); } } else if (fileCount > 0) { lines.push("- \u6587\u4EF6\u6570\uFF1A".concat(fileCount)); } return "".concat(lines.join('\n'), "\n"); } function optimizeContent() { var content = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; return wecomRobot_optimizeRobotContent.optimizeRobotContent({ content: content, maxLen: versionTip_config.MAX_CONTENT_LENGTH }); } function parseChangeLog(_ref) { var changelogStr = _ref.changelogStr, targetVersion = _ref.targetVersion; // 非大版本(1.0.0,2.0.0等),比如 1.1.1 // 不是第一个版本 var currentVersion = changelogStr.match(new RegExp("(?<=### \\[".concat(targetVersion, "\\].*\n).*?(?=\n##+ \\[?\\d+.\\d+.\\d+)"), 's')); // 大版本 // 不是第一个版本 if (!(currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion[0])) { currentVersion = changelogStr.match(new RegExp("(?<=## \\[".concat(targetVersion, "\\].*\n).*?(?=\n##+ \\[?\\d+.\\d+.\\d+)"), 's')); } // changeLog 的另一种形式,lerna 生成的 if (!(currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion[0])) { changelogStr = changelogStr.replace(/<\/?small>/g, ''); currentVersion = changelogStr.match(new RegExp("(?<=## ".concat(targetVersion, ".*\n).*?(?=\n##+ ?\\d+.\\d+.\\d+)"), 's')); } // 非大版本 // 第一个版本 if (!(currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion[0])) { currentVersion = changelogStr.match(new RegExp("(?<=### ".concat(targetVersion, ".*\n).*"), 's')); } // 大版本 // 第一个版本 if (!(currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion[0])) { currentVersion = changelogStr.match(new RegExp("(?<=## ".concat(targetVersion, ".*\n).*"), 's')); } if (!(currentVersion === null || currentVersion === void 0 ? void 0 : currentVersion[0])) { console.log("[GEN VERSION TIP] ERROR: NOT FOUND CHANGELOG INFO OF ".concat(targetVersion, " ")); return ''; } var changelog = currentVersion[0].replace(/\n\*(\s.*)/g, function (a, b) { return "\n-".concat(b); }); return changelog; } function getChangeLog(_ref2) { var targetVersion = _ref2.targetVersion, changeLogFilePath = _ref2.changeLogFilePath; var fs = nodejs_fs.getFs(); if (!fs.existsSync(changeLogFilePath)) { console.log("[GEN VERSION TIP] ERROR: NOT FOUND ".concat(changeLogFilePath, ". PLEASE GENERATE CHANGELOG. ")); return ''; } var changelogStr = fs.readFileSync(changeLogFilePath, 'utf8'); return parseChangeLog({ changelogStr: changelogStr, targetVersion: targetVersion }); } function generatePublishInfo(_ref3) { var appName = _ref3.appName, targetVersion = _ref3.version, homepage = _ref3.homepage, repoLink = _ref3.repoLink, issueLink = _ref3.issueLink, changeLogFilePath = _ref3.readmeFilePath, _ref3$showNpmLink = _ref3.showNpmLink, showNpmLink = _ref3$showNpmLink === void 0 ? false : _ref3$showNpmLink, publisher = _ref3.publisher, npmLink = _ref3.npmLink, pipelineUrl = _ref3.pipelineUrl, packageSize = _ref3.packageSize; string_replaceAll.replaceAllPolyfill(); var versionStr = "- \u7248\u672C\uFF1A<font color=\"comment\">".concat(targetVersion, "</font>\n"); // npmLink 优先级高于 showNpmLink,方便业务方接入腾讯内部 npm 镜像等自定义地址 var npmStr = ''; if (npmLink) { npmStr = "- npm: [".concat(appName, "](").concat(npmLink, ")\n"); } else if (showNpmLink) { npmStr = "- npm: [".concat(appName, "](https://npmjs.com/package/").concat(appName, ")\n"); } var issueStr = issueLink ? "- issue: [".concat(issueLink, "](").concat(issueLink, ")\n") : ''; var pRepoLink = repoLink.replace(/^git\+/, '').replace(/\.git$/, ''); var repoStr = repoLink ? "- Git: [".concat(pRepoLink, "](").concat(pRepoLink, ")\n") : ''; var homepageStr = homepage ? "- \u6587\u6863\uFF1A[".concat(homepage, "](").concat(homepage, ")\n") : ''; var pipelineStr = pipelineUrl ? "- \u6D41\u6C34\u7EBF\uFF1A[\u67E5\u770B\u6784\u5EFA\u8BE6\u60C5](".concat(pipelineUrl, ")\n") : ''; var sizeSection = generatePackageSizeSection(packageSize); var template = "### ".concat(appName, " \u66F4\u65B0\n\n\n\n").concat(versionStr).concat(npmStr).concat(repoStr).concat(issueStr).concat(homepageStr).concat(pipelineStr).concat(sizeSection, "\n\n"); var changelog = getChangeLog({ targetVersion: targetVersion, changeLogFilePath: changeLogFilePath }); var publisherDesc = publisher ? "\n\n- \u53D1\u5E03\u8005\uFF1A<@".concat(publisher, ">") : ''; var content = "".concat(template.concat(changelog).replaceAll('###', '\n\n###')).concat(publisherDesc); return optimizeContent(content); } /** * 生成版本信息,可以用来发送到群聊中 * @param {object} config 配置信息 * @param {string} config.readmeFilePath changelog文件地址 * @param {object} config.appInfo package.json信息 * @param {boolean} [config.showNpmLink] 是否显示 npm 链接(默认走 npmjs.com,被 npmLink 覆盖时优先用 npmLink) * @param {string} [config.publisher] 发布者别名,传入后会在末尾 `@发布者` * @param {string} [config.npmLink] 自定义 npm 详情页地址(如腾讯内部镜像),优先级高于 showNpmLink * @param {string} [config.pipelineUrl] 流水线/构建地址,调用方自行解析(蓝盾 BK_CI_* env 等) * @param {object} [config.packageSize] 包体积信息,存在时会渲染「📦 包体积」区块 * @returns {string} 版本信息 * @example * * const appInfo = require(`${rootPath}/package.json`); * const readmeFilePath = `${rootPath}/CHANGELOG.md`; * * const content = genVersionTip({ * readmeFilePath, * appInfo, * npmLink: 'https://mirrors.tencent.com/...', * pipelineUrl: 'https://devops.woa.com/...', * packageSize: { * oldSize: 1234567, * newSize: 1230000, * oldUnpacked: 4500000, * newUnpacked: 4490000, * fileCount: 256, * }, * }); */ function genVersionTip(_ref4) { var readmeFilePath = _ref4.readmeFilePath, appInfo = _ref4.appInfo, _ref4$showNpmLink = _ref4.showNpmLink, showNpmLink = _ref4$showNpmLink === void 0 ? false : _ref4$showNpmLink, publisher = _ref4.publisher, npmLink = _ref4.npmLink, pipelineUrl = _ref4.pipelineUrl, packageSize = _ref4.packageSize; var appName = appInfo.name, version = appInfo.version, _appInfo$homepage = appInfo.homepage, homepage = _appInfo$homepage === void 0 ? '' : _appInfo$homepage, bugs = appInfo.bugs, repository = appInfo.repository; console.log('[GEN VERSION TIP] APP INFO VERSION:', version); var issueLink = ''; var repoLink = ''; if (bugs === null || bugs === void 0 ? void 0 : bugs.url) { issueLink = bugs.url; } if (repository === null || repository === void 0 ? void 0 : repository.url) { repoLink = repository.url; } return generatePublishInfo({ showNpmLink: showNpmLink, appName: appName, version: version, homepage: homepage, repoLink: repoLink, issueLink: issueLink, readmeFilePath: readmeFilePath, publisher: publisher, npmLink: npmLink, pipelineUrl: pipelineUrl, packageSize: packageSize }); } exports.genVersionTip = genVersionTip; exports.parseChangeLog = parseChangeLog;