t-comm
Version:
专业、稳定、纯粹的工具库
126 lines (123 loc) • 4.51 kB
JavaScript
import { b as __awaiter, c as __generator } from '../tslib.es6-48fa7a9b.js';
import * as fs from 'fs';
import * as path from 'path';
import { fetchRainbowConfig } from '../rainbow/rainbow-user.js';
import { writeFileSync } from '../fs/fs.js';
import '../rainbow/helper/helper.js';
import '../base/list/flat.js';
import '@babel/runtime/helpers/typeof';
// 从七彩石获取CI配置
function getCIConfig(rainbowConfigKey, rainbowSecretInfo) {
return __awaiter(this, void 0, void 0, function () {
var res, str;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
res = {};
return [4 /*yield*/, fetchRainbowConfig(rainbowConfigKey, rainbowSecretInfo)];
case 1:
str = _a.sent();
try {
res = JSON.parse(str);
} catch (err) {}
return [2 /*return*/, res];
}
});
});
}
// 获取robot
function getRobot(_a) {
var _b, _c;
var config = _a.config,
branch = _a.branch,
env = _a.env;
var configRobot = (_c = (_b = config === null || config === void 0 ? void 0 : config.robotMap) === null || _b === void 0 ? void 0 : _b[branch]) === null || _c === void 0 ? void 0 : _c[env];
if (configRobot) {
return configRobot;
}
// if (env === 'release') return 2;
return 1;
}
// 写入环境变量
function writeEnv(robot, localEnv) {
if (localEnv === void 0) {
localEnv = '';
}
fs.writeFileSync('.env.local', "\n".concat(localEnv, "\nVUE_APP_AUTHOR=CI Robot ").concat(robot, "\n"), {
flag: 'a',
encoding: 'utf-8'
});
}
// 写入密钥
function writePrivateKey(root, privateKey) {
var nRoot = root || process.cwd();
writeFileSync(path.resolve(nRoot, 'private.key'), privateKey);
}
function writeEnvAndPrivateKey(_a) {
var branch = _a.branch,
env = _a.env,
root = _a.root,
rainbowConfigKey = _a.rainbowConfigKey,
rainbowAppId = _a.rainbowAppId,
rainbowEnvName = _a.rainbowEnvName,
rainbowGroupName = _a.rainbowGroupName;
return __awaiter(this, void 0, void 0, function () {
var rainbowSecretInfo, config, robot, rainbowPrivateKey, _b, localEnv, _c, wxPrivateKey, privateKey;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
rainbowSecretInfo = {
appId: rainbowAppId,
envName: rainbowEnvName,
groupName: rainbowGroupName
};
return [4 /*yield*/, getCIConfig(rainbowConfigKey, rainbowSecretInfo)];
case 1:
config = _d.sent();
console.log('[CI] config: ', config);
robot = getRobot({
config: config,
branch: branch,
env: env
});
rainbowPrivateKey = config.rainbowPrivateKey, _b = config.localEnv, localEnv = _b === void 0 ? '' : _b, _c = config.wxPrivateKey, wxPrivateKey = _c === void 0 ? '' : _c;
writeEnv(robot, localEnv);
privateKey = wxPrivateKey;
if (!!privateKey) return [3 /*break*/, 3];
console.log('[CI] No Default PrivateKey');
return [4 /*yield*/, fetchRainbowConfig(rainbowPrivateKey, rainbowSecretInfo)];
case 2:
privateKey = _d.sent();
_d.label = 3;
case 3:
console.log('[CI] privateKey: ', privateKey);
writePrivateKey(root, privateKey);
return [2 /*return*/];
}
});
});
}
function writeEnvAndPrivateKeyByOptions(options) {
return __awaiter(this, void 0, void 0, function () {
var branch, env, root, rainbowConfigKey, rainbowAppId, rainbowEnvName, rainbowGroupName;
return __generator(this, function (_a) {
console.log('[options] ', options);
branch = options.branch, env = options.env, root = options.root, rainbowConfigKey = options.configKey, rainbowAppId = options.appid, rainbowEnvName = options.envName, rainbowGroupName = options.groupName;
if (!branch || !env || !root || !rainbowConfigKey || !rainbowAppId || !rainbowEnvName || !rainbowGroupName) {
console.error('缺少必要参数,请检查!');
return [2 /*return*/];
}
writeEnvAndPrivateKey({
branch: branch,
env: env,
root: root,
rainbowConfigKey: rainbowConfigKey,
rainbowAppId: rainbowAppId,
rainbowEnvName: rainbowEnvName,
rainbowGroupName: rainbowGroupName
});
return [2 /*return*/];
});
});
}
export { getCIConfig, getRobot, writeEnvAndPrivateKey, writeEnvAndPrivateKeyByOptions };