Skip to content

Commit 921712a

Browse files
refactor: simplify reading dashboard config from a json file (#1828)
* refactor: simplify reading config from a json file * refactor: restore `data` nesting of config object Co-authored-by: Manuel <[email protected]>
1 parent 7846029 commit 921712a

File tree

3 files changed

+61
-102
lines changed

3 files changed

+61
-102
lines changed

Parse-Dashboard/index.js

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Command line tool for npm start
99
'use strict'
1010
const path = require('path');
11-
const jsonFile = require('json-file-plus');
11+
const fs = require('fs');
1212
const express = require('express');
1313
const parseDashboard = require('./app');
1414
const CLIHelper = require('./CLIHelper.js');
@@ -126,74 +126,72 @@ if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) {
126126
}
127127
}
128128

129-
let p = null;
129+
let config = null;
130130
let configFilePath = null;
131131
if (configFile) {
132-
p = jsonFile(configFile);
133-
configFilePath = path.dirname(configFile);
132+
try {
133+
config = {
134+
data: JSON.parse(fs.readFileSync(configFile, 'utf8'))
135+
};
136+
configFilePath = path.dirname(configFile);
137+
} catch (error) {
138+
if (error instanceof SyntaxError) {
139+
console.log('Your config file contains invalid JSON. Exiting.');
140+
process.exit(1);
141+
} else if (error.code === 'ENOENT') {
142+
if (explicitConfigFileProvided) {
143+
console.log('Your config file is missing. Exiting.');
144+
process.exit(2);
145+
} else {
146+
console.log('You must provide either a config file or required CLI options (app ID, Master Key, and server URL); not both.');
147+
process.exit(3);
148+
}
149+
} else {
150+
console.log('There was a problem with your config. Exiting.');
151+
process.exit(-1);
152+
}
153+
}
134154
} else if (configFromCLI) {
135-
p = Promise.resolve(configFromCLI);
155+
config = configFromCLI;
136156
} else {
137157
//Failed to load default config file.
138158
console.log('You must provide either a config file or an app ID, Master Key, and server URL. See parse-dashboard --help for details.');
139159
process.exit(4);
140160
}
141-
p.then(config => {
142-
config.data.apps.forEach(app => {
143-
if (!app.appName) {
144-
app.appName = app.appId;
145-
}
146-
});
147161

148-
if (config.data.iconsFolder && configFilePath) {
149-
config.data.iconsFolder = path.join(configFilePath, config.data.iconsFolder);
162+
config.data.apps.forEach(app => {
163+
if (!app.appName) {
164+
app.appName = app.appId;
150165
}
166+
});
151167

152-
const app = express();
168+
if (config.data.iconsFolder && configFilePath) {
169+
config.data.iconsFolder = path.join(configFilePath, config.data.iconsFolder);
170+
}
153171

154-
if (allowInsecureHTTP || trustProxy || dev) app.enable('trust proxy');
172+
const app = express();
155173

156-
config.data.trustProxy = trustProxy;
157-
let dashboardOptions = { allowInsecureHTTP, cookieSessionSecret, dev };
158-
app.use(mountPath, parseDashboard(config.data, dashboardOptions));
159-
let server;
160-
if(!configSSLKey || !configSSLCert){
161-
// Start the server.
162-
server = app.listen(port, host, function () {
163-
console.log(`The dashboard is now available at http://${server.address().address}:${server.address().port}${mountPath}`);
164-
});
165-
} else {
166-
// Start the server using SSL.
167-
var fs = require('fs');
168-
var privateKey = fs.readFileSync(configSSLKey);
169-
var certificate = fs.readFileSync(configSSLCert);
174+
if (allowInsecureHTTP || trustProxy || dev) app.enable('trust proxy');
170175

171-
server = require('https').createServer({
172-
key: privateKey,
173-
cert: certificate
174-
}, app).listen(port, host, function () {
175-
console.log(`The dashboard is now available at https://${server.address().address}:${server.address().port}${mountPath}`);
176-
});
177-
}
178-
handleSIGs(server);
179-
}, error => {
180-
if (error instanceof SyntaxError) {
181-
console.log('Your config file contains invalid JSON. Exiting.');
182-
process.exit(1);
183-
} else if (error.code === 'ENOENT') {
184-
if (explicitConfigFileProvided) {
185-
console.log('Your config file is missing. Exiting.');
186-
process.exit(2);
187-
} else {
188-
console.log('You must provide either a config file or required CLI options (app ID, Master Key, and server URL); not both.');
189-
process.exit(3);
190-
}
191-
} else {
192-
console.log('There was a problem with your config. Exiting.');
193-
process.exit(-1);
194-
}
195-
})
196-
.catch(error => {
197-
console.log('There was a problem loading the dashboard. Exiting.', error);
198-
process.exit(-1);
199-
});
176+
config.data.trustProxy = trustProxy;
177+
let dashboardOptions = { allowInsecureHTTP, cookieSessionSecret, dev };
178+
app.use(mountPath, parseDashboard(config.data, dashboardOptions));
179+
let server;
180+
if(!configSSLKey || !configSSLCert){
181+
// Start the server.
182+
server = app.listen(port, host, function () {
183+
console.log(`The dashboard is now available at http://${server.address().address}:${server.address().port}${mountPath}`);
184+
});
185+
} else {
186+
// Start the server using SSL.
187+
var privateKey = fs.readFileSync(configSSLKey);
188+
var certificate = fs.readFileSync(configSSLCert);
189+
190+
server = require('https').createServer({
191+
key: privateKey,
192+
cert: certificate
193+
}, app).listen(port, host, function () {
194+
console.log(`The dashboard is now available at https://${server.address().address}:${server.address().port}${mountPath}`);
195+
});
196+
}
197+
handleSIGs(server);

package-lock.json

Lines changed: 5 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
"immutable-devtools": "0.1.5",
5252
"inquirer": "8.1.3",
5353
"js-beautify": "1.14.0",
54-
"json-file-plus": "3.2.0",
5554
"otpauth": "7.0.6",
5655
"package-json": "6.5.0",
5756
"parse": "3.3.1",

0 commit comments

Comments
 (0)