-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (45 loc) · 1.4 KB
/
index.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/local/bin/node
const assert = require('assert')
const program = require('commander')
const pkg = require('./package.json')
const Robot = require('./lib/robot')
const subCommands = require('./lib/subCommands')
const list = val => {
return val ? val.split(',') : []
}
program
.version(pkg.version)
.option('-t, --token [access token]', 'Dingtalk robot accessToken or set env.DT_ROBOT_TOKEN')
.option('-q --quiet', 'will not exit with 1 when fail', false)
.option('--at [mobiles]', 'At somebody, limited using', list)
.option('--isAtAll', 'If is atall, limited using', false)
subCommands.forEach(({ command, options = [], description }) => {
let p = program.command(command).description(`${description}, use <cmd> -h to read help info`)
options.map(option => {
p = p.option.apply(p, option)
})
p.action((...args) => {
let token
const arg = args[0]
let opt = args[1]
let others
if (args.length > 2) {
others = args[1]
opt = args[2]
}
if (!(token = process.env.DT_ROBOT_TOKEN || opt.parent.token)) {
console.error(' ')
console.error('No access token given!')
console.error(' ')
process.exit(opt.parent.quiet ? 0 : 1)
}
const robot = new Robot({
token,
at: opt.parent.at,
isAtAll: opt.parent.isAtAll,
quiet: opt.parent.quiet
})
robot[opt._name](arg, opt, others)
})
})
program.parse(process.argv)