Skip to content

Commit f58befd

Browse files
committed
ES6-ify
1 parent 3ca1e9f commit f58befd

File tree

4 files changed

+89
-53
lines changed

4 files changed

+89
-53
lines changed

spec/CLI.spec.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var commander = require("../src/cli/utils/commander");
1+
var commander = require("../src/cli/utils/commander").default;
22

33
var definitions = {
44
"arg0": "PROGRAM_ARG_0",
@@ -9,10 +9,17 @@ var definitions = {
99
"arg2": {
1010
env: "PROGRAM_ARG_2",
1111
action: function(value) {
12-
return parseInt(value);
12+
var value = parseInt(value);
13+
if (!Number.isInteger(value)) {
14+
throw "port is invalid";
15+
}
16+
return value;
1317
}
1418
},
15-
"arg3": {}
19+
"arg3": {},
20+
"arg4": {
21+
default: "arg4Value"
22+
}
1623
}
1724

1825
describe("commander additions", () => {
@@ -23,6 +30,7 @@ describe("commander additions", () => {
2330
delete commander.arg1;
2431
delete commander.arg2;
2532
delete commander.arg3;
33+
delete commander.arg4;
2634
done();
2735
})
2836

@@ -33,6 +41,7 @@ describe("commander additions", () => {
3341
expect(commander.arg1).toEqual("arg1Value");
3442
expect(commander.arg2).toEqual(2);
3543
expect(commander.arg3).toEqual("some");
44+
expect(commander.arg4).toEqual("arg4Value");
3645
done();
3746
});
3847

@@ -46,20 +55,33 @@ describe("commander additions", () => {
4655
expect(commander.arg0).toEqual("arg0ENVValue");
4756
expect(commander.arg1).toEqual("arg1ENVValue");
4857
expect(commander.arg2).toEqual(3);
58+
expect(commander.arg4).toEqual("arg4Value");
4959
done();
5060
});
5161

5262
it("should load properly use args over env", (done) => {
5363
commander.loadDefinitions(definitions);
54-
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value"], {
64+
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "--arg4", "anotherArg4"], {
5565
"PROGRAM_ARG_0": "arg0ENVValue",
5666
"PROGRAM_ARG_1": "arg1ENVValue",
5767
"PROGRAM_ARG_2": "4",
5868
});
5969
expect(commander.arg0).toEqual("arg0Value");
6070
expect(commander.arg1).toEqual("arg1ENVValue");
6171
expect(commander.arg2).toEqual(4);
72+
expect(commander.arg4).toEqual("anotherArg4");
6273
done();
6374
});
6475

65-
})
76+
it("should fail in action as port is invalid", (done) => {
77+
commander.loadDefinitions(definitions);
78+
expect(()=> {
79+
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value"], {
80+
"PROGRAM_ARG_0": "arg0ENVValue",
81+
"PROGRAM_ARG_1": "arg1ENVValue",
82+
"PROGRAM_ARG_2": "hello",
83+
});
84+
}).toThrow("port is invalid");
85+
done();
86+
});
87+
});

src/cli/cli-definitions.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
"appId": {
33
env: "PARSE_SERVER_APPLICATION_ID",
44
help: "Your Parse Application ID",
@@ -11,9 +11,21 @@ module.exports = {
1111
},
1212
"serverURL": {
1313
env: "PARSE_SERVER_URL",
14-
help: "URL to your parse server with http:// or https://",
14+
help: "URL to your parse server with http:// or https://.",
1515
required: true
1616
},
17+
"port": {
18+
port: "PORT",
19+
help: "The port to run the ParseServer. defaults to 1337.",
20+
default: 1337,
21+
action: function(opt) {
22+
opt = parseInt(opt);
23+
if (!Number.isInteger(opt)) {
24+
throw new Error("The port is invalid");
25+
}
26+
return opt;
27+
}
28+
},
1729
"databaseURI": {
1830
env: "PARSE_SERVER_DATABASE_URI",
1931
help: "The full URI to your mongodb database"

src/cli/parse-server.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var path = require("path");
2-
var express = require('express');
3-
var ParseServer = require("../index").ParseServer;
4-
var definitions = require('./cli-definitions');
5-
var program = require('./utils/commander');
6-
var colors = require('colors');
1+
import path from 'path';
2+
import express from 'express';
3+
import ParseServer from '../index';
4+
import definitions from './cli-definitions';
5+
import program from './utils/commander';
6+
import colors from 'colors';
77

88
program.loadDefinitions(definitions);
99

@@ -34,10 +34,8 @@ program.on('--help', function(){
3434

3535
program.parse(process.argv, process.env);
3636

37-
var options = {};
38-
3937
if (program.args.length > 0 ) {
40-
var jsonPath = program.args[0];
38+
let jsonPath = program.args[0];
4139
jsonPath = path.resolve(jsonPath);
4240
options = require(jsonPath);
4341
console.log(`Configuation loaded from ${jsonPath}`)
@@ -51,27 +49,26 @@ if (!program.appId || !program.masterKey || !program.serverURL) {
5149
process.exit(1);
5250
}
5351

54-
var options = Object.keys(definitions).reduce(function (options, key) {
52+
let options = Object.keys(definitions).reduce(function (options, key) {
5553
if (program[key]) {
5654
options[key] = program[key];
5755
}
5856
return options;
5957
}, options);
6058

61-
var app = express();
62-
var api = new ParseServer(options);
59+
const app = express();
60+
const api = new ParseServer(options);
6361
app.use(options.mountPath, api);
6462

65-
var port = process.env.PORT || 1337;
66-
app.listen(port, function() {
63+
app.listen(options.port, function() {
6764

6865
for (let key in options) {
69-
var value = options[key];
66+
let value = options[key];
7067
if (key == "masterKey") {
7168
value = "***REDACTED***";
7269
}
7370
console.log(`${key}: ${value}`);
7471
}
7572
console.log('');
76-
console.log('parse-server running on http://localhost:'+ port + options.mountPath);
73+
console.log('parse-server running on http://localhost:'+ optins.port + options.mountPath);
7774
});

src/cli/utils/commander.js

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
var program = require('commander');
1+
import { Command } from 'commander';
22

3-
var _definitions;
4-
var _reverseDefinitions;
5-
var _defaults;
6-
program.loadDefinitions = function(definitions) {
3+
let _definitions;
4+
let _reverseDefinitions;
5+
let _defaults;
6+
7+
Command.prototype.loadDefinitions = function(definitions) {
78
_definitions = definitions;
8-
Object.keys(definitions).reduce(function(program, opt){
9+
10+
Object.keys(definitions).reduce((program, opt) => {
911
if (typeof definitions[opt] == "object") {
1012
const additionalOptions = definitions[opt];
1113
if (additionalOptions.required === true) {
@@ -14,15 +16,17 @@ program.loadDefinitions = function(definitions) {
1416
return program.option(`--${opt} [${opt}]`, additionalOptions.help, additionalOptions.action);
1517
}
1618
}
17-
return program.option(`--${opt} [${opt}]`)
18-
}, program);
19-
_defaults = Object.keys(definitions).reduce(function(defs, opt) {
19+
return program.option(`--${opt} [${opt}]`);
20+
}, this);
21+
22+
_defaults = Object.keys(definitions).reduce((defs, opt) => {
2023
if(_definitions[opt].default) {
2124
defs[opt] = _definitions[opt].default;
2225
}
2326
return defs;
2427
}, {});
25-
_reverseDefinitions = Object.keys(definitions).reduce(function(object, key){
28+
29+
_reverseDefinitions = Object.keys(definitions).reduce((object, key) => {
2630
let value = definitions[key];
2731
if (typeof value == "object") {
2832
value = value.env;
@@ -34,21 +38,21 @@ program.loadDefinitions = function(definitions) {
3438
}, {});
3539

3640
/* istanbul ignore next */
37-
program.on('--help', function(){
41+
this.on('--help', function(){
3842
console.log(' Configure From Environment:');
3943
console.log('');
40-
Object.keys(_reverseDefinitions).forEach(function(key){
44+
Object.keys(_reverseDefinitions).forEach((key) => {
4145
console.log(` $ ${key}='${_reverseDefinitions[key]}'`);
4246
});
4347
console.log('');
4448
});
4549
}
4650

47-
var envParser = function(env = {}) {
48-
return Object.keys(_reverseDefinitions).reduce(function(options, key){
51+
function parseEnvironment(env = {}) {
52+
return Object.keys(_reverseDefinitions).reduce((options, key) => {
4953
if (env[key]) {
5054
const originalKey = _reverseDefinitions[key];
51-
let action = function(option) {return option;}
55+
let action = (option) => (option);
5256
if (typeof _definitions[originalKey] === "object") {
5357
action = _definitions[originalKey].action || action;
5458
}
@@ -58,23 +62,24 @@ var envParser = function(env = {}) {
5862
}, {});
5963
}
6064

61-
program._parse = program.parse;
65+
Command.prototype.setValuesIfNeeded = function(options) {
66+
Object.keys(options).forEach((key) => {
67+
if (!this[key]) {
68+
this[key] = options[key];
69+
}
70+
});
71+
}
72+
73+
Command.prototype._parse = Command.prototype.parse;
6274

63-
program.parse = function(args, env) {
64-
program._parse(args);
75+
Command.prototype.parse = function(args, env) {
76+
this._parse(args);
6577
// Parse the environment first
66-
var envOptions = envParser(env);
78+
const envOptions = parseEnvironment(env);
79+
6780
// Load the env if not passed from command line
68-
Object.keys(envOptions).forEach(function(key){
69-
if (!program[key]) {
70-
program[key] = envOptions[key];
71-
}
72-
});
73-
Object.keys(_defaults).forEach(function(key){
74-
if (!program[key]) {
75-
program[key] = _defaults[key];
76-
}
77-
});
81+
this.setValuesIfNeeded(envOptions);
82+
this.setValuesIfNeeded(_defaults);
7883
}
7984

80-
module.exports = program;
85+
export default new Command();

0 commit comments

Comments
 (0)