Skip to content
Permalink
Browse files

refactor: CoffeeScript -> JavaScript

  • Loading branch information...
gr2m committed Jun 1, 2017
1 parent ab527de commit 7939b8f08a0a4fbbb685cfd67c706ca2d2594002
Showing with 2,378 additions and 1,911 deletions.
  1. +6 −165 bin/hubot
  2. +213 −0 bin/hubot.js
  3. +29 −0 es2015.js
  4. +0 −26 index.coffee
  5. +39 −0 index.js
  6. +0 −117 src/adapter.coffee
  7. +132 −0 src/adapter.js
  8. +0 −308 src/adapters/campfire.coffee
  9. +394 −0 src/adapters/campfire.js
  10. +0 −105 src/adapters/shell.coffee
  11. +135 −0 src/adapters/shell.js
  12. +0 −159 src/brain.coffee
  13. +198 −0 src/brain.js
  14. +0 −110 src/listener.coffee
  15. +134 −0 src/listener.js
  16. +0 −72 src/message.coffee
  17. +88 −0 src/message.js
  18. +0 −71 src/middleware.coffee
  19. +86 −0 src/middleware.js
  20. +0 −111 src/response.coffee
  21. +150 −0 src/response.js
  22. +0 −656 src/robot.coffee
  23. +749 −0 src/robot.js
  24. +0 −11 src/user.coffee
  25. +25 −0 src/user.js
171 bin/hubot
@@ -1,168 +1,9 @@
#!/usr/bin/env coffee
# vim:ft=coffee ts=2 sw=2 et :
# -*- mode:coffee -*-

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

Fs = require 'fs'
OptParse = require 'optparse'
Path = require 'path'

Switches = [
[ "-a", "--adapter ADAPTER", "The Adapter to use" ],
[ "-c", "--create PATH", "Create a deployable hubot" ],
[ "-d", "--disable-httpd", "Disable the HTTP server" ],
[ "-h", "--help", "Display the help information" ],
[ "-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" ]
]

Options =
adapter: process.env.HUBOT_ADAPTER or "shell"
alias: process.env.HUBOT_ALIAS or false
create: process.env.HUBOT_CREATE or false
enableHttpd: process.env.HUBOT_HTTPD or true
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]"

Parser.on "adapter", (opt, value) ->
Options.adapter = value

Parser.on "create", (opt, value) ->
Options.path = value
Options.create = true

Parser.on "disable-httpd", (opt) ->
Options.enableHttpd = false

Parser.on "help", (opt, value) ->
console.log Parser.toString()
process.exit 0

Parser.on "alias", (opt, value) ->
value or= '/'
Options.alias = value

Parser.on "name", (opt, value) ->
Options.name = 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

Parser.on (opt, value) ->
console.warn "Unknown option: #{opt}"

Parser.parse process.argv

unless process.platform is "win32"
process.on 'SIGTERM', ->
process.exit 0

if Options.create
console.error "'hubot --create' is deprecated. Use the yeoman generator instead:"
console.error " npm install -g yo generator-hubot"
console.error " mkdir -p #{Options.path}"
console.error " cd #{Options.path}"
console.error " yo hubot"
console.error "See https://github.com/github/hubot/blob/master/docs/index.md for more details on getting started."
process.exit 1

else
robot = Hubot.loadBot undefined, Options.adapter, Options.enableHttpd, Options.name, Options.alias

if Options.version
console.log robot.version
process.exit 0

loadScripts = ->
scriptsPath = Path.resolve ".", "scripts"
robot.load scriptsPath

scriptsPath = Path.resolve ".", "src", "scripts"
robot.load scriptsPath

hubotScripts = Path.resolve ".", "hubot-scripts.json"
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
robot.logger.error "Error parsing JSON data from hubot-scripts.json: #{err}"
process.exit(1)

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"

if scripts.length is 0
hubotScriptsWarning += "Your hubot-scripts.json is empty, so you just need to remove it."
else
hubotScriptsReplacements = Path.resolve "node_modules", "hubot-scripts", "replacements.json"

if Fs.existsSync(hubotScriptsReplacements)
hubotScriptsWarning += "The following scripts have known replacements. Follow the link for installation instructions, then remove it from hubot-scripts.json:\n"

replacementsData = Fs.readFileSync(hubotScriptsReplacements)
replacements = JSON.parse(replacementsData)
scriptsWithoutReplacements = []
for script in scripts
replacement = replacements[script]
if replacement
hubotScriptsWarning += "* #{script}: #{replacement}\n"
else
scriptsWithoutReplacements.push(script)
hubotScriptsWarning += "\n"

if scriptsWithoutReplacements.length > 0
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"
hubotScriptsWarning += "* #{script}\n" for script in scriptsWithoutReplacements

hubotScriptsWarning += "\nYou an also try updating hubot-scripts to get the latest list of replacements: npm install --save hubot-scripts@latest"
else
hubotScriptsWarning += "To get a list of recommended replacements, update your hubot-scripts: npm install --save hubot-scripts@latest"

robot.logger.warning hubotScriptsWarning

externalScripts = Path.resolve ".", "external-scripts.json"
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] == '/'
scriptsPath = path
else
scriptsPath = Path.resolve ".", path
robot.load scriptsPath

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

robot.adapter.once 'connected', loadScripts

robot.run()
require './hubot.js'
@@ -0,0 +1,213 @@
'use strict'

const fs = require('fs')
const pathResolve = require('path').resolve

const OptParse = require('optparse')

const Hubot = require('..')

const switches = [
['-a', '--adapter ADAPTER', 'The Adapter to use'],
['-c', '--create PATH', 'Create a deployable hubot'],
['-d', '--disable-httpd', 'Disable the HTTP server'],
['-h', '--help', 'Display the help information'],
['-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']
]

const options = {
adapter: process.env.HUBOT_ADAPTER || 'shell',
alias: process.env.HUBOT_ALIAS || false,
create: process.env.HUBOT_CREATE || false,
enableHttpd: process.env.HUBOT_HTTPD || true,
scripts: process.env.HUBOT_SCRIPTS || [],
name: process.env.HUBOT_NAME || 'Hubot',
path: process.env.HUBOT_PATH || '.',
configCheck: false
}

const Parser = new OptParse.OptionParser(switches)
Parser.banner = 'Usage hubot [options]'

Parser.on('adapter', (opt, value) => {
options.adapter = value
})

Parser.on('create', function (opt, value) {
options.path = value
options.create = true
})

Parser.on('disable-httpd', opt => {
options.enableHttpd = false
})

Parser.on('help', function (opt, value) {
console.log(Parser.toString())
return process.exit(0)
})

Parser.on('alias', function (opt, value) {
if (!value) {
value = '/'
}
options.alias = value
})

Parser.on('name', (opt, value) => {
options.name = 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
})

Parser.on((opt, value) => {
console.warn(`Unknown option: ${opt}`)
})

Parser.parse(process.argv)

if (process.platform !== 'win32') {
process.on('SIGTERM', () => process.exit(0))
}

if (options.create) {
console.error("'hubot --create' is deprecated. Use the yeoman generator instead:")
console.error(' npm install -g yo generator-hubot')
console.error(` mkdir -p ${options.path}`)
console.error(` cd ${options.path}`)
console.error(' yo hubot')
console.error('See https://github.com/github/hubot/blob/master/docs/index.md for more details on getting started.')
process.exit(1)
}

const robot = Hubot.loadBot(undefined, options.adapter, options.enableHttpd, options.name, options.alias)

if (options.version) {
console.log(robot.version)
process.exit(0)
}

if (options.configCheck) {
loadScripts()
console.log('OK')
process.exit(0)
}

robot.adapter.once('connected', loadScripts)

robot.run()

function loadScripts () {
robot.load(pathResolve('.', 'scripts'))
robot.load(pathResolve('.', 'src', 'scripts'))

loadHubotScripts()
loadExternalScripts()

options.scripts.forEach((scriptPath) => {
if (scriptPath[0] === '/') {
return robot.load(scriptPath)
}

robot.load(pathResolve('.', scriptPath))
})
}

function loadHubotScripts () {
const hubotScripts = pathResolve('.', 'hubot-scripts.json')
let scripts
let scriptsPath

if (fs.existsSync(hubotScripts)) {
let hubotScriptsWarning
const data = fs.readFileSync(hubotScripts)

if (data.length === 0) {
return
}

try {
scripts = JSON.parse(data)
scriptsPath = pathResolve('node_modules', 'hubot-scripts', 'src', 'scripts')
robot.loadHubotScripts(scriptsPath, scripts)
} catch (error) {
const err = error
robot.logger.error(`Error parsing JSON data from hubot-scripts.json: ${err}`)
process.exit(1)
}

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'

if (scripts.length === 0) {
hubotScriptsWarning += 'Your hubot-scripts.json is empty, so you just need to remove it.'
return robot.logger.warning(hubotScriptsWarning)
}

const hubotScriptsReplacements = pathResolve('node_modules', 'hubot-scripts', 'replacements.json')
const replacementsData = fs.readFileSync(hubotScriptsReplacements)
const replacements = JSON.parse(replacementsData)
const scriptsWithoutReplacements = []

if (!fs.existsSync(hubotScriptsReplacements)) {
hubotScriptsWarning += 'To get a list of recommended replacements, update your hubot-scripts: npm install --save hubot-scripts@latest'
return robot.logger.warning(hubotScriptsWarning)
}

hubotScriptsWarning += 'The following scripts have known replacements. Follow the link for installation instructions, then remove it from hubot-scripts.json:\n'

scripts.forEach((script) => {
const replacement = replacements[script]

if (replacement) {
hubotScriptsWarning += `* ${script}: ${replacement}\n`
} else {
scriptsWithoutReplacements.push(script)
}
})

hubotScriptsWarning += '\n'

if (scriptsWithoutReplacements.length > 0) {
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'
hubotScriptsWarning += scriptsWithoutReplacements.map((script) => `* ${script}\n`).join('')
hubotScriptsWarning += '\nYou an also try updating hubot-scripts to get the latest list of replacements: npm install --save hubot-scripts@latest'
}

robot.logger.warning(hubotScriptsWarning)
}
}

function loadExternalScripts () {
const externalScripts = pathResolve('.', 'external-scripts.json')

if (!fs.existsSync(externalScripts)) {
return
}

fs.readFile(externalScripts, function (error, data) {
if (error) {
throw error
}

try {
robot.loadExternalScripts(JSON.parse(data))
} catch (error) {
console.error(`Error parsing JSON data from external-scripts.json: ${error}`)
process.exit(1)
}
})
}
Oops, something went wrong.

0 comments on commit 7939b8f

Please sign in to comment.
You can’t perform that action at this time.