|
8 | 8 | // Command line tool for npm start
|
9 | 9 | 'use strict'
|
10 | 10 | const path = require('path');
|
11 |
| -const jsonFile = require('json-file-plus'); |
| 11 | +const fs = require('fs'); |
12 | 12 | const express = require('express');
|
13 | 13 | const parseDashboard = require('./app');
|
14 | 14 | const CLIHelper = require('./CLIHelper.js');
|
@@ -126,74 +126,72 @@ if (!program.config && !process.env.PARSE_DASHBOARD_CONFIG) {
|
126 | 126 | }
|
127 | 127 | }
|
128 | 128 |
|
129 |
| -let p = null; |
| 129 | +let config = null; |
130 | 130 | let configFilePath = null;
|
131 | 131 | 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 | + } |
134 | 154 | } else if (configFromCLI) {
|
135 |
| - p = Promise.resolve(configFromCLI); |
| 155 | + config = configFromCLI; |
136 | 156 | } else {
|
137 | 157 | //Failed to load default config file.
|
138 | 158 | console.log('You must provide either a config file or an app ID, Master Key, and server URL. See parse-dashboard --help for details.');
|
139 | 159 | process.exit(4);
|
140 | 160 | }
|
141 |
| -p.then(config => { |
142 |
| - config.data.apps.forEach(app => { |
143 |
| - if (!app.appName) { |
144 |
| - app.appName = app.appId; |
145 |
| - } |
146 |
| - }); |
147 | 161 |
|
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; |
150 | 165 | }
|
| 166 | +}); |
151 | 167 |
|
152 |
| - const app = express(); |
| 168 | +if (config.data.iconsFolder && configFilePath) { |
| 169 | + config.data.iconsFolder = path.join(configFilePath, config.data.iconsFolder); |
| 170 | +} |
153 | 171 |
|
154 |
| - if (allowInsecureHTTP || trustProxy || dev) app.enable('trust proxy'); |
| 172 | +const app = express(); |
155 | 173 |
|
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'); |
170 | 175 |
|
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); |
0 commit comments