t-comm
Version:
专业、稳定、纯粹的工具库
156 lines (149 loc) • 5.46 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib_es6 = require('../tslib.es6-01322ba9.js');
var fs = require('fs');
var path = require('path');
var fs_fs = require('../fs/fs.js');
var rainbow_rainbowUser = require('../rainbow/rainbow-user.js');
require('axios');
require('../rainbow/helper/helper.js');
require('../rainbow/sdk.js');
require('../json/json-parse.js');
require('../list/flat.js');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
var path__namespace = /*#__PURE__*/_interopNamespace(path);
// 从七彩石获取CI配置
function getCIConfig(rainbowConfigKey, rainbowSecretInfo) {
return tslib_es6.__awaiter(this, void 0, void 0, function () {
var res, str;
return tslib_es6.__generator(this, function (_a) {
switch (_a.label) {
case 0:
res = {};
return [4 /*yield*/, rainbow_rainbowUser.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__namespace.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();
fs_fs.writeFileSync(path__namespace.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 tslib_es6.__awaiter(this, void 0, void 0, function () {
var rainbowSecretInfo, config, robot, rainbowPrivateKey, _b, localEnv, _c, wxPrivateKey, privateKey;
return tslib_es6.__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*/, rainbow_rainbowUser.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 tslib_es6.__awaiter(this, void 0, void 0, function () {
var branch, env, root, rainbowConfigKey, rainbowAppId, rainbowEnvName, rainbowGroupName;
return tslib_es6.__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*/];
});
});
}
exports.getCIConfig = getCIConfig;
exports.getRobot = getRobot;
exports.writeEnvAndPrivateKey = writeEnvAndPrivateKey;
exports.writeEnvAndPrivateKeyByOptions = writeEnvAndPrivateKeyByOptions;