Skip to content

Commit

Permalink
fix(get-schema): make sure dir exists
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Apr 27, 2018
1 parent 10a8554 commit cffef19
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"is-url-superb": "2.0.0",
"js-yaml": "^3.10.0",
"lodash": "^4.17.5",
"mkdirp": "^0.5.1",
"node-fetch": "^2.0.0",
"npm-paths": "^1.0.0",
"opn": "^5.2.0",
Expand Down
21 changes: 14 additions & 7 deletions src/cmds/get-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EventEmitter } from 'events'
import * as fs from 'fs'
import { relative } from 'path'
import * as mkdirp from 'mkdirp'
import { relative, dirname } from 'path'
import { printSchema, GraphQLSchema } from 'graphql'
import {
writeSchema,
Expand Down Expand Up @@ -61,7 +62,8 @@ const command: CommandObject = {
type: 'boolean',
},
header: {
describe: 'Header to use for downloading (with endpoint URL). Format: Header=Value',
describe:
'Header to use for downloading (with endpoint URL). Format: Header=Value',
type: 'string',
},
})
Expand Down Expand Up @@ -201,8 +203,8 @@ async function updateSingleProjectEndpoint(
let newSchemaResult
try {
newSchemaResult = argv.json
? await endpoint.resolveIntrospection()
: await endpoint.resolveSchema()
? await endpoint.resolveIntrospection()
: await endpoint.resolveSchema()
} catch (err) {
emitter.emit('warning', err.message)
return
Expand Down Expand Up @@ -245,18 +247,23 @@ async function updateSingleProjectEndpoint(
}
}

let schemaPath
let schemaPath = argv.output
if (argv.console) {
console.log(
argv.json
? JSON.stringify(newSchemaResult, null, 2)
: printSchema(newSchemaResult as GraphQLSchema),
)
} else if (argv.json) {
schemaPath = argv.output
if (!fs.existsSync(schemaPath)) {
mkdirp.sync(dirname(schemaPath))
}
fs.writeFileSync(argv.output, JSON.stringify(newSchemaResult, null, 2))
} else {
schemaPath = argv.output ? argv.output : config!.schemaPath
schemaPath = schemaPath || config!.schemaPath
if (!fs.existsSync(schemaPath)) {
mkdirp.sync(dirname(schemaPath))
}
await writeSchema(schemaPath as string, newSchemaResult as GraphQLSchema, {
source: endpoint.url,
timestamp: new Date().toString(),
Expand Down

0 comments on commit cffef19

Please sign in to comment.