Skip to content

Commit 7a0fedc

Browse files
Alan-ChaErikWittern
authored andcommitted
Clean up code
Signed-off-by: Alan Cha <[email protected]>
1 parent 9c5f7d7 commit 7a0fedc

22 files changed

+493
-534
lines changed

packages/oasgraph-cli/src/oasgraph.ts

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ program
3030
.parse(process.argv)
3131

3232
// 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
3736

38-
let filePaths = program.args
37+
const filePaths = program.args
3938

4039
if (typeof filePaths === 'undefined' || filePaths.length === 0) {
4140
console.error('No path(s) provided')
@@ -76,7 +75,7 @@ Promise.all(filePaths.map(filePath => {
7675
startGraphQLServer(oass, portNumber)
7776
})
7877
.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.`)
8079
process.exit(1)
8180
})
8281

@@ -89,15 +88,12 @@ Promise.all(filePaths.map(filePath => {
8988
*/
9089
function readFile (path) {
9190
try {
92-
let doc
93-
if (/json$/.test(path)) {
94-
doc = JSON.parse(fs.readFileSync(path, 'utf8'))
95-
} else if (/yaml$|yml$/.test(path)) {
96-
doc = yaml.safeLoad(fs.readFileSync(path, 'utf8'))
97-
}
91+
const doc = /json$/.test(path) ?
92+
JSON.parse(fs.readFileSync(path, 'utf8')) :
93+
yaml.safeLoad(fs.readFileSync(path, 'utf8'))
9894
return doc
9995
} catch (e) {
100-
console.error('Error: failed to parse YAML/JSON: ' + e)
96+
console.error('Error: failed to parse YAML/JSON')
10197
return null
10298
}
10399
}
@@ -140,33 +136,33 @@ function startGraphQLServer(oas, port) {
140136
operationIdFieldNames: program.operationIdFieldNames,
141137
provideErrorExtensions: program.extensions
142138
})
143-
.then(({schema, report}) => {
144-
console.log(JSON.stringify(report, null, 2))
139+
.then(({schema, report}) => {
140+
console.log(JSON.stringify(report, null, 2))
145141

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());
165149
}
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+
})
170166
}
171167

172168

packages/oasgraph/lib/auth_builder.js

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

packages/oasgraph/lib/auth_builder.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/oasgraph/lib/index.js

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

0 commit comments

Comments
 (0)