@gdjiami/cli
Version:
CLI for build front end project.
77 lines (76 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyProxyToExpress = exports.proxyInfomation = exports.interpolateProxy = void 0;
var tslib_1 = require("tslib");
var utils_1 = require("./utils");
var http_proxy_middleware_1 = tslib_1.__importDefault(require("http-proxy-middleware"));
/**
* 解析配置中的环境变量
* @param proxy
*/
function interpolateProxy(proxy, local) {
if (Array.isArray(proxy)) {
return proxy.map(function (i) {
return tslib_1.__assign(tslib_1.__assign({}, i), { context: Array.isArray(i.context) ? i.context.map(function (c) { return utils_1.interpolate(c, local); }) : utils_1.interpolate(i.context, local), target: i.target && utils_1.interpolate(i.target, local) });
});
}
else if (typeof proxy === 'object') {
var res_1 = {};
Object.keys(proxy).forEach(function (context) {
var value = proxy[context];
res_1[utils_1.interpolate(context, local)] =
typeof value === 'string'
? utils_1.interpolate(value, local)
: tslib_1.__assign(tslib_1.__assign({}, value), { target: utils_1.interpolate(value.target, local) });
});
return res_1;
}
return proxy;
}
exports.interpolateProxy = interpolateProxy;
/**
* print proxy proxyInfomation
*/
function proxyInfomation(proxy) {
if (Array.isArray(proxy)) {
return proxy
.map(function (i) {
return (Array.isArray(i.context) ? i.context.join(', ') : i.context) + " ~ " + i.target;
})
.join('\n');
}
else if (typeof proxy === 'object') {
return Object.keys(proxy)
.map(function (context) {
var value = proxy[context];
var targetStr = typeof value === 'object' ? value.target : value;
return context + " ~> " + targetStr;
})
.join('\n');
}
return '';
}
exports.proxyInfomation = proxyInfomation;
function applyProxyToExpress(proxy, app) {
var proxyOptions = [];
if (Array.isArray(proxy)) {
proxyOptions = proxy;
}
else {
proxyOptions = Object.keys(proxy).map(function (context) {
var value = proxy[context];
if (typeof value === 'string') {
return { context: context, target: value, logLevel: 'warn' };
}
return tslib_1.__assign({ context: context, logLevel: 'warn' }, value);
});
}
proxyOptions.forEach(function (proxyConfig) {
var context = proxyConfig.context;
if (proxyConfig.target) {
var middleware = http_proxy_middleware_1.default(proxyConfig);
app.use(context, middleware);
}
});
}
exports.applyProxyToExpress = applyProxyToExpress;