Skip to content

Commit 27243f9

Browse files
committed
bin/hubot.js
1 parent b98a876 commit 27243f9

File tree

2 files changed

+219
-165
lines changed

2 files changed

+219
-165
lines changed

bin/hubot

Lines changed: 6 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,9 @@
11
#!/usr/bin/env coffee
2-
# vim:ft=coffee ts=2 sw=2 et :
3-
# -*- mode:coffee -*-
42

5-
Hubot = require '..'
3+
# While all other files have been converted to JavaScript via https://github.com/github/hubot/pull/1347,
4+
# we left the `bin/hubot` file to remain in CoffeeScript in order prevent
5+
# breaking existing 3rd party adapters of which some are still written in
6+
# CoffeeScript themselves. We will depracate and eventually remove this file
7+
# in a future version of hubot
68

7-
Fs = require 'fs'
8-
OptParse = require 'optparse'
9-
Path = require 'path'
10-
11-
Switches = [
12-
[ "-a", "--adapter ADAPTER", "The Adapter to use" ],
13-
[ "-c", "--create PATH", "Create a deployable hubot" ],
14-
[ "-d", "--disable-httpd", "Disable the HTTP server" ],
15-
[ "-h", "--help", "Display the help information" ],
16-
[ "-l", "--alias ALIAS", "Enable replacing the robot's name with alias" ],
17-
[ "-n", "--name NAME", "The name of the robot in chat" ],
18-
[ "-r", "--require PATH", "Alternative scripts path" ],
19-
[ "-t", "--config-check", "Test hubot's config to make sure it won't fail at startup"]
20-
[ "-v", "--version", "Displays the version of hubot installed" ]
21-
]
22-
23-
Options =
24-
adapter: process.env.HUBOT_ADAPTER or "shell"
25-
alias: process.env.HUBOT_ALIAS or false
26-
create: process.env.HUBOT_CREATE or false
27-
enableHttpd: process.env.HUBOT_HTTPD or true
28-
scripts: process.env.HUBOT_SCRIPTS or []
29-
name: process.env.HUBOT_NAME or "Hubot"
30-
path: process.env.HUBOT_PATH or "."
31-
configCheck: false
32-
33-
Parser = new OptParse.OptionParser(Switches)
34-
Parser.banner = "Usage hubot [options]"
35-
36-
Parser.on "adapter", (opt, value) ->
37-
Options.adapter = value
38-
39-
Parser.on "create", (opt, value) ->
40-
Options.path = value
41-
Options.create = true
42-
43-
Parser.on "disable-httpd", (opt) ->
44-
Options.enableHttpd = false
45-
46-
Parser.on "help", (opt, value) ->
47-
console.log Parser.toString()
48-
process.exit 0
49-
50-
Parser.on "alias", (opt, value) ->
51-
value or= '/'
52-
Options.alias = value
53-
54-
Parser.on "name", (opt, value) ->
55-
Options.name = value
56-
57-
Parser.on "require", (opt, value) ->
58-
Options.scripts.push(value)
59-
60-
Parser.on "config-check", (opt) ->
61-
Options.configCheck = true
62-
63-
Parser.on "version", (opt, value) ->
64-
Options.version = true
65-
66-
Parser.on (opt, value) ->
67-
console.warn "Unknown option: #{opt}"
68-
69-
Parser.parse process.argv
70-
71-
unless process.platform is "win32"
72-
process.on 'SIGTERM', ->
73-
process.exit 0
74-
75-
if Options.create
76-
console.error "'hubot --create' is deprecated. Use the yeoman generator instead:"
77-
console.error " npm install -g yo generator-hubot"
78-
console.error " mkdir -p #{Options.path}"
79-
console.error " cd #{Options.path}"
80-
console.error " yo hubot"
81-
console.error "See https://github.com/github/hubot/blob/master/docs/index.md for more details on getting started."
82-
process.exit 1
83-
84-
else
85-
robot = Hubot.loadBot undefined, Options.adapter, Options.enableHttpd, Options.name, Options.alias
86-
87-
if Options.version
88-
console.log robot.version
89-
process.exit 0
90-
91-
loadScripts = ->
92-
scriptsPath = Path.resolve ".", "scripts"
93-
robot.load scriptsPath
94-
95-
scriptsPath = Path.resolve ".", "src", "scripts"
96-
robot.load scriptsPath
97-
98-
hubotScripts = Path.resolve ".", "hubot-scripts.json"
99-
if Fs.existsSync(hubotScripts)
100-
data = Fs.readFileSync(hubotScripts)
101-
if data.length > 0
102-
try
103-
scripts = JSON.parse data
104-
scriptsPath = Path.resolve "node_modules", "hubot-scripts", "src", "scripts"
105-
robot.loadHubotScripts scriptsPath, scripts
106-
catch err
107-
robot.logger.error "Error parsing JSON data from hubot-scripts.json: #{err}"
108-
process.exit(1)
109-
110-
hubotScriptsWarning = "Loading scripts from hubot-scripts.json is deprecated and " +
111-
"will be removed in 3.0 (https://github.com/github/hubot-scripts/issues/1113) " +
112-
"in favor of packages for each script.\n\n"
113-
114-
if scripts.length is 0
115-
hubotScriptsWarning += "Your hubot-scripts.json is empty, so you just need to remove it."
116-
else
117-
hubotScriptsReplacements = Path.resolve "node_modules", "hubot-scripts", "replacements.json"
118-
119-
if Fs.existsSync(hubotScriptsReplacements)
120-
hubotScriptsWarning += "The following scripts have known replacements. Follow the link for installation instructions, then remove it from hubot-scripts.json:\n"
121-
122-
replacementsData = Fs.readFileSync(hubotScriptsReplacements)
123-
replacements = JSON.parse(replacementsData)
124-
scriptsWithoutReplacements = []
125-
for script in scripts
126-
replacement = replacements[script]
127-
if replacement
128-
hubotScriptsWarning += "* #{script}: #{replacement}\n"
129-
else
130-
scriptsWithoutReplacements.push(script)
131-
hubotScriptsWarning += "\n"
132-
133-
if scriptsWithoutReplacements.length > 0
134-
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"
135-
hubotScriptsWarning += "* #{script}\n" for script in scriptsWithoutReplacements
136-
137-
hubotScriptsWarning += "\nYou an also try updating hubot-scripts to get the latest list of replacements: npm install --save hubot-scripts@latest"
138-
else
139-
hubotScriptsWarning += "To get a list of recommended replacements, update your hubot-scripts: npm install --save hubot-scripts@latest"
140-
141-
robot.logger.warning hubotScriptsWarning
142-
143-
externalScripts = Path.resolve ".", "external-scripts.json"
144-
if Fs.existsSync(externalScripts)
145-
Fs.readFile externalScripts, (err, data) ->
146-
if data.length > 0
147-
try
148-
scripts = JSON.parse data
149-
catch err
150-
console.error "Error parsing JSON data from external-scripts.json: #{err}"
151-
process.exit(1)
152-
robot.loadExternalScripts scripts
153-
154-
for path in Options.scripts
155-
if path[0] == '/'
156-
scriptsPath = path
157-
else
158-
scriptsPath = Path.resolve ".", path
159-
robot.load scriptsPath
160-
161-
if Options.configCheck
162-
loadScripts()
163-
console.log "OK"
164-
process.exit 0
165-
166-
robot.adapter.once 'connected', loadScripts
167-
168-
robot.run()
9+
require './hubot.js'

bin/hubot.js

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const pathResolve = require('path').resolve
5+
6+
const OptParse = require('optparse')
7+
8+
const Hubot = require('..')
9+
10+
const switches = [
11+
['-a', '--adapter ADAPTER', 'The Adapter to use'],
12+
['-c', '--create PATH', 'Create a deployable hubot'],
13+
['-d', '--disable-httpd', 'Disable the HTTP server'],
14+
['-h', '--help', 'Display the help information'],
15+
['-l', '--alias ALIAS', "Enable replacing the robot's name with alias"],
16+
['-n', '--name NAME', 'The name of the robot in chat'],
17+
['-r', '--require PATH', 'Alternative scripts path'],
18+
['-t', '--config-check', "Test hubot's config to make sure it won't fail at startup"],
19+
['-v', '--version', 'Displays the version of hubot installed']
20+
]
21+
22+
const options = {
23+
adapter: process.env.HUBOT_ADAPTER || 'shell',
24+
alias: process.env.HUBOT_ALIAS || false,
25+
create: process.env.HUBOT_CREATE || false,
26+
enableHttpd: process.env.HUBOT_HTTPD || true,
27+
scripts: process.env.HUBOT_SCRIPTS || [],
28+
name: process.env.HUBOT_NAME || 'Hubot',
29+
path: process.env.HUBOT_PATH || '.',
30+
configCheck: false
31+
}
32+
33+
const Parser = new OptParse.OptionParser(switches)
34+
Parser.banner = 'Usage hubot [options]'
35+
36+
Parser.on('adapter', (opt, value) => {
37+
options.adapter = value
38+
})
39+
40+
Parser.on('create', function (opt, value) {
41+
options.path = value
42+
options.create = true
43+
})
44+
45+
Parser.on('disable-httpd', opt => {
46+
options.enableHttpd = false
47+
})
48+
49+
Parser.on('help', function (opt, value) {
50+
console.log(Parser.toString())
51+
return process.exit(0)
52+
})
53+
54+
Parser.on('alias', function (opt, value) {
55+
if (!value) {
56+
value = '/'
57+
}
58+
options.alias = value
59+
})
60+
61+
Parser.on('name', (opt, value) => {
62+
options.name = value
63+
})
64+
65+
Parser.on('require', (opt, value) => {
66+
options.scripts.push(value)
67+
})
68+
69+
Parser.on('config-check', opt => {
70+
options.configCheck = true
71+
})
72+
73+
Parser.on('version', (opt, value) => {
74+
options.version = true
75+
})
76+
77+
Parser.on((opt, value) => {
78+
console.warn(`Unknown option: ${opt}`)
79+
})
80+
81+
Parser.parse(process.argv)
82+
83+
if (process.platform !== 'win32') {
84+
process.on('SIGTERM', () => process.exit(0))
85+
}
86+
87+
if (options.create) {
88+
console.error("'hubot --create' is deprecated. Use the yeoman generator instead:")
89+
console.error(' npm install -g yo generator-hubot')
90+
console.error(` mkdir -p ${options.path}`)
91+
console.error(` cd ${options.path}`)
92+
console.error(' yo hubot')
93+
console.error('See https://github.com/github/hubot/blob/master/docs/index.md for more details on getting started.')
94+
process.exit(1)
95+
}
96+
97+
const robot = Hubot.loadBot(undefined, options.adapter, options.enableHttpd, options.name, options.alias)
98+
99+
if (options.version) {
100+
console.log(robot.version)
101+
process.exit(0)
102+
}
103+
104+
if (options.configCheck) {
105+
loadScripts()
106+
console.log('OK')
107+
process.exit(0)
108+
}
109+
110+
robot.adapter.once('connected', loadScripts)
111+
112+
robot.run()
113+
114+
function loadScripts () {
115+
robot.load(pathResolve('.', 'scripts'))
116+
robot.load(pathResolve('.', 'src', 'scripts'))
117+
118+
loadHubotScripts()
119+
loadExternalScripts()
120+
121+
options.scripts.forEach((scriptPath) => {
122+
if (scriptPath[0] === '/') {
123+
return robot.load(scriptPath)
124+
}
125+
126+
robot.load(pathResolve('.', scriptPath))
127+
})
128+
}
129+
130+
function loadHubotScripts () {
131+
const hubotScripts = pathResolve('.', 'hubot-scripts.json')
132+
let scripts
133+
let scriptsPath
134+
135+
if (fs.existsSync(hubotScripts)) {
136+
let hubotScriptsWarning
137+
const data = fs.readFileSync(hubotScripts)
138+
139+
if (data.length === 0) {
140+
return
141+
}
142+
143+
try {
144+
scripts = JSON.parse(data)
145+
scriptsPath = pathResolve('node_modules', 'hubot-scripts', 'src', 'scripts')
146+
robot.loadHubotScripts(scriptsPath, scripts)
147+
} catch (error) {
148+
const err = error
149+
robot.logger.error(`Error parsing JSON data from hubot-scripts.json: ${err}`)
150+
process.exit(1)
151+
}
152+
153+
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'
154+
155+
if (scripts.length === 0) {
156+
hubotScriptsWarning += 'Your hubot-scripts.json is empty, so you just need to remove it.'
157+
return robot.logger.warning(hubotScriptsWarning)
158+
}
159+
160+
const hubotScriptsReplacements = pathResolve('node_modules', 'hubot-scripts', 'replacements.json')
161+
const replacementsData = fs.readFileSync(hubotScriptsReplacements)
162+
const replacements = JSON.parse(replacementsData)
163+
const scriptsWithoutReplacements = []
164+
165+
if (!fs.existsSync(hubotScriptsReplacements)) {
166+
hubotScriptsWarning += 'To get a list of recommended replacements, update your hubot-scripts: npm install --save hubot-scripts@latest'
167+
return robot.logger.warning(hubotScriptsWarning)
168+
}
169+
170+
hubotScriptsWarning += 'The following scripts have known replacements. Follow the link for installation instructions, then remove it from hubot-scripts.json:\n'
171+
172+
scripts.forEach((script) => {
173+
const replacement = replacements[script]
174+
175+
if (replacement) {
176+
hubotScriptsWarning += `* ${script}: ${replacement}\n`
177+
} else {
178+
scriptsWithoutReplacements.push(script)
179+
}
180+
})
181+
182+
hubotScriptsWarning += '\n'
183+
184+
if (scriptsWithoutReplacements.length > 0) {
185+
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'
186+
hubotScriptsWarning += scriptsWithoutReplacements.map((script) => `* ${script}\n`).join('')
187+
hubotScriptsWarning += '\nYou an also try updating hubot-scripts to get the latest list of replacements: npm install --save hubot-scripts@latest'
188+
}
189+
190+
robot.logger.warning(hubotScriptsWarning)
191+
}
192+
}
193+
194+
function loadExternalScripts () {
195+
const externalScripts = pathResolve('.', 'external-scripts.json')
196+
197+
if (!fs.existsSync(externalScripts)) {
198+
return
199+
}
200+
201+
fs.readFile(externalScripts, function (error, data) {
202+
if (error) {
203+
throw error
204+
}
205+
206+
try {
207+
robot.loadExternalScripts(JSON.parse(data))
208+
} catch (error) {
209+
console.error(`Error parsing JSON data from external-scripts.json: ${error}`)
210+
process.exit(1)
211+
}
212+
})
213+
}

0 commit comments

Comments
 (0)