7
7
*/
8
8
// Command line tool for npm start
9
9
'use strict'
10
- const path = require ( 'path' ) ;
11
- const fs = require ( 'fs' ) ;
12
- const express = require ( 'express' ) ;
13
- const parseDashboard = require ( './app' ) ;
14
10
const CLIHelper = require ( './CLIHelper.js' ) ;
11
+ const startServer = require ( './server' ) ;
15
12
16
13
const program = require ( 'commander' ) ;
17
14
program . option ( '--appId [appId]' , 'the app Id of the app you would like to manage.' ) ;
@@ -31,168 +28,21 @@ program.option('--trustProxy [trustProxy]', 'set this flag when you are behind a
31
28
program . option ( '--cookieSessionSecret [cookieSessionSecret]' , 'set the cookie session secret, defaults to a random string. You should set that value if you want sessions to work across multiple server, or across restarts' ) ;
32
29
program . option ( '--createUser' , 'helper tool to allow you to generate secure user passwords and secrets. Use this on trusted devices only.' ) ;
33
30
program . option ( '--createMFA' , 'helper tool to allow you to generate multi-factor authentication secrets.' ) ;
34
-
35
- program . parse ( process . argv ) ;
36
- const options = program . opts ( ) ;
37
-
38
- for ( const key in options ) {
39
- const func = CLIHelper [ key ] ;
40
- if ( func && typeof func === 'function' ) {
41
- func ( ) ;
42
- return ;
43
- }
44
- }
45
-
46
- const host = options . host || process . env . HOST || '0.0.0.0' ;
47
- const port = options . port || process . env . PORT || 4040 ;
48
- const mountPath = options . mountPath || process . env . MOUNT_PATH || '/' ;
49
- const allowInsecureHTTP = options . allowInsecureHTTP || process . env . PARSE_DASHBOARD_ALLOW_INSECURE_HTTP ;
50
- const cookieSessionSecret = options . cookieSessionSecret || process . env . PARSE_DASHBOARD_COOKIE_SESSION_SECRET ;
51
- const trustProxy = options . trustProxy || process . env . PARSE_DASHBOARD_TRUST_PROXY ;
52
- const dev = options . dev ;
53
-
54
- if ( trustProxy && allowInsecureHTTP ) {
55
- console . log ( 'Set only trustProxy *or* allowInsecureHTTP, not both. Only one is needed to handle being behind a proxy.' ) ;
56
- process . exit ( - 1 ) ;
57
- }
58
-
59
- let explicitConfigFileProvided = ! ! options . config ;
60
- let configFile = null ;
61
- let configFromCLI = null ;
62
- let configServerURL = options . serverURL || process . env . PARSE_DASHBOARD_SERVER_URL ;
63
- let configGraphQLServerURL = options . graphQLServerURL || process . env . PARSE_DASHBOARD_GRAPHQL_SERVER_URL ;
64
- let configMasterKey = options . masterKey || process . env . PARSE_DASHBOARD_MASTER_KEY ;
65
- let configAppId = options . appId || process . env . PARSE_DASHBOARD_APP_ID ;
66
- let configAppName = options . appName || process . env . PARSE_DASHBOARD_APP_NAME ;
67
- let configUserId = options . userId || process . env . PARSE_DASHBOARD_USER_ID ;
68
- let configUserPassword = options . userPassword || process . env . PARSE_DASHBOARD_USER_PASSWORD ;
69
- let configSSLKey = options . sslKey || process . env . PARSE_DASHBOARD_SSL_KEY ;
70
- let configSSLCert = options . sslCert || process . env . PARSE_DASHBOARD_SSL_CERT ;
71
-
72
- function handleSIGs ( server ) {
73
- const signals = {
74
- 'SIGINT' : 2 ,
75
- 'SIGTERM' : 15
76
- } ;
77
- function shutdown ( signal , value ) {
78
- server . close ( function ( ) {
79
- console . log ( 'server stopped by ' + signal ) ;
80
- process . exit ( 128 + value ) ;
81
- } ) ;
82
- }
83
- Object . keys ( signals ) . forEach ( function ( signal ) {
84
- process . on ( signal , function ( ) {
85
- shutdown ( signal , signals [ signal ] ) ;
86
- } ) ;
87
- } ) ;
88
- }
89
-
90
- if ( ! options . config && ! process . env . PARSE_DASHBOARD_CONFIG ) {
91
- if ( configServerURL && configMasterKey && configAppId ) {
92
- configFromCLI = {
93
- data : {
94
- apps : [
95
- {
96
- appId : configAppId ,
97
- serverURL : configServerURL ,
98
- masterKey : configMasterKey ,
99
- appName : configAppName ,
100
- } ,
101
- ]
102
- }
103
- } ;
104
- if ( configGraphQLServerURL ) {
105
- configFromCLI . data . apps [ 0 ] . graphQLServerURL = configGraphQLServerURL ;
106
- }
107
- if ( configUserId && configUserPassword ) {
108
- configFromCLI . data . users = [
109
- {
110
- user : configUserId ,
111
- pass : configUserPassword ,
112
- }
113
- ] ;
31
+ program . action ( async ( options ) => {
32
+ for ( const key in options ) {
33
+ const func = CLIHelper [ key ] ;
34
+ if ( func && typeof func === 'function' ) {
35
+ await func ( ) ;
36
+ process . exit ( 0 ) ;
114
37
}
115
- } else if ( ! configServerURL && ! configMasterKey && ! configAppName ) {
116
- configFile = path . join ( __dirname , 'parse-dashboard-config.json' ) ;
117
- }
118
- } else if ( ! options . config && process . env . PARSE_DASHBOARD_CONFIG ) {
119
- configFromCLI = {
120
- data : JSON . parse ( process . env . PARSE_DASHBOARD_CONFIG )
121
- } ;
122
- } else {
123
- configFile = options . config ;
124
- if ( options . appId || options . serverURL || options . masterKey || options . appName || options . graphQLServerURL ) {
125
- console . log ( 'You must provide either a config file or other CLI options (appName, appId, masterKey, serverURL, and graphQLServerURL); not both.' ) ;
126
- process . exit ( 3 ) ;
127
- }
128
- }
129
-
130
- let config = null ;
131
- let configFilePath = null ;
132
- if ( configFile ) {
133
- try {
134
- config = {
135
- data : JSON . parse ( fs . readFileSync ( configFile , 'utf8' ) )
136
- } ;
137
- configFilePath = path . dirname ( configFile ) ;
138
- } catch ( error ) {
139
- if ( error instanceof SyntaxError ) {
140
- console . log ( 'Your config file contains invalid JSON. Exiting.' ) ;
141
- process . exit ( 1 ) ;
142
- } else if ( error . code === 'ENOENT' ) {
143
- if ( explicitConfigFileProvided ) {
144
- console . log ( 'Your config file is missing. Exiting.' ) ;
145
- process . exit ( 2 ) ;
146
- } else {
147
- console . log ( 'You must provide either a config file or required CLI options (app ID, Master Key, and server URL); not both.' ) ;
148
- process . exit ( 3 ) ;
149
- }
150
- } else {
151
- console . log ( 'There was a problem with your config. Exiting.' ) ;
152
- process . exit ( - 1 ) ;
153
- }
154
- }
155
- } else if ( configFromCLI ) {
156
- config = configFromCLI ;
157
- } else {
158
- //Failed to load default config file.
159
- console . log ( 'You must provide either a config file or an app ID, Master Key, and server URL. See parse-dashboard --help for details.' ) ;
160
- process . exit ( 4 ) ;
161
- }
162
-
163
- config . data . apps . forEach ( app => {
164
- if ( ! app . appName ) {
165
- app . appName = app . appId ;
166
38
}
167
39
} ) ;
168
40
169
- if ( config . data . iconsFolder && configFilePath ) {
170
- config . data . iconsFolder = path . join ( configFilePath , config . data . iconsFolder ) ;
171
- }
172
-
173
- const app = express ( ) ;
41
+ async function run ( ) {
42
+ await program . parseAsync ( process . argv ) ;
43
+ const options = program . opts ( ) ;
174
44
175
- if ( allowInsecureHTTP || trustProxy || dev ) app . enable ( 'trust proxy' ) ;
176
-
177
- config . data . trustProxy = trustProxy ;
178
- let dashboardOptions = { allowInsecureHTTP, cookieSessionSecret, dev } ;
179
- app . use ( mountPath , parseDashboard ( config . data , dashboardOptions ) ) ;
180
- let server ;
181
- if ( ! configSSLKey || ! configSSLCert ) {
182
- // Start the server.
183
- server = app . listen ( port , host , function ( ) {
184
- console . log ( `The dashboard is now available at http://${ server . address ( ) . address } :${ server . address ( ) . port } ${ mountPath } ` ) ;
185
- } ) ;
186
- } else {
187
- // Start the server using SSL.
188
- var privateKey = fs . readFileSync ( configSSLKey ) ;
189
- var certificate = fs . readFileSync ( configSSLCert ) ;
190
-
191
- server = require ( 'https' ) . createServer ( {
192
- key : privateKey ,
193
- cert : certificate
194
- } , app ) . listen ( port , host , function ( ) {
195
- console . log ( `The dashboard is now available at https://${ server . address ( ) . address } :${ server . address ( ) . port } ${ mountPath } ` ) ;
196
- } ) ;
45
+ startServer ( options ) ;
197
46
}
198
- handleSIGs ( server ) ;
47
+
48
+ run ( ) ;
0 commit comments