Skip to content

Commit fcf02d7

Browse files
Alan-ChaErikWittern
authored andcommitted
Fix CLI remote YAML bug
If the remote file was YAML, the CLI would not parse it correctly. Signed-off-by: Alan Cha <[email protected]>
1 parent 71b93a3 commit fcf02d7

File tree

3 files changed

+65
-43
lines changed

3 files changed

+65
-43
lines changed

packages/openapi-to-graphql-cli/lib/openapi-to-graphql.js

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

packages/openapi-to-graphql-cli/lib/openapi-to-graphql.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/openapi-to-graphql-cli/src/openapi-to-graphql.ts

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ Promise.all(
8686
try {
8787
resolve(readFile(path.resolve(filePath)))
8888
} catch (error) {
89-
console.error(error)
90-
reject(filePath)
89+
reject(error)
9190
}
9291

9392
// Check if file is in a remote location
@@ -97,24 +96,21 @@ Promise.all(
9796
resolve(remoteContent)
9897
})
9998
.catch(error => {
100-
console.error(error)
101-
reject(filePath)
99+
reject(error)
102100
})
103101

104102
// Cannot determine location of file
105103
} else {
106-
reject(filePath)
104+
reject(`File path '${filePath}' is invalid`)
107105
}
108106
})
109107
})
110108
)
111109
.then(oass => {
112110
startGraphQLServer(oass, portNumber)
113111
})
114-
.catch(filePath => {
115-
console.error(
116-
`OpenAPI-to-GraphQL cannot read file. File '${filePath}' does not exist.`
117-
)
112+
.catch(error => {
113+
console.error(error)
118114
process.exit(1)
119115
})
120116

@@ -125,14 +121,15 @@ Promise.all(
125121
* @return {object} Content of read file
126122
*/
127123
function readFile(path) {
128-
try {
129-
const doc = /json$/.test(path)
130-
? JSON.parse(fs.readFileSync(path, 'utf8'))
131-
: yaml.safeLoad(fs.readFileSync(path, 'utf8'))
132-
return doc
133-
} catch (e) {
134-
console.error('Error: failed to parse YAML/JSON')
135-
return null
124+
if (/json$/.test(path)) {
125+
return JSON.parse(fs.readFileSync(path, 'utf8'))
126+
127+
} else if (/yaml$/.test(path) || /yml$/.test(path)) {
128+
return yaml.safeLoad(fs.readFileSync(path, 'utf8'))
129+
130+
} else {
131+
throw new Error(`Failed to parse JSON/YAML. Ensure file '${path}' has ` +
132+
`the correct extension (i.e. '.json', '.yaml', or '.yml).`)
136133
}
137134
}
138135

@@ -144,16 +141,31 @@ function getRemoteFileSpec(uri) {
144141
return new Promise((resolve, reject) => {
145142
request(
146143
{
147-
uri,
148-
json: true
144+
uri
149145
},
150146
(err, res, body) => {
151147
if (err) {
152148
reject(err)
153-
} else if (res.statusCode !== 200) {
154-
reject(new Error(`Error: ${JSON.stringify(body)}`))
149+
} else if (res.statusCode < 200 && res.statusCode <= 300) {
150+
reject(
151+
new Error(
152+
`Could not retrieve file. Received unsuccessful status code '${res.statusCode}.`
153+
)
154+
)
155155
} else {
156-
resolve(body)
156+
if (typeof body === 'string') {
157+
try {
158+
resolve(JSON.parse(body))
159+
} catch (e) {
160+
try {
161+
resolve(yaml.safeLoad(body))
162+
} catch (f) {
163+
console.error(`JSON parse error: ${e}\nYAML parse error: ${f}`)
164+
}
165+
}
166+
}
167+
168+
reject(new Error(`Cannot parse remote file`))
157169
}
158170
}
159171
)

0 commit comments

Comments
 (0)