Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 69 lines (62 sloc) 2.672 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
# Description:
# Allows hubot to update itself using git pull and npm update.
# If updates are downloaded you'll need to restart hubot, for example using "hubot die" (restart using a watcher like forever.js).
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot update - Performs a git pull and npm udate.
# hubot pending update - Informs if there are pending updates (hubot needs a restart)
#
# Author:
# benjamine

child_process = require 'child_process'
downloaded_updates = false

module.exports = (robot) ->

    robot.respond /pending updates?\??$/i, (msg) ->
        if downloaded_updates
            msg.send "I have some pending updates, KILL ME PLEASE! (hint: hubot die)"
        else
            msg.send "I'm up-to-date!"

    robot.respond /update( yourself)?$/i, (msg) ->
        changes = false
        try
            msg.send "git pull..."
            child_process.exec 'git pull', (error, stdout, stderr) ->
                if error
                    msg.send "git pull failed: " + stderr
                else
                    output = stdout+''
                    if not /Already up\-to\-date/.test output
                        msg.send "my source code changed:\n" + output
                        changes = true
                    else
                        msg.send "my source code is up-to-date"
                try
                    msg.send "npm update..."
                    child_process.exec 'npm update', (error, stdout, stderr) ->
                        if error
                            msg.send "npm update failed: " + stderr
                        else
                            output = stdout+''
                            if /node_modules/.test output
                                msg.send "some dependencies updated:\n" + output
                                changes = true
                            else
                                msg.send "all dependencies are up-to-date"
                        if changes
                            downloaded_updates = true
                            msg.send "I downloaded some updates, KILL ME PLEASE! (hint: hubot die)"
                        else
                            if downloaded_updates
                                msg.send "I have some pending updates, KILL ME PLEASE! (hint: hubot die)"
                            else
                                msg.send "I'm up-to-date!"
                catch error
                    msg.send "npm update failed: " + error
        catch error
            msg.send "git pull failed: " + error


Something went wrong with that request. Please try again.