@@ -30,12 +30,11 @@ program
30
30
. parse ( process . argv )
31
31
32
32
// Select the port on which to host the GraphQL server
33
- let portNumber : number | string = 3000
34
- if ( program . port ) {
35
- portNumber = program . port
36
- }
33
+ const portNumber : number | string = program . port ?
34
+ program . port :
35
+ 3000
37
36
38
- let filePaths = program . args
37
+ const filePaths = program . args
39
38
40
39
if ( typeof filePaths === 'undefined' || filePaths . length === 0 ) {
41
40
console . error ( 'No path(s) provided' )
@@ -76,7 +75,7 @@ Promise.all(filePaths.map(filePath => {
76
75
startGraphQLServer ( oass , portNumber )
77
76
} )
78
77
. catch ( filePath => {
79
- console . error ( `OASGraph cannot read file. File ' ${ filePath } ' does not exist.` )
78
+ console . error ( `OASGraph cannot read file. File " ${ filePath } " does not exist.` )
80
79
process . exit ( 1 )
81
80
} )
82
81
@@ -89,15 +88,12 @@ Promise.all(filePaths.map(filePath => {
89
88
*/
90
89
function readFile ( path ) {
91
90
try {
92
- let doc
93
- if ( / j s o n $ / . test ( path ) ) {
94
- doc = JSON . parse ( fs . readFileSync ( path , 'utf8' ) )
95
- } else if ( / y a m l $ | y m l $ / . test ( path ) ) {
96
- doc = yaml . safeLoad ( fs . readFileSync ( path , 'utf8' ) )
97
- }
91
+ const doc = / j s o n $ / . test ( path ) ?
92
+ JSON . parse ( fs . readFileSync ( path , 'utf8' ) ) :
93
+ yaml . safeLoad ( fs . readFileSync ( path , 'utf8' ) )
98
94
return doc
99
95
} catch ( e ) {
100
- console . error ( 'Error: failed to parse YAML/JSON: ' + e )
96
+ console . error ( 'Error: failed to parse YAML/JSON' )
101
97
return null
102
98
}
103
99
}
@@ -140,33 +136,33 @@ function startGraphQLServer(oas, port) {
140
136
operationIdFieldNames : program . operationIdFieldNames ,
141
137
provideErrorExtensions : program . extensions
142
138
} )
143
- . then ( ( { schema, report} ) => {
144
- console . log ( JSON . stringify ( report , null , 2 ) )
139
+ . then ( ( { schema, report} ) => {
140
+ console . log ( JSON . stringify ( report , null , 2 ) )
145
141
146
- // save local file if required
147
- if ( program . save ) {
148
- writeSchema ( schema ) ;
149
- } else {
150
- // Enable CORS
151
- if ( program . cors ) {
152
- app . use ( cors ( ) ) ;
153
- }
154
-
155
- // mounting graphql endpoint using the middleware express-graphql
156
- app . use ( '/graphql' , graphqlHTTP ( {
157
- schema : schema ,
158
- graphiql : true
159
- } ) )
160
-
161
- // initiating the server on the port specified by user or the default one
162
- app . listen ( port , ( ) => {
163
- console . log ( `GraphQL accessible at: http://localhost:${ port } /graphql` )
164
- } )
142
+ // save local file if required
143
+ if ( program . save ) {
144
+ writeSchema ( schema ) ;
145
+ } else {
146
+ // Enable CORS
147
+ if ( program . cors ) {
148
+ app . use ( cors ( ) ) ;
165
149
}
166
- } )
167
- . catch ( err => {
168
- console . log ( 'OASGraph creation event error: ' , err . message )
169
- } )
150
+
151
+ // mounting graphql endpoint using the middleware express-graphql
152
+ app . use ( '/graphql' , graphqlHTTP ( {
153
+ schema : schema ,
154
+ graphiql : true
155
+ } ) )
156
+
157
+ // initiating the server on the port specified by user or the default one
158
+ app . listen ( port , ( ) => {
159
+ console . log ( `GraphQL accessible at: http://localhost:${ port } /graphql` )
160
+ } )
161
+ }
162
+ } )
163
+ . catch ( err => {
164
+ console . log ( 'OASGraph creation event error: ' , err . message )
165
+ } )
170
166
}
171
167
172
168
0 commit comments