-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcli.ts
52 lines (44 loc) · 1.63 KB
/
cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env node
import program from 'commander'
import Listr from 'listr'
import chalk from 'chalk'
import { task } from './cliHelpers/task'
import { validateConfigs } from './cliHelpers/validateConfigs'
import { Config } from './config'
import { requireModuleFromPath } from './helpers/files'
function collect(value: string, previous: string[]) {
return previous.concat([value])
}
program
.option('-o, --output <./myClient>', 'output directory')
.option('-e, --endpoint <http://example.com/graphql>', 'GraphQL endpoint')
.option('-p, --post', 'use POST for introspection query')
.option("-H, --header <'header: value'>", 'headers to use in fetch', collect, [])
.option('-s, --schema <./mySchema.graphql>', 'path to GraphQL schema definition file')
.option('-f, --fetcher <./schemaFetcher.js>', 'path to introspection query fetcher file')
.option('-c, --config <./myConfig.js>', 'path to config file')
.option('-v, --verbose', 'verbose output')
.parse(process.argv)
const resolveConfigs = (configs: Config | Config[]) => (Array.isArray(configs) ? configs : [configs])
const configs: Config[] = program.config
? resolveConfigs(<Config>requireModuleFromPath([program.config]))
: [
{
endpoint: program.endpoint,
post: program.post,
headers: program.header,
schema: program.schema,
output: program.output,
fetcher: program.fetcher,
},
]
if (!validateConfigs(configs)) program.help()
new Listr(
configs.map(config => task(config)),
{ renderer: program.verbose ? 'verbose' : 'default' },
)
.run()
.catch(e => {
console.log(chalk.red(e.stack))
process.exit(1)
})