This commit is contained in:
Florent Vilmart
2016-02-24 08:50:04 -05:00
parent 3ca1e9f79e
commit f58befd7bb
4 changed files with 89 additions and 53 deletions

View File

@@ -1,4 +1,4 @@
var commander = require("../src/cli/utils/commander"); var commander = require("../src/cli/utils/commander").default;
var definitions = { var definitions = {
"arg0": "PROGRAM_ARG_0", "arg0": "PROGRAM_ARG_0",
@@ -9,10 +9,17 @@ var definitions = {
"arg2": { "arg2": {
env: "PROGRAM_ARG_2", env: "PROGRAM_ARG_2",
action: function(value) { action: function(value) {
return parseInt(value); var value = parseInt(value);
if (!Number.isInteger(value)) {
throw "port is invalid";
}
return value;
} }
}, },
"arg3": {} "arg3": {},
"arg4": {
default: "arg4Value"
}
} }
describe("commander additions", () => { describe("commander additions", () => {
@@ -23,6 +30,7 @@ describe("commander additions", () => {
delete commander.arg1; delete commander.arg1;
delete commander.arg2; delete commander.arg2;
delete commander.arg3; delete commander.arg3;
delete commander.arg4;
done(); done();
}) })
@@ -33,6 +41,7 @@ describe("commander additions", () => {
expect(commander.arg1).toEqual("arg1Value"); expect(commander.arg1).toEqual("arg1Value");
expect(commander.arg2).toEqual(2); expect(commander.arg2).toEqual(2);
expect(commander.arg3).toEqual("some"); expect(commander.arg3).toEqual("some");
expect(commander.arg4).toEqual("arg4Value");
done(); done();
}); });
@@ -46,12 +55,13 @@ describe("commander additions", () => {
expect(commander.arg0).toEqual("arg0ENVValue"); expect(commander.arg0).toEqual("arg0ENVValue");
expect(commander.arg1).toEqual("arg1ENVValue"); expect(commander.arg1).toEqual("arg1ENVValue");
expect(commander.arg2).toEqual(3); expect(commander.arg2).toEqual(3);
expect(commander.arg4).toEqual("arg4Value");
done(); done();
}); });
it("should load properly use args over env", (done) => { it("should load properly use args over env", (done) => {
commander.loadDefinitions(definitions); commander.loadDefinitions(definitions);
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value"], { commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "--arg4", "anotherArg4"], {
"PROGRAM_ARG_0": "arg0ENVValue", "PROGRAM_ARG_0": "arg0ENVValue",
"PROGRAM_ARG_1": "arg1ENVValue", "PROGRAM_ARG_1": "arg1ENVValue",
"PROGRAM_ARG_2": "4", "PROGRAM_ARG_2": "4",
@@ -59,7 +69,19 @@ describe("commander additions", () => {
expect(commander.arg0).toEqual("arg0Value"); expect(commander.arg0).toEqual("arg0Value");
expect(commander.arg1).toEqual("arg1ENVValue"); expect(commander.arg1).toEqual("arg1ENVValue");
expect(commander.arg2).toEqual(4); expect(commander.arg2).toEqual(4);
expect(commander.arg4).toEqual("anotherArg4");
done(); done();
}); });
}) it("should fail in action as port is invalid", (done) => {
commander.loadDefinitions(definitions);
expect(()=> {
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value"], {
"PROGRAM_ARG_0": "arg0ENVValue",
"PROGRAM_ARG_1": "arg1ENVValue",
"PROGRAM_ARG_2": "hello",
});
}).toThrow("port is invalid");
done();
});
});

View File

@@ -1,4 +1,4 @@
module.exports = { export default {
"appId": { "appId": {
env: "PARSE_SERVER_APPLICATION_ID", env: "PARSE_SERVER_APPLICATION_ID",
help: "Your Parse Application ID", help: "Your Parse Application ID",
@@ -11,9 +11,21 @@ module.exports = {
}, },
"serverURL": { "serverURL": {
env: "PARSE_SERVER_URL", env: "PARSE_SERVER_URL",
help: "URL to your parse server with http:// or https://", help: "URL to your parse server with http:// or https://.",
required: true required: true
}, },
"port": {
port: "PORT",
help: "The port to run the ParseServer. defaults to 1337.",
default: 1337,
action: function(opt) {
opt = parseInt(opt);
if (!Number.isInteger(opt)) {
throw new Error("The port is invalid");
}
return opt;
}
},
"databaseURI": { "databaseURI": {
env: "PARSE_SERVER_DATABASE_URI", env: "PARSE_SERVER_DATABASE_URI",
help: "The full URI to your mongodb database" help: "The full URI to your mongodb database"

View File

@@ -1,9 +1,9 @@
var path = require("path"); import path from 'path';
var express = require('express'); import express from 'express';
var ParseServer = require("../index").ParseServer; import ParseServer from '../index';
var definitions = require('./cli-definitions'); import definitions from './cli-definitions';
var program = require('./utils/commander'); import program from './utils/commander';
var colors = require('colors'); import colors from 'colors';
program.loadDefinitions(definitions); program.loadDefinitions(definitions);
@@ -34,10 +34,8 @@ program.on('--help', function(){
program.parse(process.argv, process.env); program.parse(process.argv, process.env);
var options = {};
if (program.args.length > 0 ) { if (program.args.length > 0 ) {
var jsonPath = program.args[0]; let jsonPath = program.args[0];
jsonPath = path.resolve(jsonPath); jsonPath = path.resolve(jsonPath);
options = require(jsonPath); options = require(jsonPath);
console.log(`Configuation loaded from ${jsonPath}`) console.log(`Configuation loaded from ${jsonPath}`)
@@ -51,27 +49,26 @@ if (!program.appId || !program.masterKey || !program.serverURL) {
process.exit(1); process.exit(1);
} }
var options = Object.keys(definitions).reduce(function (options, key) { let options = Object.keys(definitions).reduce(function (options, key) {
if (program[key]) { if (program[key]) {
options[key] = program[key]; options[key] = program[key];
} }
return options; return options;
}, options); }, options);
var app = express(); const app = express();
var api = new ParseServer(options); const api = new ParseServer(options);
app.use(options.mountPath, api); app.use(options.mountPath, api);
var port = process.env.PORT || 1337; app.listen(options.port, function() {
app.listen(port, function() {
for (let key in options) { for (let key in options) {
var value = options[key]; let value = options[key];
if (key == "masterKey") { if (key == "masterKey") {
value = "***REDACTED***"; value = "***REDACTED***";
} }
console.log(`${key}: ${value}`); console.log(`${key}: ${value}`);
} }
console.log(''); console.log('');
console.log('parse-server running on http://localhost:'+ port + options.mountPath); console.log('parse-server running on http://localhost:'+ optins.port + options.mountPath);
}); });

View File

@@ -1,11 +1,13 @@
var program = require('commander'); import { Command } from 'commander';
var _definitions; let _definitions;
var _reverseDefinitions; let _reverseDefinitions;
var _defaults; let _defaults;
program.loadDefinitions = function(definitions) {
Command.prototype.loadDefinitions = function(definitions) {
_definitions = definitions; _definitions = definitions;
Object.keys(definitions).reduce(function(program, opt){
Object.keys(definitions).reduce((program, opt) => {
if (typeof definitions[opt] == "object") { if (typeof definitions[opt] == "object") {
const additionalOptions = definitions[opt]; const additionalOptions = definitions[opt];
if (additionalOptions.required === true) { if (additionalOptions.required === true) {
@@ -14,15 +16,17 @@ program.loadDefinitions = function(definitions) {
return program.option(`--${opt} [${opt}]`, additionalOptions.help, additionalOptions.action); return program.option(`--${opt} [${opt}]`, additionalOptions.help, additionalOptions.action);
} }
} }
return program.option(`--${opt} [${opt}]`) return program.option(`--${opt} [${opt}]`);
}, program); }, this);
_defaults = Object.keys(definitions).reduce(function(defs, opt) {
_defaults = Object.keys(definitions).reduce((defs, opt) => {
if(_definitions[opt].default) { if(_definitions[opt].default) {
defs[opt] = _definitions[opt].default; defs[opt] = _definitions[opt].default;
} }
return defs; return defs;
}, {}); }, {});
_reverseDefinitions = Object.keys(definitions).reduce(function(object, key){
_reverseDefinitions = Object.keys(definitions).reduce((object, key) => {
let value = definitions[key]; let value = definitions[key];
if (typeof value == "object") { if (typeof value == "object") {
value = value.env; value = value.env;
@@ -34,21 +38,21 @@ program.loadDefinitions = function(definitions) {
}, {}); }, {});
/* istanbul ignore next */ /* istanbul ignore next */
program.on('--help', function(){ this.on('--help', function(){
console.log(' Configure From Environment:'); console.log(' Configure From Environment:');
console.log(''); console.log('');
Object.keys(_reverseDefinitions).forEach(function(key){ Object.keys(_reverseDefinitions).forEach((key) => {
console.log(` $ ${key}='${_reverseDefinitions[key]}'`); console.log(` $ ${key}='${_reverseDefinitions[key]}'`);
}); });
console.log(''); console.log('');
}); });
} }
var envParser = function(env = {}) { function parseEnvironment(env = {}) {
return Object.keys(_reverseDefinitions).reduce(function(options, key){ return Object.keys(_reverseDefinitions).reduce((options, key) => {
if (env[key]) { if (env[key]) {
const originalKey = _reverseDefinitions[key]; const originalKey = _reverseDefinitions[key];
let action = function(option) {return option;} let action = (option) => (option);
if (typeof _definitions[originalKey] === "object") { if (typeof _definitions[originalKey] === "object") {
action = _definitions[originalKey].action || action; action = _definitions[originalKey].action || action;
} }
@@ -58,23 +62,24 @@ var envParser = function(env = {}) {
}, {}); }, {});
} }
program._parse = program.parse; Command.prototype.setValuesIfNeeded = function(options) {
Object.keys(options).forEach((key) => {
program.parse = function(args, env) { if (!this[key]) {
program._parse(args); this[key] = options[key];
// Parse the environment first
var envOptions = envParser(env);
// Load the env if not passed from command line
Object.keys(envOptions).forEach(function(key){
if (!program[key]) {
program[key] = envOptions[key];
}
});
Object.keys(_defaults).forEach(function(key){
if (!program[key]) {
program[key] = _defaults[key];
} }
}); });
} }
module.exports = program; Command.prototype._parse = Command.prototype.parse;
Command.prototype.parse = function(args, env) {
this._parse(args);
// Parse the environment first
const envOptions = parseEnvironment(env);
// Load the env if not passed from command line
this.setValuesIfNeeded(envOptions);
this.setValuesIfNeeded(_defaults);
}
export default new Command();