Repository navigation
Expand file tree
/
Copy pathhubot.js
More file actions
213 lines (167 loc) · 6.27 KB
/
Copy pathhubot.js
File metadata and controls
213 lines (167 loc) · 6.27 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict'
const fs = require('fs')
const pathResolve = require('path').resolve
const OptParse = require('optparse')
const Hubot = require('..')
const 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']
]
const options = {
adapter: process.env.HUBOT_ADAPTER || 'shell',
alias: process.env.HUBOT_ALIAS || false,
create: process.env.HUBOT_CREATE || false,
enableHttpd: process.env.HUBOT_HTTPD || true,
scripts: process.env.HUBOT_SCRIPTS || [],
name: process.env.HUBOT_NAME || 'Hubot',
path: process.env.HUBOT_PATH || '.',
configCheck: false
}
const Parser = new OptParse.OptionParser(switches)
Parser.banner = 'Usage hubot [options]'
Parser.on('adapter', (opt, value) => {
options.adapter = value
})
Parser.on('create', function (opt, value) {
options.path = value
options.create = true
})
Parser.on('disable-httpd', opt => {
options.enableHttpd = false
})
Parser.on('help', function (opt, value) {
console.log(Parser.toString())
return process.exit(0)
})
Parser.on('alias', function (opt, value) {
if (!value) {
value = '/'
}
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)
if (process.platform !== '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)
}
const robot = Hubot.loadBot(undefined, options.adapter, options.enableHttpd, options.name, options.alias)
if (options.version) {
console.log(robot.version)
process.exit(0)
}
if (options.configCheck) {
loadScripts()
console.log('OK')
process.exit(0)
}
robot.adapter.once('connected', loadScripts)
robot.run()
function loadScripts () {
robot.load(pathResolve('.', 'scripts'))
robot.load(pathResolve('.', 'src', 'scripts'))
loadHubotScripts()
loadExternalScripts()
options.scripts.forEach((scriptPath) => {
if (scriptPath[0] === '/') {
return robot.load(scriptPath)
}
robot.load(pathResolve('.', scriptPath))
})
}
function loadHubotScripts () {
const hubotScripts = pathResolve('.', 'hubot-scripts.json')
let scripts
let scriptsPath
if (fs.existsSync(hubotScripts)) {
let hubotScriptsWarning
const data = fs.readFileSync(hubotScripts)
if (data.length === 0) {
return
}
try {
scripts = JSON.parse(data)
scriptsPath = pathResolve('node_modules', 'hubot-scripts', 'src', 'scripts')
robot.loadHubotScripts(scriptsPath, scripts)
} catch (error) {
const err = error
robot.logger.error(`Error parsing JSON data from hubot-scripts.json: ${err}`)
process.exit(1)
}
hubotScriptsWarning = 'Loading scripts from hubot-scripts.json is deprecated and ' + 'will be removed in 3.0 (https://github.com/github/hubot-scripts/issues/1113) ' + 'in favor of packages for each script.\n\n'
if (scripts.length === 0) {
hubotScriptsWarning += 'Your hubot-scripts.json is empty, so you just need to remove it.'
return robot.logger.warning(hubotScriptsWarning)
}
const hubotScriptsReplacements = pathResolve('node_modules', 'hubot-scripts', 'replacements.json')
const replacementsData = fs.readFileSync(hubotScriptsReplacements)
const replacements = JSON.parse(replacementsData)
const scriptsWithoutReplacements = []
if (!fs.existsSync(hubotScriptsReplacements)) {
hubotScriptsWarning += 'To get a list of recommended replacements, update your hubot-scripts: npm install --save hubot-scripts@latest'
return robot.logger.warning(hubotScriptsWarning)
}
hubotScriptsWarning += 'The following scripts have known replacements. Follow the link for installation instructions, then remove it from hubot-scripts.json:\n'
scripts.forEach((script) => {
const replacement = replacements[script]
if (replacement) {
hubotScriptsWarning += `* ${script}: ${replacement}\n`
} else {
scriptsWithoutReplacements.push(script)
}
})
hubotScriptsWarning += '\n'
if (scriptsWithoutReplacements.length > 0) {
hubotScriptsWarning += 'The following scripts don’t have (known) replacements. You can try searching https://www.npmjs.com/ or http://github.com/search or your favorite search engine. You can copy the script into your local scripts directory, or consider creating a new package to maintain yourself. If you find a replacement or create a package yourself, please post on https://github.com/github/hubot-scripts/issues/1641:\n'
hubotScriptsWarning += scriptsWithoutReplacements.map((script) => `* ${script}\n`).join('')
hubotScriptsWarning += '\nYou an also try updating hubot-scripts to get the latest list of replacements: npm install --save hubot-scripts@latest'
}
robot.logger.warning(hubotScriptsWarning)
}
}
function loadExternalScripts () {
const externalScripts = pathResolve('.', 'external-scripts.json')
if (!fs.existsSync(externalScripts)) {
return
}
fs.readFile(externalScripts, function (error, data) {
if (error) {
throw error
}
try {
robot.loadExternalScripts(JSON.parse(data))
} catch (error) {
console.error(`Error parsing JSON data from external-scripts.json: ${error}`)
process.exit(1)
}
})
}