refactor: 1.1.4

pull/21/merge
xiaojunnuo 2023-07-03 13:42:48 +08:00
parent 461de8d269
commit 0317118cd9
2 changed files with 15 additions and 12 deletions

View File

@ -7,7 +7,6 @@ import { PipelineEntity } from '../modules/pipeline/entity/pipeline';
//import { logger } from '../utils/logger';
// load .env file in process.cwd
import { mergeConfig } from './loader';
const development = {
keys: '',
koa: {
@ -16,7 +15,7 @@ const development = {
staticFile: {
usePrecompiledGzip: true,
buffer: true,
maxAge: 60 * 60 * 24 * 30 * 1000,
maxAge: 0,
gzip: true,
dirs: {
default: {
@ -25,11 +24,6 @@ const development = {
alias: {
'/': '/index.html',
},
files: {
'/index.html': {
maxAge: 60 * 60 * 1000,
},
},
},
},
},

View File

@ -4,7 +4,7 @@ import _ from 'lodash';
const yaml = require('js-yaml');
const fs = require('fs');
function parseEnv() {
function parseEnv(defaultConfig: any) {
const config = {};
for (const key in process.env) {
let keyName = key;
@ -13,21 +13,30 @@ function parseEnv() {
}
keyName = keyName.replace('certd_', '');
const configKey = keyName.replace('_', '.');
_.set(config, configKey, process.env[key]);
const oldValue = _.get(defaultConfig, configKey);
let value: any = process.env[key];
if (typeof oldValue === 'boolean') {
value = value === 'true';
} else if (Number.isInteger(oldValue)) {
value = parseInt(value, 10);
} else if (typeof oldValue === 'number') {
value = parseFloat(value);
}
_.set(config, configKey, value);
}
return config;
}
export function load(env = '') {
export function load(config, env = '') {
// Get document, or throw exception on error
const yamlPath = path.join(process.cwd(), `.env.${env}.yaml`);
const doc = yaml.load(fs.readFileSync(yamlPath, 'utf8'));
_.merge(doc, parseEnv());
_.merge(doc, parseEnv(config));
return doc;
}
export function mergeConfig(config: any, envType: string) {
_.merge(config, load(envType));
_.merge(config, load(config, envType));
const keys = _.get(config, 'auth.jwt.secret');
if (keys) {
config.keys = keys;