Skip to content

Adds ability to pass json file path to parse-server script #289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 1 addition & 28 deletions bin/parse-server
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,7 @@ var ParseServer = require("../index").ParseServer;

var app = express();

var options = {};
if (process.env.PARSE_SERVER_OPTIONS) {

options = JSON.parse(process.env.PARSE_SERVER_OPTIONS);

} else {

options.databaseURI = process.env.PARSE_SERVER_DATABASE_URI;
options.cloud = process.env.PARSE_SERVER_CLOUD_CODE_MAIN;
options.collectionPrefix = process.env.PARSE_SERVER_COLLECTION_PREFIX;

// Keys and App ID
options.appId = process.env.PARSE_SERVER_APPLICATION_ID;
options.clientKey = process.env.PARSE_SERVER_CLIENT_KEY;
options.restAPIKey = process.env.PARSE_SERVER_REST_API_KEY;
options.dotNetKey = process.env.PARSE_SERVER_DOTNET_KEY;
options.javascriptKey = process.env.PARSE_SERVER_JAVASCRIPT_KEY;
options.dotNetKey = process.env.PARSE_SERVER_DOTNET_KEY;
options.masterKey = process.env.PARSE_SERVER_MASTER_KEY;
options.fileKey = process.env.PARSE_SERVER_FILE_KEY;
// Comma separated list of facebook app ids
var facebookAppIds = process.env.PARSE_SERVER_FACEBOOK_APP_IDS;

if (facebookAppIds) {
facebookAppIds = facebookAppIds.split(",");
options.facebookAppIds = facebookAppIds;
}
}
var options = require("../cli")();

var mountPath = process.env.PARSE_SERVER_MOUNT_PATH || "/";
var api = new ParseServer(options);
Expand Down
58 changes: 58 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var path = require("path");
function loadFromCommandLine(args) {
args = args || [];
while (args.length > 0) {
if (args[0] == "--config") {
if (args.length < 2) {
throw "Please specify the configuration file (json)";
}
return require(path.resolve(args[1]));
}
args = args.slice(1, args.length);
}
}

function loadFromEnvironment(env) {
env = env || {};
var options = {};
if (env.PARSE_SERVER_OPTIONS) {

options = JSON.parse(env.PARSE_SERVER_OPTIONS);

} else {

options.databaseURI = env.PARSE_SERVER_DATABASE_URI;
options.cloud = env.PARSE_SERVER_CLOUD_CODE_MAIN;
options.collectionPrefix = env.PARSE_SERVER_COLLECTION_PREFIX;

// Keys and App ID
options.appId = env.PARSE_SERVER_APPLICATION_ID;
options.clientKey = env.PARSE_SERVER_CLIENT_KEY;
options.restAPIKey = env.PARSE_SERVER_REST_API_KEY;
options.dotNetKey = env.PARSE_SERVER_DOTNET_KEY;
options.javascriptKey = env.PARSE_SERVER_JAVASCRIPT_KEY;
options.dotNetKey = env.PARSE_SERVER_DOTNET_KEY;
options.masterKey = env.PARSE_SERVER_MASTER_KEY;
options.fileKey = env.PARSE_SERVER_FILE_KEY;
// Comma separated list of facebook app ids
var facebookAppIds = env.PARSE_SERVER_FACEBOOK_APP_IDS;

if (facebookAppIds) {
facebookAppIds = facebookAppIds.split(",");
options.facebookAppIds = facebookAppIds;
}
}
return options;
}


module.exports = function() {
var options = loadFromCommandLine(process.argv);
if (typeof options == "undefined") {
options = loadFromEnvironment(process.env);
}
return options;
}

module.exports.loadFromEnvironment = loadFromEnvironment;
module.exports.loadFromCommandLine = loadFromCommandLine;
85 changes: 85 additions & 0 deletions spec/CLI.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
var configuration = require("./config.json");
var Parse = require("parse/node");
var apps = configuration.applications;
var configLoader = require("../cli");

describe('CLI configuration loading', () => {
it('should load a JSON from arguments', done => {
var config = configLoader.loadFromCommandLine(["--config", "./spec/config.json"]);
expect(config).not.toBe(undefined);
expect(config.appId).toBe("123456");
done();
});

it('should throw when json does not exist', done => {
function load() {
return configLoader.loadFromCommandLine(["--config", "./spec/bar.json"]);
}
expect(load).toThrow();
done();
});

it('should throw when json is missing', done => {
function load() {
return configLoader.loadFromCommandLine(["--config"]);
}
expect(load).toThrow("Please specify the configuration file (json)");
done();
});

it('should retun nothing when nothing is specified', done => {
var config = configLoader.loadFromCommandLine();
expect(config).toBe(undefined);
done();
});

it('should support more arguments', done => {
var config = configLoader.loadFromCommandLine(["--some","--config", "./spec/config.json", "--other"]);
expect(config).not.toBe(undefined);
expect(config.appId).toBe("123456");
done();
});

it('should load from environment', done => {
var env = {
PARSE_SERVER_DATABASE_URI: "",
PARSE_SERVER_CLOUD_CODE_MAIN: "",
PARSE_SERVER_COLLECTION_PREFIX: "",
PARSE_SERVER_APPLICATION_ID: "",
PARSE_SERVER_CLIENT_KEY: "",
PARSE_SERVER_REST_API_KEY: "",
PARSE_SERVER_DOTNET_KEY: "",
PARSE_SERVER_JAVASCRIPT_KEY: "",
PARSE_SERVER_DOTNET_KEY: "",
PARSE_SERVER_MASTER_KEY: "",
PARSE_SERVER_FILE_KEY: "",
PARSE_SERVER_FACEBOOK_APP_IDS: "hello,world"
}

var config = configLoader.loadFromEnvironment(env);
expect(config).not.toBe(undefined);
expect(Object.keys(config).length).toBe(Object.keys(env).length);
expect(config.facebookAppIds.length).toBe(2);
expect(config.facebookAppIds).toContain("hello");
expect(config.facebookAppIds).toContain("world");
done();
});

it('should load from environment options', done => {
var env = {
PARSE_SERVER_OPTIONS: require("fs").readFileSync("./spec/config.json")
}

var config = configLoader.loadFromEnvironment(env);
expect(config).not.toBe(undefined);
expect(config.appId).toBe("123456");
done();
});
it('should load empty configuration options', done => {
var config = configLoader();
expect(config).not.toBe(undefined);
expect(config).not.toBe({});
expect(config.appId).toBe(undefined);
done();
});
});
4 changes: 4 additions & 0 deletions spec/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"appId": "123456",
"masterKey": "masterKey"
}