Skip to content

Commit 7e52c18

Browse files
committed
Merge pull request #1363 from ParsePlatform/issue/1319
Improves config loading and tests
2 parents 27dd2eb + 8c92b80 commit 7e52c18

8 files changed

+155
-39
lines changed

spec/CLI.spec.js

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
var commander = require("../src/cli/utils/commander").default;
23

34
var definitions = {
@@ -11,7 +12,7 @@ var definitions = {
1112
action: function(value) {
1213
var value = parseInt(value);
1314
if (!Number.isInteger(value)) {
14-
throw "port is invalid";
15+
throw "arg2 is invalid";
1516
}
1617
return value;
1718
}
@@ -23,7 +24,7 @@ var definitions = {
2324
}
2425

2526
describe("commander additions", () => {
26-
27+
2728
afterEach((done) => {
2829
commander.options = [];
2930
delete commander.arg0;
@@ -33,7 +34,7 @@ describe("commander additions", () => {
3334
delete commander.arg4;
3435
done();
3536
})
36-
37+
3738
it("should load properly definitions from args", (done) => {
3839
commander.loadDefinitions(definitions);
3940
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "--arg1", "arg1Value", "--arg2", "2", "--arg3", "some"]);
@@ -44,7 +45,7 @@ describe("commander additions", () => {
4445
expect(commander.arg4).toEqual("arg4Value");
4546
done();
4647
});
47-
48+
4849
it("should load properly definitions from env", (done) => {
4950
commander.loadDefinitions(definitions);
5051
commander.parse([], {
@@ -58,7 +59,7 @@ describe("commander additions", () => {
5859
expect(commander.arg4).toEqual("arg4Value");
5960
done();
6061
});
61-
62+
6263
it("should load properly use args over env", (done) => {
6364
commander.loadDefinitions(definitions);
6465
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "--arg4", "anotherArg4"], {
@@ -72,7 +73,7 @@ describe("commander additions", () => {
7273
expect(commander.arg4).toEqual("anotherArg4");
7374
done();
7475
});
75-
76+
7677
it("should fail in action as port is invalid", (done) => {
7778
commander.loadDefinitions(definitions);
7879
expect(()=> {
@@ -81,7 +82,58 @@ describe("commander additions", () => {
8182
"PROGRAM_ARG_1": "arg1ENVValue",
8283
"PROGRAM_ARG_2": "hello",
8384
});
84-
}).toThrow("port is invalid");
85+
}).toThrow("arg2 is invalid");
86+
done();
87+
});
88+
89+
it("should not override config.json", (done) => {
90+
commander.loadDefinitions(definitions);
91+
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "./spec/configs/CLIConfig.json"], {
92+
"PROGRAM_ARG_0": "arg0ENVValue",
93+
"PROGRAM_ARG_1": "arg1ENVValue",
94+
});
95+
let options = commander.getOptions();
96+
expect(options.arg2).toBe(8888);
97+
expect(options.arg3).toBe("hello"); //config value
98+
expect(options.arg4).toBe('/1');
99+
done();
100+
});
101+
102+
it("should fail with invalid values in JSON", (done) => {
103+
commander.loadDefinitions(definitions);
104+
expect(() => {
105+
commander.parse(["node","./CLI.spec.js","--arg0", "arg0Value", "./spec/configs/CLIConfigFail.json"], {
106+
"PROGRAM_ARG_0": "arg0ENVValue",
107+
"PROGRAM_ARG_1": "arg1ENVValue",
108+
});
109+
}).toThrow("arg2 is invalid")
110+
done();
111+
});
112+
113+
it("should fail when too many apps are set", (done) => {
114+
commander.loadDefinitions(definitions);
115+
expect(() => {
116+
commander.parse(["node","./CLI.spec.js","./spec/configs/CLIConfigFailTooManyApps.json"]);
117+
}).toThrow("Multiple apps are not supported")
118+
done();
119+
});
120+
121+
it("should load config from apps", (done) => {
122+
commander.loadDefinitions(definitions);
123+
commander.parse(["node", "./CLI.spec.js", "./spec/configs/CLIConfigApps.json"]);
124+
let options = commander.getOptions();
125+
expect(options.arg1).toBe("my_app");
126+
expect(options.arg2).toBe(8888);
127+
expect(options.arg3).toBe("hello"); //config value
128+
expect(options.arg4).toBe('/1');
129+
done();
130+
});
131+
132+
it("should fail when passing an invalid arguement", (done) => {
133+
commander.loadDefinitions(definitions);
134+
expect(() => {
135+
commander.parse(["node", "./CLI.spec.js", "./spec/configs/CLIConfigUnknownArg.json"]);
136+
}).toThrow('error: unknown option myArg')
85137
done();
86138
});
87-
});
139+
});

spec/configs/CLIConfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arg1": "my_app",
3+
"arg2": "8888",
4+
"arg3": "hello",
5+
"arg4": "/1"
6+
}

spec/configs/CLIConfigApps.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"apps": [
3+
{
4+
"arg1": "my_app",
5+
"arg2": 8888,
6+
"arg3": "hello",
7+
"arg4": "/1"
8+
}]
9+
}

spec/configs/CLIConfigFail.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arg1": "my_app",
3+
"arg2": "hello",
4+
"arg3": "hello",
5+
"arg4": "/1"
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"apps": [
3+
{
4+
"arg1": "my_app",
5+
"arg2": "99999",
6+
"arg3": "hello",
7+
"arg4": "/1"
8+
},
9+
{
10+
"arg1": "my_app2",
11+
"arg2": "9999",
12+
"arg3": "hello",
13+
"arg4": "/1"
14+
}
15+
]
16+
}

spec/configs/CLIConfigUnknownArg.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"arg1": "my_app",
3+
"arg2": "8888",
4+
"arg3": "hello",
5+
"myArg": "/1"
6+
}

src/cli/parse-server.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import express from 'express';
33
import { ParseServer } from '../index';
44
import definitions from './cli-definitions';
55
import program from './utils/commander';
6+
import { mergeWithOptions } from './utils/commander';
67
import colors from 'colors';
78

89
program.loadDefinitions(definitions);
@@ -34,28 +35,7 @@ program.on('--help', function(){
3435

3536
program.parse(process.argv, process.env);
3637

37-
let options = {};
38-
if (program.args.length > 0 ) {
39-
let jsonPath = program.args[0];
40-
jsonPath = path.resolve(jsonPath);
41-
let jsonConfig = require(jsonPath);
42-
if (jsonConfig.apps) {
43-
if (jsonConfig.apps.length > 1) {
44-
throw 'Multiple apps are not supported';
45-
}
46-
options = jsonConfig.apps[0];
47-
} else {
48-
options = jsonConfig;
49-
}
50-
console.log(`Configuation loaded from ${jsonPath}`)
51-
}
52-
53-
options = Object.keys(definitions).reduce(function (options, key) {
54-
if (typeof program[key] !== 'undefined') {
55-
options[key] = program[key];
56-
}
57-
return options;
58-
}, options);
38+
let options = program.getOptions();
5939

6040
if (!options.serverURL) {
6141
options.serverURL = `http://localhost:${options.port}${options.mountPath}`;

src/cli/utils/commander.js

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Command } from 'commander';
2-
2+
import path from 'path';
33
let _definitions;
44
let _reverseDefinitions;
55
let _defaults;
66

77
Command.prototype.loadDefinitions = function(definitions) {
88
_definitions = definitions;
9-
9+
1010
Object.keys(definitions).reduce((program, opt) => {
1111
if (typeof definitions[opt] == "object") {
1212
const additionalOptions = definitions[opt];
@@ -18,14 +18,14 @@ Command.prototype.loadDefinitions = function(definitions) {
1818
}
1919
return program.option(`--${opt} [${opt}]`);
2020
}, this);
21-
21+
2222
_defaults = Object.keys(definitions).reduce((defs, opt) => {
2323
if(_definitions[opt].default) {
2424
defs[opt] = _definitions[opt].default;
2525
}
2626
return defs;
2727
}, {});
28-
28+
2929
_reverseDefinitions = Object.keys(definitions).reduce((object, key) => {
3030
let value = definitions[key];
3131
if (typeof value == "object") {
@@ -36,7 +36,7 @@ Command.prototype.loadDefinitions = function(definitions) {
3636
}
3737
return object;
3838
}, {});
39-
39+
4040
/* istanbul ignore next */
4141
this.on('--help', function(){
4242
console.log(' Configure From Environment:');
@@ -58,28 +58,69 @@ function parseEnvironment(env = {}) {
5858
}
5959
options[_reverseDefinitions[key]] = action(env[key]);
6060
}
61-
return options;
61+
return options;
6262
}, {});
6363
}
6464

65+
function parseConfigFile(program) {
66+
let options = {};
67+
if (program.args.length > 0) {
68+
let jsonPath = program.args[0];
69+
jsonPath = path.resolve(jsonPath);
70+
let jsonConfig = require(jsonPath);
71+
if (jsonConfig.apps) {
72+
if (jsonConfig.apps.length > 1) {
73+
throw 'Multiple apps are not supported';
74+
}
75+
options = jsonConfig.apps[0];
76+
} else {
77+
options = jsonConfig;
78+
}
79+
Object.keys(options).forEach((key) => {
80+
let value = options[key];
81+
if (!_definitions[key]) {
82+
throw `error: unknown option ${key}`;
83+
}
84+
let action = _definitions[key].action;
85+
if (action) {
86+
options[key] = action(value);
87+
}
88+
})
89+
console.log(`Configuation loaded from ${jsonPath}`)
90+
}
91+
return options;
92+
}
93+
6594
Command.prototype.setValuesIfNeeded = function(options) {
6695
Object.keys(options).forEach((key) => {
6796
if (!this[key]) {
6897
this[key] = options[key];
69-
}
98+
}
7099
});
71-
}
100+
}
72101

73102
Command.prototype._parse = Command.prototype.parse;
74103

75104
Command.prototype.parse = function(args, env) {
76105
this._parse(args);
77106
// Parse the environment first
78107
const envOptions = parseEnvironment(env);
79-
108+
const fromFile = parseConfigFile(this);
80109
// Load the env if not passed from command line
81110
this.setValuesIfNeeded(envOptions);
111+
// Load from file to override
112+
this.setValuesIfNeeded(fromFile);
113+
// Last set the defaults
82114
this.setValuesIfNeeded(_defaults);
83115
}
84116

117+
Command.prototype.getOptions = function() {
118+
return Object.keys(_definitions).reduce((options, key) => {
119+
if (typeof this[key] !== 'undefined') {
120+
options[key] = this[key];
121+
}
122+
return options;
123+
}, {});
124+
}
125+
85126
export default new Command();

0 commit comments

Comments
 (0)