UNPKG

t-comm

Version:

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

243 lines (240 loc) 7.54 kB
import { b as __awaiter, c as __generator, _ as __assign } from '../../tslib.es6-096fffdd.js'; import { compareTwoObj } from '../../object/compare.mjs'; import { getJsonFromLog, saveJsonToLog } from '../../node/fs-util.mjs'; import { getAllDevopsTemplateInstances } from '../template.mjs'; import { DEVOPS_MP_CI_CONFIG } from './config.mjs'; import '../../object/equal.mjs'; import 'fs'; import 'path'; import '../../fs/fs.mjs'; import '../../time/time.mjs'; import 'axios'; import '../token.mjs'; var CI_SEPARATOR = DEVOPS_MP_CI_CONFIG.CI_SEPARATOR, VALID_ENVS = DEVOPS_MP_CI_CONFIG.VALID_ENVS; var remoteInstancesMap = {}; function genOneFileFromRainbowGroup(list) { var obj = list.reduce(function (acc, item) { acc[item.key] = { value: item.value, value_type: item.value_type }; return acc; }, {}); return obj; } function parseRobotMap(obj) { if (obj === void 0) { obj = {}; } var branches = Object.keys(obj); var res = {}; for (var _i = 0, branches_1 = branches; _i < branches_1.length; _i++) { var branch = branches_1[_i]; var envs = Object.keys(obj[branch]); for (var _a = 0, envs_1 = envs; _a < envs_1.length; _a++) { var env = envs_1[_a]; if (!VALID_ENVS.includes(env)) { continue; } var robotNumber = obj[branch][env]; res[robotNumber] = { branch: branch, env: env }; } } console.log('[parseRobotMap] res: ', res); return res; } /** * 获取流水线名称 * @param {object} config 配置信息 * @ignore * * @example * getPipelineName({ * robot: '26', * env: 'release', * branch: 'feature/team-pvp-kpl', * ciKey: 'convert_wz', * prefix: 'wxci', * }) * // wxci__convert_wz__26__release__feature/team-pvp-kpl */ function getPipelineName(_a) { var robot = _a.robot, env = _a.env, branch = _a.branch, ciKey = _a.ciKey, prefix = _a.prefix; return [prefix, ciKey, robot, env, branch].join(CI_SEPARATOR); } function parseInstanceName(name) { var list = name.split(CI_SEPARATOR); var prefix = list[0], projectShortName = list[1], robotNumber = list[2], env = list[3], branch = list[4]; return { prefix: prefix, projectShortName: projectShortName, robotNumber: robotNumber, env: env, branch: branch }; } function getPipelineParam(_a) { var _b; var branch = _a.branch, env = _a.env, rainbowConfigKey = _a.rainbowConfigKey, repo = _a.repo, isWxCI = _a.isWxCI, devopsParams = _a.devopsParams; var res = [{ id: 'rainbowConfigKey', required: true, type: 'STRING', defaultValue: rainbowConfigKey, desc: '七彩石配置的key', readOnly: false }, { id: 'repo', required: true, type: 'STRING', defaultValue: repo, desc: '仓库', readOnly: false }, { id: 'branch', required: true, type: 'STRING', defaultValue: branch, desc: '分支', readOnly: false }, { id: 'env', required: true, type: 'STRING', defaultValue: env, desc: '环境', readOnly: false }, { id: 'forceNpm', required: true, type: 'STRING', // 从 devopsParams 中获取 defaultValue: (_b = devopsParams === null || devopsParams === void 0 ? void 0 : devopsParams.forceNpm) !== null && _b !== void 0 ? _b : '1', desc: '是否强制使用 NPM,默认是', readOnly: false }]; if (isWxCI) { res.push.apply(res, [{ id: 'needSourceMap', required: true, type: 'STRING', defaultValue: '0', desc: '是否需要sourceMap', readOnly: false }, { id: 'tamProjectId', required: true, type: 'STRING', defaultValue: '', desc: 'Tam上报ID', readOnly: false }, { id: 'useDevopsWXCIPlugin', required: true, type: 'STRING', defaultValue: (devopsParams === null || devopsParams === void 0 ? void 0 : devopsParams.useDevopsWXCIPlugin) || '0', desc: '是否使用蓝盾平台小程序CI插件', readOnly: false }]); } else { res.push.apply(res, [{ id: 'useNode16', required: true, type: 'STRING', defaultValue: (devopsParams === null || devopsParams === void 0 ? void 0 : devopsParams.useDevopsWXCIPlugin) == '1' ? '1' : '0', desc: '是否使用 node 16 版本', readOnly: false }]); } return res; } function compareFromLogFile(key, obj) { var originFile = getJsonFromLog(key); var compareRes = compareTwoObj(originFile, obj); return compareRes; } var getTemplateInstanceFileName = function getTemplateInstanceFileName(isWxCI) { return "devops-template-instances-".concat(isWxCI ? 'wx' : 'qq', ".json"); }; var getRainbowMpCIFileName = function getRainbowMpCIFileName(isWxCI) { return "devops-mp-ci-rainbow-".concat(isWxCI ? 'wx' : 'qq', ".json"); }; function isPipelineUpdated(_a) { var pipelineName = _a.pipelineName, isWxCI = _a.isWxCI, forceUpdate = _a.forceUpdate, devopsConfig = _a.devopsConfig, templateIdMap = _a.templateIdMap; return __awaiter(this, void 0, void 0, function () { var _b, prefix, projectShortName, robotNumber, name, templateId, remoteInstances, _c, _d, sameRobotPipeline; return __generator(this, function (_e) { switch (_e.label) { case 0: _b = parseInstanceName(pipelineName), prefix = _b.prefix, projectShortName = _b.projectShortName, robotNumber = _b.robotNumber; name = "".concat([prefix, projectShortName, robotNumber].join(CI_SEPARATOR)).concat(CI_SEPARATOR); templateId = isWxCI ? templateIdMap.WX_MP_CI : templateIdMap.QQ_MP_CI; remoteInstances = remoteInstancesMap[templateId] || []; if (!!remoteInstances.length) return [3 /*break*/, 2]; _c = remoteInstancesMap; _d = templateId; return [4 /*yield*/, getAllDevopsTemplateInstances(__assign(__assign({}, devopsConfig), { templateId: templateId }))]; case 1: _c[_d] = _e.sent(); remoteInstances = remoteInstancesMap[templateId]; saveJsonToLog(remoteInstances, getTemplateInstanceFileName(isWxCI)); return [3 /*break*/, 3]; case 2: console.log('cache'); _e.label = 3; case 3: sameRobotPipeline = remoteInstances.find(function (item) { return item.pipelineName.startsWith(name); }); if (sameRobotPipeline) { // 如果是强制更新,可以注释掉下面这段 if (!forceUpdate && sameRobotPipeline.pipelineName === pipelineName) { return [2 /*return*/, { type: 'NONE' }]; } return [2 /*return*/, __assign({ type: 'UPDATE' }, sameRobotPipeline)]; } return [2 /*return*/, { type: 'CREATE' }]; } }); }); } function getUnusedPipelineList(_a) { var usefulList = _a.usefulList, isWxCI = _a.isWxCI, templateIdMap = _a.templateIdMap; var templateId = isWxCI ? templateIdMap.WX_MP_CI : templateIdMap.QQ_MP_CI; var remoteInstances = remoteInstancesMap[templateId] || []; return remoteInstances.filter(function (item) { return usefulList.indexOf(item.pipelineId) === -1; }); } export { compareFromLogFile, genOneFileFromRainbowGroup, getPipelineName, getPipelineParam, getRainbowMpCIFileName, getTemplateInstanceFileName, getUnusedPipelineList, isPipelineUpdated, parseInstanceName, parseRobotMap };