2016-11-24 15:47:41 -05:00
|
|
|
/* eslint-disable no-console */
|
2016-02-24 08:50:04 -05:00
|
|
|
import { Command } from 'commander';
|
2016-04-04 20:02:34 -04:00
|
|
|
import path from 'path';
|
2016-02-24 08:50:04 -05:00
|
|
|
let _definitions;
|
|
|
|
|
let _reverseDefinitions;
|
|
|
|
|
let _defaults;
|
|
|
|
|
|
|
|
|
|
Command.prototype.loadDefinitions = function(definitions) {
|
2016-02-19 20:16:14 -05:00
|
|
|
_definitions = definitions;
|
2016-04-04 20:02:34 -04:00
|
|
|
|
2016-02-24 08:50:04 -05:00
|
|
|
Object.keys(definitions).reduce((program, opt) => {
|
2016-02-19 20:16:14 -05:00
|
|
|
if (typeof definitions[opt] == "object") {
|
|
|
|
|
const additionalOptions = definitions[opt];
|
|
|
|
|
if (additionalOptions.required === true) {
|
|
|
|
|
return program.option(`--${opt} <${opt}>`, additionalOptions.help, additionalOptions.action);
|
|
|
|
|
} else {
|
|
|
|
|
return program.option(`--${opt} [${opt}]`, additionalOptions.help, additionalOptions.action);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-24 08:50:04 -05:00
|
|
|
return program.option(`--${opt} [${opt}]`);
|
|
|
|
|
}, this);
|
2016-04-04 20:02:34 -04:00
|
|
|
|
2016-02-24 08:50:04 -05:00
|
|
|
_defaults = Object.keys(definitions).reduce((defs, opt) => {
|
2016-02-23 21:33:36 -05:00
|
|
|
if(_definitions[opt].default) {
|
|
|
|
|
defs[opt] = _definitions[opt].default;
|
|
|
|
|
}
|
|
|
|
|
return defs;
|
|
|
|
|
}, {});
|
2016-04-04 20:02:34 -04:00
|
|
|
|
2016-02-24 08:50:04 -05:00
|
|
|
_reverseDefinitions = Object.keys(definitions).reduce((object, key) => {
|
2016-11-24 15:47:41 -05:00
|
|
|
let value = definitions[key];
|
|
|
|
|
if (typeof value == "object") {
|
|
|
|
|
value = value.env;
|
|
|
|
|
}
|
|
|
|
|
if (value) {
|
|
|
|
|
object[value] = key;
|
|
|
|
|
}
|
|
|
|
|
return object;
|
|
|
|
|
}, {});
|
2016-04-04 20:02:34 -04:00
|
|
|
|
2017-06-20 09:15:26 -07:00
|
|
|
/* istanbul ignore next */
|
2016-11-24 15:47:41 -05:00
|
|
|
this.on('--help', function(){
|
2016-02-19 20:16:14 -05:00
|
|
|
console.log(' Configure From Environment:');
|
|
|
|
|
console.log('');
|
2016-02-24 08:50:04 -05:00
|
|
|
Object.keys(_reverseDefinitions).forEach((key) => {
|
2016-02-19 20:16:14 -05:00
|
|
|
console.log(` $ ${key}='${_reverseDefinitions[key]}'`);
|
|
|
|
|
});
|
|
|
|
|
console.log('');
|
|
|
|
|
});
|
2016-11-30 13:48:49 +00:00
|
|
|
};
|
2016-02-19 20:16:14 -05:00
|
|
|
|
2016-02-24 08:50:04 -05:00
|
|
|
function parseEnvironment(env = {}) {
|
|
|
|
|
return Object.keys(_reverseDefinitions).reduce((options, key) => {
|
2016-02-19 20:16:14 -05:00
|
|
|
if (env[key]) {
|
|
|
|
|
const originalKey = _reverseDefinitions[key];
|
2016-02-24 08:50:04 -05:00
|
|
|
let action = (option) => (option);
|
2016-02-19 20:16:14 -05:00
|
|
|
if (typeof _definitions[originalKey] === "object") {
|
|
|
|
|
action = _definitions[originalKey].action || action;
|
|
|
|
|
}
|
|
|
|
|
options[_reverseDefinitions[key]] = action(env[key]);
|
|
|
|
|
}
|
2016-04-04 20:02:34 -04:00
|
|
|
return options;
|
2016-02-19 20:16:14 -05:00
|
|
|
}, {});
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 20:02:34 -04:00
|
|
|
function parseConfigFile(program) {
|
|
|
|
|
let options = {};
|
|
|
|
|
if (program.args.length > 0) {
|
|
|
|
|
let jsonPath = program.args[0];
|
|
|
|
|
jsonPath = path.resolve(jsonPath);
|
2016-12-07 15:17:05 -08:00
|
|
|
const jsonConfig = require(jsonPath);
|
2016-04-04 20:02:34 -04:00
|
|
|
if (jsonConfig.apps) {
|
|
|
|
|
if (jsonConfig.apps.length > 1) {
|
|
|
|
|
throw 'Multiple apps are not supported';
|
|
|
|
|
}
|
|
|
|
|
options = jsonConfig.apps[0];
|
|
|
|
|
} else {
|
|
|
|
|
options = jsonConfig;
|
|
|
|
|
}
|
|
|
|
|
Object.keys(options).forEach((key) => {
|
2016-12-07 15:17:05 -08:00
|
|
|
const value = options[key];
|
2016-04-04 20:02:34 -04:00
|
|
|
if (!_definitions[key]) {
|
|
|
|
|
throw `error: unknown option ${key}`;
|
|
|
|
|
}
|
2016-12-07 15:17:05 -08:00
|
|
|
const action = _definitions[key].action;
|
2016-04-04 20:02:34 -04:00
|
|
|
if (action) {
|
|
|
|
|
options[key] = action(value);
|
|
|
|
|
}
|
2016-11-30 13:48:49 +00:00
|
|
|
});
|
2016-07-20 16:08:22 -04:00
|
|
|
console.log(`Configuration loaded from ${jsonPath}`)
|
2016-04-04 20:02:34 -04:00
|
|
|
}
|
|
|
|
|
return options;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-24 08:50:04 -05:00
|
|
|
Command.prototype.setValuesIfNeeded = function(options) {
|
|
|
|
|
Object.keys(options).forEach((key) => {
|
2016-11-30 13:48:49 +00:00
|
|
|
if (!this.hasOwnProperty(key)) {
|
2016-11-24 15:47:41 -05:00
|
|
|
this[key] = options[key];
|
|
|
|
|
}
|
2016-02-24 08:50:04 -05:00
|
|
|
});
|
2016-11-30 13:48:49 +00:00
|
|
|
};
|
2016-02-24 08:50:04 -05:00
|
|
|
|
|
|
|
|
Command.prototype._parse = Command.prototype.parse;
|
2016-02-19 20:16:14 -05:00
|
|
|
|
2016-02-24 08:50:04 -05:00
|
|
|
Command.prototype.parse = function(args, env) {
|
|
|
|
|
this._parse(args);
|
2016-02-19 20:16:14 -05:00
|
|
|
// Parse the environment first
|
2016-02-24 08:50:04 -05:00
|
|
|
const envOptions = parseEnvironment(env);
|
2016-04-04 20:02:34 -04:00
|
|
|
const fromFile = parseConfigFile(this);
|
2016-02-19 20:16:14 -05:00
|
|
|
// Load the env if not passed from command line
|
2016-02-24 08:50:04 -05:00
|
|
|
this.setValuesIfNeeded(envOptions);
|
2016-04-04 20:02:34 -04:00
|
|
|
// Load from file to override
|
|
|
|
|
this.setValuesIfNeeded(fromFile);
|
|
|
|
|
// Last set the defaults
|
2016-02-24 08:50:04 -05:00
|
|
|
this.setValuesIfNeeded(_defaults);
|
2016-11-30 13:48:49 +00:00
|
|
|
};
|
2016-02-19 20:16:14 -05:00
|
|
|
|
2016-04-04 20:02:34 -04:00
|
|
|
Command.prototype.getOptions = function() {
|
|
|
|
|
return Object.keys(_definitions).reduce((options, key) => {
|
|
|
|
|
if (typeof this[key] !== 'undefined') {
|
|
|
|
|
options[key] = this[key];
|
|
|
|
|
}
|
|
|
|
|
return options;
|
|
|
|
|
}, {});
|
2016-11-30 13:48:49 +00:00
|
|
|
};
|
2016-04-04 20:02:34 -04:00
|
|
|
|
2016-02-24 08:50:04 -05:00
|
|
|
export default new Command();
|
2016-11-24 15:47:41 -05:00
|
|
|
/* eslint-enable no-console */
|