Skip to content

Commit 0c9cb58

Browse files
Alan-ChaErikWittern
authored andcommitted
Prettier
Signed-off-by: Alan Cha <[email protected]>
1 parent 7a0fedc commit 0c9cb58

35 files changed

+1447
-1039
lines changed

packages/oasgraph-cli/.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "none",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true
6+
}

packages/oasgraph-cli/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
"dev": "tsc -w",
4242
"build": "tsc"
4343
},
44+
"husky": {
45+
"hooks": {
46+
"pre-commit": "pretty-quick --staged"
47+
}
48+
},
4449
"dependencies": {
4550
"commander": "^2.19.0",
4651
"cors": "^2.8.5",
@@ -53,7 +58,10 @@
5358
},
5459
"devDependencies": {
5560
"@types/node": "^11.9.4",
61+
"husky": "^2.3.0",
5662
"jest": "^24.1.0",
63+
"prettier": "^1.17.1",
64+
"pretty-quick": "^1.11.0",
5765
"standard": "^12.0.1",
5866
"tslint": "^5.11.0",
5967
"tslint-config-standard": "^8.0.1",

packages/oasgraph-cli/src/oasgraph.ts

Lines changed: 122 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -18,161 +18,186 @@ program
1818
.version(require('../package.json').version)
1919
.usage('<OAS JSON file path(s) and/or remote url(s)> [options]')
2020
.arguments('<path(s) and/or url(s)>')
21-
.option('-p, --port <port>', 'select the port where the server will start', parseInt)
21+
.option(
22+
'-p, --port <port>',
23+
'select the port where the server will start',
24+
parseInt
25+
)
2226
.option('-u, --url <url>', 'select the base url which paths will be built on')
23-
.option('-s, --strict', 'throw an error if OASGraph cannot run without compensating for errors or missing data in the OAS')
24-
.option('-f, --fillEmptyResponses', 'create placeholder schemas for operations with HTTP status code 204 (no response) rather than ignore them')
25-
.option('-o, --operationIdFieldNames', 'create field names based on the operationId')
27+
.option(
28+
'-s, --strict',
29+
'throw an error if OASGraph cannot run without compensating for errors or missing data in the OAS'
30+
)
31+
.option(
32+
'-f, --fillEmptyResponses',
33+
'create placeholder schemas for operations with HTTP status code 204 (no response) rather than ignore them'
34+
)
35+
.option(
36+
'-o, --operationIdFieldNames',
37+
'create field names based on the operationId'
38+
)
2639
.option('--cors', 'enable Cross-origin resource sharing (CORS)')
27-
.option('--no-viewer', 'do not create GraphQL viewer objects for passing authentication credentials')
28-
.option('--no-extensions', 'do not add extentions, containing information about failed REST calls, to the GraphQL errors objects')
40+
.option(
41+
'--no-viewer',
42+
'do not create GraphQL viewer objects for passing authentication credentials'
43+
)
44+
.option(
45+
'--no-extensions',
46+
'do not add extentions, containing information about failed REST calls, to the GraphQL errors objects'
47+
)
2948
.option('--save <file path>', 'save schema to path and do not start server')
3049
.parse(process.argv)
3150

3251
// Select the port on which to host the GraphQL server
33-
const portNumber: number | string = program.port ?
34-
program.port :
35-
3000
52+
const portNumber: number | string = program.port ? program.port : 3000
3653

3754
const filePaths = program.args
3855

3956
if (typeof filePaths === 'undefined' || filePaths.length === 0) {
4057
console.error('No path(s) provided')
41-
console.error('Please refer to the help manual (oasgraph -h) for more information')
58+
console.error(
59+
'Please refer to the help manual (oasgraph -h) for more information'
60+
)
4261
process.exit(1)
4362
}
4463

4564
// Load the OASs based off of the provided paths
46-
Promise.all(filePaths.map(filePath => {
47-
return new Promise((resolve, reject) => {
48-
// Check if the file exists
49-
if (fs.existsSync(path.resolve(filePath))) {
50-
try {
51-
resolve(readFile(path.resolve(filePath)))
52-
} catch (error) {
53-
console.error(error)
65+
Promise.all(
66+
filePaths.map(filePath => {
67+
return new Promise((resolve, reject) => {
68+
// Check if the file exists
69+
if (fs.existsSync(path.resolve(filePath))) {
70+
try {
71+
resolve(readFile(path.resolve(filePath)))
72+
} catch (error) {
73+
console.error(error)
74+
reject(filePath)
75+
}
76+
77+
// Check if file is in a remote location
78+
} else if (filePath.match(/^https?/g)) {
79+
getRemoteFileSpec(filePath)
80+
.then(remoteContent => {
81+
resolve(remoteContent)
82+
})
83+
.catch(error => {
84+
console.error(error)
85+
reject(filePath)
86+
})
87+
88+
// Cannot determine location of file
89+
} else {
5490
reject(filePath)
5591
}
56-
57-
// Check if file is in a remote location
58-
} else if (filePath.match(/^https?/g)) {
59-
getRemoteFileSpec(filePath)
60-
.then((remoteContent) => {
61-
resolve(remoteContent)
62-
})
63-
.catch((error) => {
64-
console.error(error)
65-
reject(filePath)
66-
})
67-
68-
// Cannot determine location of file
69-
} else {
70-
reject(filePath)
71-
}
92+
})
93+
})
94+
)
95+
.then(oass => {
96+
startGraphQLServer(oass, portNumber)
97+
})
98+
.catch(filePath => {
99+
console.error(
100+
`OASGraph cannot read file. File "${filePath}" does not exist.`
101+
)
102+
process.exit(1)
72103
})
73-
}))
74-
.then(oass => {
75-
startGraphQLServer(oass, portNumber)
76-
})
77-
.catch(filePath => {
78-
console.error(`OASGraph cannot read file. File "${filePath}" does not exist.`)
79-
process.exit(1)
80-
})
81-
82104

83105
/**
84-
* Returns content of read JSON/YAML file.
85-
*
86-
* @param {string} path Path to file to read
87-
* @return {object} Content of read file
88-
*/
89-
function readFile (path) {
106+
* Returns content of read JSON/YAML file.
107+
*
108+
* @param {string} path Path to file to read
109+
* @return {object} Content of read file
110+
*/
111+
function readFile(path) {
90112
try {
91-
const doc = /json$/.test(path) ?
92-
JSON.parse(fs.readFileSync(path, 'utf8')) :
93-
yaml.safeLoad(fs.readFileSync(path, 'utf8'))
113+
const doc = /json$/.test(path)
114+
? JSON.parse(fs.readFileSync(path, 'utf8'))
115+
: yaml.safeLoad(fs.readFileSync(path, 'utf8'))
94116
return doc
95117
} catch (e) {
96118
console.error('Error: failed to parse YAML/JSON')
97119
return null
98120
}
99121
}
100122

101-
102123
/**
103124
* reads a remote file content using http protocol
104125
* @param {string} url specifies a valid URL path including the port number
105126
*/
106-
function getRemoteFileSpec (uri) {
127+
function getRemoteFileSpec(uri) {
107128
return new Promise((resolve, reject) => {
108-
request({
109-
uri,
110-
json: true
111-
}, (err, res, body) => {
112-
if (err) {
113-
reject(err)
114-
} else if (res.statusCode !== 200) {
115-
reject(new Error(`Error: ${JSON.stringify(body)}`))
116-
} else {
117-
resolve(body)
129+
request(
130+
{
131+
uri,
132+
json: true
133+
},
134+
(err, res, body) => {
135+
if (err) {
136+
reject(err)
137+
} else if (res.statusCode !== 200) {
138+
reject(new Error(`Error: ${JSON.stringify(body)}`))
139+
} else {
140+
resolve(body)
141+
}
118142
}
119-
})
143+
)
120144
})
121145
}
122146

123-
124147
/**
125148
* generates a GraphQL schema and starts the GraphQL server on the specified port
126149
* @param {object} oas the OAS specification file
127150
* @param {number} port the port number to listen on on this server
128151
*/
129152
function startGraphQLServer(oas, port) {
130153
// Create GraphQL interface
131-
createGraphQlSchema(oas, {
154+
createGraphQlSchema(oas, {
132155
strict: program.strict,
133156
viewer: program.viewer,
134157
fillEmptyResponses: program.fillEmptyResponses,
135158
baseUrl: program.url,
136159
operationIdFieldNames: program.operationIdFieldNames,
137160
provideErrorExtensions: program.extensions
138161
})
139-
.then(({schema, report}) => {
140-
console.log(JSON.stringify(report, null, 2))
141-
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());
149-
}
162+
.then(({ schema, report }) => {
163+
console.log(JSON.stringify(report, null, 2))
150164

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-
})
165+
// save local file if required
166+
if (program.save) {
167+
writeSchema(schema)
168+
} else {
169+
// Enable CORS
170+
if (program.cors) {
171+
app.use(cors())
172+
}
173+
174+
// mounting graphql endpoint using the middleware express-graphql
175+
app.use(
176+
'/graphql',
177+
graphqlHTTP({
178+
schema: schema,
179+
graphiql: true
180+
})
181+
)
182+
183+
// initiating the server on the port specified by user or the default one
184+
app.listen(port, () => {
185+
console.log(`GraphQL accessible at: http://localhost:${port}/graphql`)
186+
})
187+
}
188+
})
189+
.catch(err => {
190+
console.log('OASGraph creation event error: ', err.message)
191+
})
166192
}
167193

168-
169194
/**
170195
* saves a grahpQL schema generated by OASGraph to a file
171196
* @param {createGraphQlSchema} schema
172197
*/
173-
function writeSchema(schema){
174-
fs.writeFile(program.save, printSchema(schema), (err) => {
198+
function writeSchema(schema) {
199+
fs.writeFile(program.save, printSchema(schema), err => {
175200
if (err) throw err
176201
console.log(`OASGraph successfully saved your schema at ${program.save}`)
177202
})
178-
}
203+
}

packages/oasgraph/.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "none",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true
6+
}

packages/oasgraph/lib/auth_builder.js

Lines changed: 14 additions & 12 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.

0 commit comments

Comments
 (0)