Files
cre-app-godo/config/index.js
2021-07-13 16:53:42 +09:00

71 lines
2.2 KiB
JavaScript

const commandLineArgs = require('command-line-args')
, xmlParserConfig = require('./xmlParserConfig')
, godoParams = require('./params.json')
, logger = require("logops");
class Config {
constructor() {
this.options = {}
this.requires = ['env', 'gateway', 'quix24', 'mongo']
this.init()
this.validate()
this.finalize()
}
init() {
try {
this.options = commandLineArgs([
{ name: 'env', alias: 'e', type: String, defaultValue: ['production'] },
{ name: 'host', type: String },
{ name: 'port', type: Number },
{ name: 'database', alias: 'd', type: String },
{ name: 'username', alias: 'u', type: String },
{ name: 'password', alias: 'p', type: String },
{ name: 'mongo', alias: 'm', type: String },
{ name: 'redis', alias: 'r', type: String },
{ name: 'level', alias: 'l', type: String },
{ name: 'gateway', alias: 'g', type: String },
{ name: 'quix24', alias: 'q', type: String }
]);
} catch (e) {
logger.debug('Command line arguments interpret failed :', e.message)
logger.debug('expected arguments : ', JSON.stringify(this.requires))
process.exit(1)
}
}
finalize() {
if (this.options.env == 'development') {
this.options.level = this.options.level ? this.options.level : "DEBUG"
logger.formatters.dev.omit = ['pid', 'port', 'hostname', 'app'];
// logger.format = logger.formatters.dev;
} else {
this.options.level = this.options.level ? this.options.level : "WARN"
}
this.options.xmlParser = xmlParserConfig
this.options.godoParams = godoParams
logger.formatters.json.omit = ['pid', 'port', 'hostname', 'app'];
logger.setLevel(this.options.level)
logger.debug(this.options, 'Environment setting options')
}
validate() {
for (let key in this.options) {
if (this.options[key]) {
delete this.requires[this.requires.indexOf(key)]
}
}
this.requires = this.requires.filter(function (el) {
return el != null;
})
if (this.requires.length) {
logger.debug('Process terminated invalid required arguments: ', JSON.stringify(this.requires))
process.exit(1)
}
}
}
let config = new Config;
module.exports = config.options