UNPKG

1.82 kBPlain TextView Raw
1#!/usr/bin/env node
2
3import * as minimist from 'minimist'
4import { Command } from './types'
5import createCommand from './commands/create'
6import fetchCommand from './commands/fetch'
7import authCommand from './commands/auth'
8import * as chalk from 'chalk'
9import figures = require('figures')
10import { usageRoot, usageCreate, usageFetch, usageAuth } from './usage'
11
12const {version} = require('../package.json')
13
14async function main() {
15 const argv = minimist(process.argv.slice(2))
16
17 const command = argv._[0] as Command | undefined
18
19 process.stdout.write('\n')
20
21 switch (command) {
22 case undefined: {
23 process.stdout.write(usageRoot)
24 process.exit(0)
25 }
26 case 'auth': {
27 checkHelp(argv, usageAuth)
28
29 const token = argv['token'] || argv['t']
30 await authCommand({
31 token,
32 })
33 break
34 }
35 case 'create': {
36 checkHelp(argv, usageCreate)
37
38 const name = argv['name'] || argv['n']
39 const alias = argv['alias'] || argv['a']
40 const schema = argv._[1]
41
42 await createCommand({
43 name,
44 alias,
45 schema,
46 })
47 break
48 }
49 case 'fetch': {
50 checkHelp(argv, usageFetch)
51
52 const projectId = argv['project-id'] || argv['p']
53 await fetchCommand({
54 projectId,
55 })
56 break
57 }
58 case 'version': {
59 console.log(version)
60 break
61 }
62 default: {
63 console.log(`Unknown command: ${command}`)
64 break
65 }
66 }
67}
68
69function checkHelp(argv: any, usage: string) {
70 if (argv['help'] || argv['h']) {
71 process.stdout.write(usage)
72 process.exit(0)
73 }
74}
75
76function onError(e: Error) {
77 console.log(`${chalk.red(figures.cross)} Error: ${e.message}\n`)
78 console.log(e.stack)
79 process.exit(1)
80}
81
82process.on('unhandledRejection', e => onError(e))
83
84main().catch(e => onError(e))