Skip to content

Commit ab425d8

Browse files
committed
New CLI for parse-server
1 parent 7bbe166 commit ab425d8

File tree

5 files changed

+233
-46
lines changed

5 files changed

+233
-46
lines changed

bin/parse-server

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,2 @@
11
#!/usr/bin/env node
2-
var express = require('express');
3-
var ParseServer = require("../lib/index").ParseServer;
4-
5-
var app = express();
6-
7-
var options = {};
8-
if (process.env.PARSE_SERVER_OPTIONS) {
9-
10-
options = JSON.parse(process.env.PARSE_SERVER_OPTIONS);
11-
12-
} else {
13-
14-
options.databaseURI = process.env.PARSE_SERVER_DATABASE_URI;
15-
options.cloud = process.env.PARSE_SERVER_CLOUD_CODE_MAIN;
16-
options.collectionPrefix = process.env.PARSE_SERVER_COLLECTION_PREFIX;
17-
18-
// Keys and App ID
19-
options.appId = process.env.PARSE_SERVER_APPLICATION_ID;
20-
options.clientKey = process.env.PARSE_SERVER_CLIENT_KEY;
21-
options.restAPIKey = process.env.PARSE_SERVER_REST_API_KEY;
22-
options.dotNetKey = process.env.PARSE_SERVER_DOTNET_KEY;
23-
options.javascriptKey = process.env.PARSE_SERVER_JAVASCRIPT_KEY;
24-
options.masterKey = process.env.PARSE_SERVER_MASTER_KEY;
25-
options.fileKey = process.env.PARSE_SERVER_FILE_KEY;
26-
// Comma separated list of facebook app ids
27-
var facebookAppIds = process.env.PARSE_SERVER_FACEBOOK_APP_IDS;
28-
29-
if (facebookAppIds) {
30-
facebookAppIds = facebookAppIds.split(",");
31-
options.facebookAppIds = facebookAppIds;
32-
}
33-
34-
var oauth = process.env.PARSE_SERVER_OAUTH_PROVIDERS;
35-
if (oauth) {
36-
options.oauth = JSON.parse(oauth);
37-
};
38-
}
39-
40-
var mountPath = process.env.PARSE_SERVER_MOUNT_PATH || "/";
41-
var api = new ParseServer(options);
42-
app.use(mountPath, api);
43-
44-
var port = process.env.PORT || 1337;
45-
app.listen(port, function() {
46-
console.log('parse-server-example running on http://localhost:'+ port + mountPath);
47-
});
2+
require("../lib/cli/parse-server");

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"babel-runtime": "^6.5.0",
2323
"bcrypt-nodejs": "0.0.3",
2424
"body-parser": "^1.14.2",
25+
"commander": "^2.9.0",
2526
"deepcopy": "^0.6.1",
2627
"express": "^4.13.4",
2728
"mime": "^1.3.4",

src/cli/cli-definitions.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
module.exports = {
2+
"appId": {
3+
env: "PARSE_SERVER_APPLICATION_ID",
4+
help: "Your Parse Application ID",
5+
required: true
6+
},
7+
"masterKey": {
8+
env: "PARSE_SERVER_MASTER_KEY",
9+
help: "Your Parse Master Key",
10+
required: true
11+
},
12+
"serverURL": {
13+
env: "PARSE_SERVER_URL",
14+
help: "URL to your parse server with http:// or https://"
15+
},
16+
"databaseURI": {
17+
env: "PARSE_SERVER_DATABASE_URI",
18+
help: "The full URI to your mongodb database"
19+
},
20+
"clientKey": {
21+
env: "PARSE_SERVER_CLIENT_KEY",
22+
help: "Key for iOS, MacOS, tvOS clients"
23+
},
24+
"javascriptKey": {
25+
env: "PARSE_SERVER_JAVASCRIPT_KEY",
26+
help: "Key for the Javascript SDK"
27+
},
28+
"restAPIKey": {
29+
env: "PARSE_SERVER_REST_API_KEY",
30+
help: "Key for REST calls"
31+
},
32+
"dotNetKey": {
33+
env: "PARSE_SERVER_DOT_NET_KEY",
34+
help: "Key for Unity and .Net SDK"
35+
},
36+
"cloud": {
37+
env: "PARSE_SERVER_CLOUD_CODE_MAIN",
38+
help: "Full path to your cloud code main.js"
39+
},
40+
"push": {
41+
env: "PARSE_SERVER_PUSH",
42+
help: "Configuration for push, as stringified JSON. See https://github.com/ParsePlatform/parse-server/wiki/Push",
43+
action: function(opt) {
44+
return JSON.parse(opt)
45+
}
46+
},
47+
"oauth": {
48+
env: "PARSE_SERVER_OAUTH_PROVIDERS",
49+
help: "Configuration for your oAuth providers, as stringified JSON. See https://github.com/ParsePlatform/parse-server/wiki/Parse-Server-Guide#oauth",
50+
action: function(opt) {
51+
return JSON.parse(opt)
52+
}
53+
},
54+
"fileKey": {
55+
env: "PARSE_SERVER_FILE_KEY",
56+
help: "Key for your files",
57+
},
58+
"facebookAppIds": {
59+
env: "PARSE_SERVER_FACEBOOK_APP_IDS",
60+
help: "Comma separated list for your facebook app Ids",
61+
type: "list",
62+
action: function(opt) {
63+
return opt.split(",")
64+
}
65+
},
66+
"enableAnonymousUsers": {
67+
env: "PARSE_SERVER_ENABLE_ANON_USERS",
68+
help: "Enable (or disable) anon users, enabled by default",
69+
action: function(opt) {
70+
if (opt == "true" || opt == "1") {
71+
return true;
72+
}
73+
return false;
74+
}
75+
},
76+
"mountPath": {
77+
env: "PARSE_SERVER_MOUNT_PATH",
78+
help: "Mount path for the server, defaults to /"
79+
},
80+
"databaseAdapter": {
81+
env: "PARSE_SERVER_DATABASE_ADAPTER",
82+
help: "Adapter module for the database sub-system"
83+
},
84+
"filesAdapter": {
85+
env: "PARSE_SERVER_FILES_ADAPTER",
86+
help: "Adapter module for the files sub-system"
87+
},
88+
"loggerAdapter": {
89+
env: "PARSE_SERVER_LOGGER_ADAPTER",
90+
help: "Adapter module for the logging sub-system"
91+
}
92+
};

src/cli/parse-server.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
7+
program.loadDefinitions(definitions);
8+
9+
program
10+
.usage('[options] <path/to/configuration.json>');
11+
12+
program.on('--help', function(){
13+
console.log(' Get Started guide:');
14+
console.log('');
15+
console.log(' Please have a look at the get started guide!')
16+
console.log(' https://github.com/ParsePlatform/parse-server/wiki/Parse-Server-Guide');
17+
console.log('');
18+
console.log('');
19+
console.log(' Usage with npm start');
20+
console.log('');
21+
console.log(' $ npm start -- path/to/config.json');
22+
console.log(' $ npm start -- --appId APP_ID --masterKey MASTER_KEY');
23+
console.log(' $ npm start -- --appId APP_ID --masterKey MASTER_KEY');
24+
console.log('');
25+
console.log('');
26+
console.log(' Usage:');
27+
console.log('');
28+
console.log(' $ parse-server path/to/config.json');
29+
console.log(' $ parse-server -- --appId APP_ID --masterKey MASTER_KEY');
30+
console.log(' $ parse-server -- --appId APP_ID --masterKey MASTER_KEY');
31+
console.log('');
32+
});
33+
34+
program.parse(process.argv, process.env);
35+
36+
var options = {};
37+
38+
if (program.args.length > 0 ) {
39+
var jsonPath = program.args[0];
40+
jsonPath = path.resolve(jsonPath);
41+
options = require(jsonPath);
42+
console.log(`Configuation loaded from ${jsonPath}`)
43+
}
44+
45+
var options = Object.keys(definitions).reduce(function (options, key) {
46+
if (program[key]) {
47+
options[key] = program[key];
48+
}
49+
return options;
50+
}, options);
51+
52+
options.mountPath = options.mountPath || '/';
53+
54+
var app = express();
55+
var api = new ParseServer(options);
56+
app.use(options.mountPath, api);
57+
58+
var port = process.env.PORT || 1337;
59+
app.listen(port, function() {
60+
61+
for (let key in options) {
62+
var value = options[key];
63+
if (key == "masterKey") {
64+
value = "***REDACTED***";
65+
}
66+
console.log(`${key}: ${value}`);
67+
}
68+
console.log('');
69+
console.log('parse-server running on http://localhost:'+ port + options.mountPath);
70+
});

src/cli/utils/commander.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
var program = require('commander');
2+
3+
var _definitions;
4+
var _reverseDefinitions;
5+
6+
program.loadDefinitions = function(definitions, prefix = "") {
7+
_definitions = definitions;
8+
Object.keys(definitions).reduce(function(program, opt){
9+
if (typeof definitions[opt] == "object") {
10+
const additionalOptions = definitions[opt];
11+
if (additionalOptions.required === true) {
12+
return program.option(`--${opt} <${opt}>`, additionalOptions.help, additionalOptions.action);
13+
} else {
14+
return program.option(`--${opt} [${opt}]`, additionalOptions.help, additionalOptions.action);
15+
}
16+
}
17+
return program.option(`--${opt} [${opt}]`)
18+
}, program);
19+
20+
_reverseDefinitions = Object.keys(definitions).reduce(function(object, key){
21+
let value = definitions[key];
22+
if (typeof value == "object") {
23+
value = value.env;
24+
}
25+
if (value) {
26+
object[value] = key;
27+
}
28+
return object;
29+
}, {});
30+
31+
program.on('--help', function(){
32+
console.log(' Configure From Environment:');
33+
console.log('');
34+
Object.keys(_reverseDefinitions).forEach(function(key){
35+
console.log(` $ ${key}='${_reverseDefinitions[key]}'`);
36+
});
37+
console.log('');
38+
});
39+
}
40+
41+
var envParser = function(env = {}) {
42+
return Object.keys(_reverseDefinitions).reduce(function(options, key){
43+
if (env[key]) {
44+
const originalKey = _reverseDefinitions[key];
45+
let action = function(option) {return option;}
46+
if (typeof _definitions[originalKey] === "object") {
47+
action = _definitions[originalKey].action || action;
48+
}
49+
options[_reverseDefinitions[key]] = action(env[key]);
50+
}
51+
return options;
52+
}, {});
53+
}
54+
55+
program._parse = program.parse;
56+
57+
program.parse = function(args, env) {
58+
program._parse(args);
59+
// Parse the environment first
60+
var envOptions = envParser(env);
61+
// Load the env if not passed from command line
62+
Object.keys(envOptions).forEach(function(key){
63+
if (!program[key]) {
64+
program[key] = envOptions[key];
65+
}
66+
});
67+
}
68+
69+
module.exports = program;

0 commit comments

Comments
 (0)