From 42e9a1ff4f943b66536f2df21952cf44e2dafed8 Mon Sep 17 00:00:00 2001 From: Damian Stasik Date: Mon, 4 Oct 2021 20:22:34 +0200 Subject: [PATCH 1/2] refactor: simplify reading config from a json file --- Parse-Dashboard/index.js | 138 ++++++++++++++++++--------------------- package-lock.json | 48 ++------------ package.json | 1 - 3 files changed, 70 insertions(+), 117 deletions(-) diff --git a/Parse-Dashboard/index.js b/Parse-Dashboard/index.js index d5ed2019fc..468289478a 100644 --- a/Parse-Dashboard/index.js +++ b/Parse-Dashboard/index.js @@ -8,7 +8,7 @@ // Command line tool for npm start 'use strict' const path = require('path'); -const jsonFile = require('json-file-plus'); +const fs = require('fs'); const express = require('express'); const parseDashboard = require('./app'); const CLIHelper = require('./CLIHelper.js'); @@ -89,22 +89,20 @@ function handleSIGs(server) { if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) { if (configServerURL && configMasterKey && configAppId) { configFromCLI = { - data: { - apps: [ - { - appId: configAppId, - serverURL: configServerURL, - masterKey: configMasterKey, - appName: configAppName, - }, - ] - } + apps: [ + { + appId: configAppId, + serverURL: configServerURL, + masterKey: configMasterKey, + appName: configAppName, + }, + ] }; if (configGraphQLServerURL) { - configFromCLI.data.apps[0].graphQLServerURL = configGraphQLServerURL; + configFromCLI.apps[0].graphQLServerURL = configGraphQLServerURL; } if (configUserId && configUserPassword) { - configFromCLI.data.users = [ + configFromCLI.users = [ { user: configUserId, pass: configUserPassword, @@ -115,9 +113,7 @@ if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) { configFile = path.join(__dirname, 'parse-dashboard-config.json'); } } else if (!program.config && process.env.PARSE_DASHBOARD_CONFIG) { - configFromCLI = { - data: JSON.parse(process.env.PARSE_DASHBOARD_CONFIG) - }; + configFromCLI = JSON.parse(process.env.PARSE_DASHBOARD_CONFIG); } else { configFile = program.config; if (program.appId || program.serverURL || program.masterKey || program.appName || program.graphQLServerURL) { @@ -126,74 +122,70 @@ if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) { } } -let p = null; +let config = null; let configFilePath = null; if (configFile) { - p = jsonFile(configFile); - configFilePath = path.dirname(configFile); + try { + config = JSON.parse(fs.readFileSync(configFile, 'utf8')); + configFilePath = path.dirname(configFile); + } catch (error) { + if (error instanceof SyntaxError) { + console.log('Your config file contains invalid JSON. Exiting.'); + process.exit(1); + } else if (error.code === 'ENOENT') { + if (explicitConfigFileProvided) { + console.log('Your config file is missing. Exiting.'); + process.exit(2); + } else { + console.log('You must provide either a config file or required CLI options (app ID, Master Key, and server URL); not both.'); + process.exit(3); + } + } else { + console.log('There was a problem with your config. Exiting.'); + process.exit(-1); + } + } } else if (configFromCLI) { - p = Promise.resolve(configFromCLI); + config = configFromCLI; } else { //Failed to load default config file. console.log('You must provide either a config file or an app ID, Master Key, and server URL. See parse-dashboard --help for details.'); process.exit(4); } -p.then(config => { - config.data.apps.forEach(app => { - if (!app.appName) { - app.appName = app.appId; - } - }); - if (config.data.iconsFolder && configFilePath) { - config.data.iconsFolder = path.join(configFilePath, config.data.iconsFolder); +config.apps.forEach(app => { + if (!app.appName) { + app.appName = app.appId; } +}); - const app = express(); +if (config.iconsFolder && configFilePath) { + config.iconsFolder = path.join(configFilePath, config.iconsFolder); +} - if (allowInsecureHTTP || trustProxy || dev) app.enable('trust proxy'); +const app = express(); - config.data.trustProxy = trustProxy; - let dashboardOptions = { allowInsecureHTTP, cookieSessionSecret, dev }; - app.use(mountPath, parseDashboard(config.data, dashboardOptions)); - let server; - if(!configSSLKey || !configSSLCert){ - // Start the server. - server = app.listen(port, host, function () { - console.log(`The dashboard is now available at http://${server.address().address}:${server.address().port}${mountPath}`); - }); - } else { - // Start the server using SSL. - var fs = require('fs'); - var privateKey = fs.readFileSync(configSSLKey); - var certificate = fs.readFileSync(configSSLCert); +if (allowInsecureHTTP || trustProxy || dev) app.enable('trust proxy'); - server = require('https').createServer({ - key: privateKey, - cert: certificate - }, app).listen(port, host, function () { - console.log(`The dashboard is now available at https://${server.address().address}:${server.address().port}${mountPath}`); - }); - } - handleSIGs(server); -}, error => { - if (error instanceof SyntaxError) { - console.log('Your config file contains invalid JSON. Exiting.'); - process.exit(1); - } else if (error.code === 'ENOENT') { - if (explicitConfigFileProvided) { - console.log('Your config file is missing. Exiting.'); - process.exit(2); - } else { - console.log('You must provide either a config file or required CLI options (app ID, Master Key, and server URL); not both.'); - process.exit(3); - } - } else { - console.log('There was a problem with your config. Exiting.'); - process.exit(-1); - } -}) -.catch(error => { - console.log('There was a problem loading the dashboard. Exiting.', error); - process.exit(-1); -}); +config.trustProxy = trustProxy; +let dashboardOptions = { allowInsecureHTTP, cookieSessionSecret, dev }; +app.use(mountPath, parseDashboard(config, dashboardOptions)); +let server; +if(!configSSLKey || !configSSLCert){ + // Start the server. + server = app.listen(port, host, function () { + console.log(`The dashboard is now available at http://${server.address().address}:${server.address().port}${mountPath}`); + }); +} else { + // Start the server using SSL. + var privateKey = fs.readFileSync(configSSLKey); + var certificate = fs.readFileSync(configSSLCert); + + server = require('https').createServer({ + key: privateKey, + cert: certificate + }, app).listen(port, host, function () { + console.log(`The dashboard is now available at https://${server.address().address}:${server.address().port}${mountPath}`); + }); +} +handleSIGs(server); diff --git a/package-lock.json b/package-lock.json index 6186494f48..8df97ffee9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6782,7 +6782,8 @@ "function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", - "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==" + "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", + "dev": true }, "functional-red-black-tree": { "version": "1.0.1", @@ -7458,6 +7459,7 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", + "dev": true, "requires": { "function-bind": "^1.1.1" } @@ -7943,11 +7945,6 @@ "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" }, - "is": { - "version": "3.3.0", - "resolved": "https://registry.npmjs.org/is/-/is-3.3.0.tgz", - "integrity": "sha512-nW24QBoPcFGGHJGUwnfpI7Yc5CdqWNdsyHQszVE/z2pKHXzh7FZ5GWhJqSyaQ9wMkQnsTx+kAI8bHlCX4tKdbg==" - }, "is-accessor-descriptor": { "version": "0.1.6", "resolved": "https://registry.npmjs.org/is-accessor-descriptor/-/is-accessor-descriptor-0.1.6.tgz", @@ -8002,7 +7999,8 @@ "is-callable": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.2.4.tgz", - "integrity": "sha512-nsuwtxZfMX67Oryl9LCQ+upnC0Z0BgpwntpS89m1H/TLF0zNfzfLMV/9Wa/6MZsj0acpEjAO0KF1xT6ZdLl95w==" + "integrity": "sha512-nsuwtxZfMX67Oryl9LCQ+upnC0Z0BgpwntpS89m1H/TLF0zNfzfLMV/9Wa/6MZsj0acpEjAO0KF1xT6ZdLl95w==", + "dev": true }, "is-ci": { "version": "2.0.0", @@ -10328,16 +10326,6 @@ "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==", "dev": true }, - "json-file-plus": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/json-file-plus/-/json-file-plus-3.2.0.tgz", - "integrity": "sha1-QTYJ4kmoFHtombmVytwqGWdx3Xs=", - "requires": { - "is": "^3.1.0", - "node.extend": "^1.1.5", - "promiseback": "^2.0.2" - } - }, "json-parse-better-errors": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz", @@ -11872,15 +11860,6 @@ "@babel/parser": "^7.0.0" } }, - "node.extend": { - "version": "1.1.8", - "resolved": "https://registry.npmjs.org/node.extend/-/node.extend-1.1.8.tgz", - "integrity": "sha512-L/dvEBwyg3UowwqOUTyDsGBU6kjBQOpOhshio9V3i3BMPv5YUb9+mWNN8MK0IbWqT0AqaTSONZf0aTuMMahWgA==", - "requires": { - "has": "^1.0.3", - "is": "^3.2.1" - } - }, "nopt": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz", @@ -15022,23 +15001,6 @@ "asap": "~2.0.3" } }, - "promise-deferred": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/promise-deferred/-/promise-deferred-2.0.3.tgz", - "integrity": "sha512-n10XaoznCzLfyPFOlEE8iurezHpxrYzyjgq/1eW9Wk1gJwur/N7BdBmjJYJpqMeMcXK4wEbzo2EvZQcqjYcKUQ==", - "requires": { - "promise": "^7.3.1" - } - }, - "promiseback": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/promiseback/-/promiseback-2.0.3.tgz", - "integrity": "sha512-VZXdCwS0ppVNTIRfNsCvVwJAaP2b+pxQF7lM8DMWfmpNWyTxB6O5YNbzs+8z0ki/KIBHKHk308NTIl4kJUem3w==", - "requires": { - "is-callable": "^1.1.5", - "promise-deferred": "^2.0.3" - } - }, "prompts": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/prompts/-/prompts-2.4.1.tgz", diff --git a/package.json b/package.json index 4100a5e107..9cb1247625 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,6 @@ "immutable-devtools": "0.1.5", "inquirer": "8.1.2", "js-beautify": "1.14.0", - "json-file-plus": "3.2.0", "otpauth": "7.0.5", "package-json": "6.5.0", "parse": "3.3.1", From 8fab8f7ab0fd0d73a709fc5db9fb15ce129c9a0d Mon Sep 17 00:00:00 2001 From: Damian Stasik Date: Tue, 5 Oct 2021 18:09:03 +0200 Subject: [PATCH 2/2] refactor: restore `data` nesting of config object --- Parse-Dashboard/index.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/Parse-Dashboard/index.js b/Parse-Dashboard/index.js index 468289478a..2363f685b9 100644 --- a/Parse-Dashboard/index.js +++ b/Parse-Dashboard/index.js @@ -89,20 +89,22 @@ function handleSIGs(server) { if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) { if (configServerURL && configMasterKey && configAppId) { configFromCLI = { - apps: [ - { - appId: configAppId, - serverURL: configServerURL, - masterKey: configMasterKey, - appName: configAppName, - }, - ] + data: { + apps: [ + { + appId: configAppId, + serverURL: configServerURL, + masterKey: configMasterKey, + appName: configAppName, + }, + ] + } }; if (configGraphQLServerURL) { - configFromCLI.apps[0].graphQLServerURL = configGraphQLServerURL; + configFromCLI.data.apps[0].graphQLServerURL = configGraphQLServerURL; } if (configUserId && configUserPassword) { - configFromCLI.users = [ + configFromCLI.data.users = [ { user: configUserId, pass: configUserPassword, @@ -113,7 +115,9 @@ if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) { configFile = path.join(__dirname, 'parse-dashboard-config.json'); } } else if (!program.config && process.env.PARSE_DASHBOARD_CONFIG) { - configFromCLI = JSON.parse(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) { @@ -126,7 +130,9 @@ let config = null; let configFilePath = null; if (configFile) { try { - config = JSON.parse(fs.readFileSync(configFile, 'utf8')); + config = { + data: JSON.parse(fs.readFileSync(configFile, 'utf8')) + }; configFilePath = path.dirname(configFile); } catch (error) { if (error instanceof SyntaxError) { @@ -153,23 +159,23 @@ if (configFile) { process.exit(4); } -config.apps.forEach(app => { +config.data.apps.forEach(app => { if (!app.appName) { app.appName = app.appId; } }); -if (config.iconsFolder && configFilePath) { - config.iconsFolder = path.join(configFilePath, config.iconsFolder); +if (config.data.iconsFolder && configFilePath) { + config.data.iconsFolder = path.join(configFilePath, config.data.iconsFolder); } const app = express(); if (allowInsecureHTTP || trustProxy || dev) app.enable('trust proxy'); -config.trustProxy = trustProxy; +config.data.trustProxy = trustProxy; let dashboardOptions = { allowInsecureHTTP, cookieSessionSecret, dev }; -app.use(mountPath, parseDashboard(config, dashboardOptions)); +app.use(mountPath, parseDashboard(config.data, dashboardOptions)); let server; if(!configSSLKey || !configSSLCert){ // Start the server.