Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 474b56a

Browse files
author
Tom Bell
committed
Merge pull request #959 from spajus/script_reloading
Hubot script reloading without restart
2 parents 62f147a + a511bd5 commit 474b56a

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

src/scripts/reload.coffee

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Description:
2+
# Allows Hubot to (re)load scripts without restart
3+
#
4+
# Dependencies:
5+
# None
6+
#
7+
# Configuration:
8+
# None
9+
#
10+
# Commands:
11+
# hubot reload all scripts - Reloads scripts without restart. Loads new scripts too.
12+
# hubot command count - Tells how many commands hubot knows
13+
#
14+
# Author:
15+
# spajus
16+
17+
Fs = require 'fs'
18+
Path = require 'path'
19+
20+
oldCommands = null
21+
oldListeners = null
22+
23+
module.exports = (robot) ->
24+
25+
robot.hear /command count/i, (msg) ->
26+
msg.send "I am aware of #{msg.robot.commands.length} commands"
27+
28+
robot.respond /reload all scripts/i, (msg) ->
29+
try
30+
oldCommands = robot.commands
31+
oldListeners = robot.listeners
32+
33+
robot.commands = []
34+
robot.listeners = []
35+
36+
reloadAllScripts msg, success, (err) ->
37+
msg.send err
38+
catch error
39+
console.log "Hubot reloader:", error
40+
msg.send "Could not reload all scripts: #{error}"
41+
42+
success = (msg) ->
43+
# Cleanup old listeners and help
44+
for listener in oldListeners
45+
listener = {}
46+
oldListeners = null
47+
oldCommands = null
48+
msg.send "Reloaded all scripts"
49+
50+
reloadAllScripts = (msg, success, error) ->
51+
robot = msg.robot
52+
scriptsPath = Path.resolve ".", "scripts"
53+
robot.load scriptsPath
54+
55+
scriptsPath = Path.resolve ".", "src", "scripts"
56+
robot.load scriptsPath
57+
58+
hubotScripts = Path.resolve ".", "hubot-scripts.json"
59+
Fs.exists hubotScripts, (exists) ->
60+
if exists
61+
Fs.readFile hubotScripts, (err, data) ->
62+
if data.length > 0
63+
try
64+
scripts = JSON.parse data
65+
scriptsPath = Path.resolve "node_modules", "hubot-scripts", "src", "scripts"
66+
robot.loadHubotScripts scriptsPath, scripts
67+
catch err
68+
error "Error parsing JSON data from hubot-scripts.json: #{err}"
69+
return
70+
71+
externalScripts = Path.resolve ".", "external-scripts.json"
72+
Fs.exists externalScripts, (exists) ->
73+
if exists
74+
Fs.readFile externalScripts, (err, data) ->
75+
if data.length > 0
76+
try
77+
scripts = JSON.parse data
78+
catch err
79+
error "Error parsing JSON data from external-scripts.json: #{err}"
80+
robot.loadExternalScripts scripts
81+
return
82+
success(msg)
83+

0 commit comments

Comments
 (0)