/
hubot
executable file
·135 lines (107 loc) · 3.96 KB
/
hubot
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env coffee
# vim:ft=coffee ts=2 sw=2 et :
# -*- mode:coffee -*-
Hubot = require '..'
Fs = require 'fs'
OptParse = require 'optparse'
Path = require 'path'
Switches = [
[ "-a", "--adapter ADAPTER", "The Adapter to use" ],
[ "-c", "--create PATH", "Create a deployable hubot" ],
[ "-d", "--disable-httpd", "Disable the HTTP server" ],
[ "-h", "--help", "Display the help information" ],
[ "-l", "--alias ALIAS", "Enable replacing the robot's name with alias" ],
[ "-n", "--name NAME", "The name of the robot in chat" ],
[ "-r", "--require PATH", "Alternative scripts path" ],
[ "-t", "--config-check", "Test hubot's config to make sure it won't fail at startup"]
[ "-v", "--version", "Displays the version of hubot installed" ]
]
Options =
adapter: process.env.HUBOT_ADAPTER or "shell"
alias: process.env.HUBOT_ALIAS or false
create: process.env.HUBOT_CREATE or false
enableHttpd: process.env.HUBOT_HTTPD or true
scripts: process.env.HUBOT_SCRIPTS or []
name: process.env.HUBOT_NAME or "Hubot"
path: process.env.HUBOT_PATH or "."
configCheck: false
Parser = new OptParse.OptionParser(Switches)
Parser.banner = "Usage hubot [options]"
Parser.on "adapter", (opt, value) ->
Options.adapter = value
Parser.on "create", (opt, value) ->
Options.path = value
Options.create = true
Parser.on "disable-httpd", (opt) ->
Options.enableHttpd = false
Parser.on "help", (opt, value) ->
console.log Parser.toString()
process.exit 0
Parser.on "alias", (opt, value) ->
value or= '/'
Options.alias = value
Parser.on "name", (opt, value) ->
Options.name = value
Parser.on "require", (opt, value) ->
Options.scripts.push(value)
Parser.on "config-check", (opt) ->
Options.configCheck = true
Parser.on "version", (opt, value) ->
Options.version = true
Parser.on (opt, value) ->
console.warn "Unknown option: #{opt}"
Parser.parse process.argv
unless process.platform is "win32"
process.on 'SIGTERM', ->
process.exit 0
if Options.create
console.error "'hubot --create' is deprecated. Use the yeoman generator instead:"
console.error " npm install -g yo generator-hubot"
console.error " mkdir -p #{Options.path}"
console.error " cd #{Options.path}"
console.error " yo hubot"
console.error "See https://github.com/github/hubot/blob/master/docs/index.md for more details on getting started."
process.exit 1
else
robot = Hubot.loadBot undefined, Options.adapter, Options.enableHttpd, Options.name, Options.alias
if Options.version
console.log robot.version
process.exit 0
loadScripts = ->
scriptsPath = Path.resolve ".", "scripts"
robot.load scriptsPath
scriptsPath = Path.resolve ".", "src", "scripts"
robot.load scriptsPath
hubotScripts = Path.resolve ".", "hubot-scripts.json"
if Fs.existsSync(hubotScripts)
data = Fs.readFileSync(hubotScripts)
if data.length > 0
try
scripts = JSON.parse data
scriptsPath = Path.resolve "node_modules", "hubot-scripts", "src", "scripts"
robot.loadHubotScripts scriptsPath, scripts
catch err
console.error "Error parsing JSON data from hubot-scripts.json: #{err}"
process.exit(1)
externalScripts = Path.resolve ".", "external-scripts.json"
if Fs.existsSync(externalScripts)
Fs.readFile externalScripts, (err, data) ->
if data.length > 0
try
scripts = JSON.parse data
catch err
console.error "Error parsing JSON data from external-scripts.json: #{err}"
process.exit(1)
robot.loadExternalScripts scripts
for path in Options.scripts
if path[0] == '/'
scriptsPath = path
else
scriptsPath = Path.resolve ".", path
robot.load scriptsPath
if Options.configCheck
loadScripts()
console.log "OK"
process.exit 0
robot.adapter.once 'connected', loadScripts
robot.run()