Skip to content

Commit

Permalink
fix: config options like --port or --config are ignored (#2113)
Browse files Browse the repository at this point in the history
  • Loading branch information
bugra9 authored May 7, 2022
1 parent 728d114 commit 6d70d8a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
45 changes: 23 additions & 22 deletions Parse-Dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,41 @@ program.option('--createUser', 'helper tool to allow you to generate secure user
program.option('--createMFA', 'helper tool to allow you to generate multi-factor authentication secrets.');

program.parse(process.argv);
const options = program.opts();

for (const key in program) {
for (const key in options) {
const func = CLIHelper[key];
if (func && typeof func === 'function') {
func();
return;
}
}

const host = program.host || process.env.HOST || '0.0.0.0';
const port = program.port || process.env.PORT || 4040;
const mountPath = program.mountPath || process.env.MOUNT_PATH || '/';
const allowInsecureHTTP = program.allowInsecureHTTP || process.env.PARSE_DASHBOARD_ALLOW_INSECURE_HTTP;
const cookieSessionSecret = program.cookieSessionSecret || process.env.PARSE_DASHBOARD_COOKIE_SESSION_SECRET;
const trustProxy = program.trustProxy || process.env.PARSE_DASHBOARD_TRUST_PROXY;
const dev = program.dev;
const host = options.host || process.env.HOST || '0.0.0.0';
const port = options.port || process.env.PORT || 4040;
const mountPath = options.mountPath || process.env.MOUNT_PATH || '/';
const allowInsecureHTTP = options.allowInsecureHTTP || process.env.PARSE_DASHBOARD_ALLOW_INSECURE_HTTP;
const cookieSessionSecret = options.cookieSessionSecret || process.env.PARSE_DASHBOARD_COOKIE_SESSION_SECRET;
const trustProxy = options.trustProxy || process.env.PARSE_DASHBOARD_TRUST_PROXY;
const dev = options.dev;

if (trustProxy && allowInsecureHTTP) {
console.log('Set only trustProxy *or* allowInsecureHTTP, not both. Only one is needed to handle being behind a proxy.');
process.exit(-1);
}

let explicitConfigFileProvided = !!program.config;
let explicitConfigFileProvided = !!options.config;
let configFile = null;
let configFromCLI = null;
let configServerURL = program.serverURL || process.env.PARSE_DASHBOARD_SERVER_URL;
let configGraphQLServerURL = program.graphQLServerURL || process.env.PARSE_DASHBOARD_GRAPHQL_SERVER_URL;
let configMasterKey = program.masterKey || process.env.PARSE_DASHBOARD_MASTER_KEY;
let configAppId = program.appId || process.env.PARSE_DASHBOARD_APP_ID;
let configAppName = program.appName || process.env.PARSE_DASHBOARD_APP_NAME;
let configUserId = program.userId || process.env.PARSE_DASHBOARD_USER_ID;
let configUserPassword = program.userPassword || process.env.PARSE_DASHBOARD_USER_PASSWORD;
let configSSLKey = program.sslKey || process.env.PARSE_DASHBOARD_SSL_KEY;
let configSSLCert = program.sslCert || process.env.PARSE_DASHBOARD_SSL_CERT;
let configServerURL = options.serverURL || process.env.PARSE_DASHBOARD_SERVER_URL;
let configGraphQLServerURL = options.graphQLServerURL || process.env.PARSE_DASHBOARD_GRAPHQL_SERVER_URL;
let configMasterKey = options.masterKey || process.env.PARSE_DASHBOARD_MASTER_KEY;
let configAppId = options.appId || process.env.PARSE_DASHBOARD_APP_ID;
let configAppName = options.appName || process.env.PARSE_DASHBOARD_APP_NAME;
let configUserId = options.userId || process.env.PARSE_DASHBOARD_USER_ID;
let configUserPassword = options.userPassword || process.env.PARSE_DASHBOARD_USER_PASSWORD;
let configSSLKey = options.sslKey || process.env.PARSE_DASHBOARD_SSL_KEY;
let configSSLCert = options.sslCert || process.env.PARSE_DASHBOARD_SSL_CERT;

function handleSIGs(server) {
const signals = {
Expand All @@ -86,7 +87,7 @@ function handleSIGs(server) {
});
}

if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) {
if (!options.config && !process.env.PARSE_DASHBOARD_CONFIG) {
if (configServerURL && configMasterKey && configAppId) {
configFromCLI = {
data: {
Expand Down Expand Up @@ -114,13 +115,13 @@ if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) {
} else if (!configServerURL && !configMasterKey && !configAppName) {
configFile = path.join(__dirname, 'parse-dashboard-config.json');
}
} else if (!program.config && process.env.PARSE_DASHBOARD_CONFIG) {
} else if (!options.config && process.env.PARSE_DASHBOARD_CONFIG) {
configFromCLI = {
data: JSON.parse(process.env.PARSE_DASHBOARD_CONFIG)
};
} else {
configFile = program.config;
if (program.appId || program.serverURL || program.masterKey || program.appName || program.graphQLServerURL) {
configFile = options.config;
if (options.appId || options.serverURL || options.masterKey || options.appName || options.graphQLServerURL) {
console.log('You must provide either a config file or other CLI options (appName, appId, masterKey, serverURL, and graphQLServerURL); not both.');
process.exit(3);
}
Expand Down
50 changes: 50 additions & 0 deletions src/lib/tests/e2e/dashboard.e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
jest.disableAutomock();

const express = require('express');
const path = require('path');
const spawn = require('child_process').spawn;
const ParseDashboard = require('../../../../Parse-Dashboard/app');
const puppeteer = require('puppeteer');

Expand Down Expand Up @@ -46,3 +48,51 @@ describe('dashboard e2e', () => {
server.close();
});
});


describe('Config options', () => {
it('should start with port option', async () => {
const result = await startParseDashboardAndGetOutput(['--port', '4041']);
expect(result).toContain('The dashboard is now available at http://0.0.0.0:4041/');
});

it('should reject to start if config and other options are combined', async () => {
const args = [
'--appId',
'--serverURL',
'--masterKey',
'--appName',
'--graphQLServerURL'
];

for (const arg of args) {
const result = await startParseDashboardAndGetOutput(['--config', 'helloworld', arg, 'helloworld']);
expect(result).toContain('You must provide either a config file or other CLI options (appName, appId, masterKey, serverURL, and graphQLServerURL); not both.');
}
});
});

function startParseDashboardAndGetOutput(args) {
const timeoutInMs = 1000;
return new Promise((resolve) => {
const indexFilePath = path.resolve('./Parse-Dashboard/index.js');
const child = spawn('node', [indexFilePath, ...args], { cwd: '.', timeout: timeoutInMs });
setTimeout(() => { child.kill(); }, timeoutInMs); // node.js 12 hack (spawn timeout option is not supported.)

let output = '';
child.on('error', () => { resolve(output); });
child.on('close', () => { resolve(output); });

if (child.stdout) {
child.stdout.on('data', data => {
output += `STDOUT: ${data}\n`;
});
}

if (child.stderr) {
child.stderr.on('data', data => {
output += `STDERROR: ${data}\n`;
});
}
});
}

0 comments on commit 6d70d8a

Please sign in to comment.