mirror of https://github.com/certd/certd
refactor: 1.1.4
parent
461de8d269
commit
0317118cd9
|
@ -7,7 +7,6 @@ import { PipelineEntity } from '../modules/pipeline/entity/pipeline';
|
||||||
//import { logger } from '../utils/logger';
|
//import { logger } from '../utils/logger';
|
||||||
// load .env file in process.cwd
|
// load .env file in process.cwd
|
||||||
import { mergeConfig } from './loader';
|
import { mergeConfig } from './loader';
|
||||||
|
|
||||||
const development = {
|
const development = {
|
||||||
keys: '',
|
keys: '',
|
||||||
koa: {
|
koa: {
|
||||||
|
@ -16,7 +15,7 @@ const development = {
|
||||||
staticFile: {
|
staticFile: {
|
||||||
usePrecompiledGzip: true,
|
usePrecompiledGzip: true,
|
||||||
buffer: true,
|
buffer: true,
|
||||||
maxAge: 60 * 60 * 24 * 30 * 1000,
|
maxAge: 0,
|
||||||
gzip: true,
|
gzip: true,
|
||||||
dirs: {
|
dirs: {
|
||||||
default: {
|
default: {
|
||||||
|
@ -25,11 +24,6 @@ const development = {
|
||||||
alias: {
|
alias: {
|
||||||
'/': '/index.html',
|
'/': '/index.html',
|
||||||
},
|
},
|
||||||
files: {
|
|
||||||
'/index.html': {
|
|
||||||
maxAge: 60 * 60 * 1000,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -4,7 +4,7 @@ import _ from 'lodash';
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
function parseEnv() {
|
function parseEnv(defaultConfig: any) {
|
||||||
const config = {};
|
const config = {};
|
||||||
for (const key in process.env) {
|
for (const key in process.env) {
|
||||||
let keyName = key;
|
let keyName = key;
|
||||||
|
@ -13,21 +13,30 @@ function parseEnv() {
|
||||||
}
|
}
|
||||||
keyName = keyName.replace('certd_', '');
|
keyName = keyName.replace('certd_', '');
|
||||||
const configKey = keyName.replace('_', '.');
|
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;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function load(env = '') {
|
export function load(config, env = '') {
|
||||||
// Get document, or throw exception on error
|
// Get document, or throw exception on error
|
||||||
const yamlPath = path.join(process.cwd(), `.env.${env}.yaml`);
|
const yamlPath = path.join(process.cwd(), `.env.${env}.yaml`);
|
||||||
const doc = yaml.load(fs.readFileSync(yamlPath, 'utf8'));
|
const doc = yaml.load(fs.readFileSync(yamlPath, 'utf8'));
|
||||||
_.merge(doc, parseEnv());
|
_.merge(doc, parseEnv(config));
|
||||||
return doc;
|
return doc;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mergeConfig(config: any, envType: string) {
|
export function mergeConfig(config: any, envType: string) {
|
||||||
_.merge(config, load(envType));
|
_.merge(config, load(config, envType));
|
||||||
const keys = _.get(config, 'auth.jwt.secret');
|
const keys = _.get(config, 'auth.jwt.secret');
|
||||||
if (keys) {
|
if (keys) {
|
||||||
config.keys = keys;
|
config.keys = keys;
|
||||||
|
|
Loading…
Reference in New Issue