-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·55 lines (44 loc) · 1.63 KB
/
cli.js
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
53
54
55
#!/usr/bin/env node
const commandLineArgs = require('command-line-args')
const { resolve } = require('path')
const { red, yellow } = require('kleur')
const commands = new Map([
['add-hook', require('./commands/add-hook')],
['help', require('./commands/help')],
['init', require('./commands/init')],
['insert', require('./commands/insert')],
['install', require('./commands/install')],
['upload-sourcemaps', require('./commands/upload-sourcemaps')],
['set-api-key', require('./commands/set-api-key')]
])
// define top-level options
const cliOpts = [
{ name: 'command', defaultOption: true },
{ name: 'help', type: Boolean },
{ name: 'project-root', defaultValue: process.cwd() },
{ name: 'npm', type: Boolean },
{ name: 'yarn', type: Boolean }
]
const parsedArgs = commandLineArgs(cliOpts, { stopAtFirstUnknown: true })
const argv = parsedArgs._unknown || []
// make project root absolute
parsedArgs['project-root'] = resolve(process.cwd(), parsedArgs['project-root'])
const go = async () => {
try {
// `bugsnag-expo --help` works
if (parsedArgs.help) return await commands.get('help')(argv, parsedArgs)
// bugsnag-expo <cmd>
const cmd = commands.get(parsedArgs.command)
if (cmd) return await cmd(argv, parsedArgs)
// no command found, maybe nothing was provided?
process.exitCode = 1
// print out what was received
if (parsedArgs.command) console.log(yellow(`\n Unknown command: ${parsedArgs.command}`))
// send help
return await commands.get('help')(argv, parsedArgs)
} catch (e) {
console.error(red(`\n ${e.stack.split('\n').join('\n ')} \n`))
process.exitCode = 1
}
}
go()