all files / direwolf/lib/core/ config.js

91.67% Statements 11/12
50% Branches 3/6
100% Functions 3/3
91.67% Lines 11/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                                                                               
/**
 * Created by zdy on 2016/9/28.
 * 应用配置类
 */
 
'use strict';
 
const Path = require('path');
const _ = require('lodash');
 
class Config {
    constructor(options) {
        // 应用根目录
        this.baseDir = options.baseDir || process.cwd();
 
        // 框架的基础配置文件
        this._direwolfConfig = require('../config/config');
 
        this._options = options;
 
        // 合并配置文件
        _.assign(this, this.appConfig);
    }
 
    /**
     * 应用配置
     * @returns {*}
     */
    get appConfig() {
        // 应用中的默认配置
        let conf = require(Path.join(this.baseDir, this._direwolfConfig.config.default));
 
        // 生产环境下,再合并production的配置
        Iif (this.env === 'production') {
            conf = _.assign(conf, require(Path.join(this.baseDir, this._direwolfConfig.config.production)));
        }
 
        return conf;
    }
 
    /**
     * 应用环境
     * @returns {string}
     */
    get env() {
        // 默认为development
        return process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
    }
}
 
module.exports = Config;