all files / lib/ config.js

100% Statements 23/23
100% Branches 14/14
100% Functions 1/1
100% Lines 22/22
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                  
import fs from 'fs';
import debug from 'debug';
import path from 'path';
import env2obj from 'env2obj';
const log = debug('helper:config:info');
const error = debug('helper:config:error');
export class Config {
  constructor(basedir = process.env.PWD, prefix = 'APP') {
    let env = process.env.NODE_ENV || 'development';
    this.file = {};
    if (fs.existsSync(path.join(basedir, 'config.js'))) {
      this.file = require(path.join(basedir, 'config.js'), 'utf-8')[env];
    } else {
      log('file', 'did not use ', path.join(basedir, 'config.js'))
    }
    this.fileDefault = {};
    if (fs.existsSync(path.join(basedir, 'config.default.js'))) {
      this.fileDefault =
          require(path.join(basedir, 'config.default.js'), 'utf-8')[env]
    } else {
      log('file', 'did not use ', path.join(basedir, 'config.default.js'))
    }
    this.env = env2obj(prefix);
    this.config = Object.assign({}, this.fileDefault, this.file, this.env);
    log('used config', this.config)
  }
}
const config = new Config();
 
exports.default = config.config;