This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
/
reload.coffee
83 lines (70 loc) · 2.16 KB
/
reload.coffee
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
# Description:
# Allows Hubot to (re)load scripts without restart
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot reload all scripts - Reloads scripts without restart. Loads new scripts too.
# hubot command count - Tells how many commands hubot knows
#
# Author:
# spajus
Fs = require 'fs'
Path = require 'path'
oldCommands = null
oldListeners = null
module.exports = (robot) ->
robot.hear /command count/i, (msg) ->
msg.send "I am aware of #{msg.robot.commands.length} commands"
robot.respond /reload all scripts/i, (msg) ->
try
oldCommands = robot.commands
oldListeners = robot.listeners
robot.commands = []
robot.listeners = []
reloadAllScripts msg, success, (err) ->
msg.send err
catch error
console.log "Hubot reloader:", error
msg.send "Could not reload all scripts: #{error}"
success = (msg) ->
# Cleanup old listeners and help
for listener in oldListeners
listener = {}
oldListeners = null
oldCommands = null
msg.send "Reloaded all scripts"
reloadAllScripts = (msg, success, error) ->
robot = msg.robot
scriptsPath = Path.resolve ".", "scripts"
robot.load scriptsPath
scriptsPath = Path.resolve ".", "src", "scripts"
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
error "Error parsing JSON data from hubot-scripts.json: #{err}"
return
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
error "Error parsing JSON data from external-scripts.json: #{err}"
robot.loadExternalScripts scripts
return
success(msg)