Skip to content

Commit

Permalink
Merge pull request hubotio#732 from github/config-check
Browse files Browse the repository at this point in the history
Add support for --config-check for checking that the scripts are working
  • Loading branch information
technicalpickles committed Jul 24, 2014
2 parents 39681ea + e678459 commit 0754635
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
50 changes: 29 additions & 21 deletions bin/hubot
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Switches = [
[ "-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" ]
]

Expand All @@ -28,6 +29,7 @@ Options =
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]"
Expand Down Expand Up @@ -56,6 +58,9 @@ Parser.on "name", (opt, 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

Expand Down Expand Up @@ -88,29 +93,27 @@ else
robot.load scriptsPath

hubotScripts = Path.resolve ".", "hubot-scripts.json"
Fs.exists hubotScripts, (exists) ->
if exists
Fs.readFile hubotScripts, (err, data) ->
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)
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"
Fs.exists externalScripts, (exists) ->
if exists
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
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] == '/'
Expand All @@ -119,6 +122,11 @@ else
scriptsPath = Path.resolve ".", path
robot.load scriptsPath

if Options.configCheck
loadScripts()
console.log "OK"
process.exit 0

robot.adapter.on 'connected', loadScripts

robot.run()
8 changes: 4 additions & 4 deletions src/robot.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ class Robot
# Returns nothing.
load: (path) ->
@logger.debug "Loading scripts from #{path}"
Fs.exists path, (exists) =>
if exists
for file in Fs.readdirSync(path).sort()
@loadFile path, file

if Fs.existsSync(path)
for file in Fs.readdirSync(path).sort()
@loadFile path, file

# Public: Load scripts specfied in the `hubot-scripts.json` file.
#
Expand Down
3 changes: 2 additions & 1 deletion src/templates/bin/hubot
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/sh

set -e

npm install
export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"

exec node_modules/.bin/hubot "$@"

0 comments on commit 0754635

Please sign in to comment.