#!/usr/bin/env coffee # vim:ft=coffee ts=2 sw=2 et : # -*- mode:coffee -*- Q = require 'q' Brobbot = require '..' Fs = require 'fs' OptParse = require 'optparse' Path = require 'path' Switches = [ [ "-b", "--brain BRAIN", "The Brain to use" ], [ "-a", "--adapter ADAPTER", "The Adapter to use" ], [ "-c", "--create PATH", "Create a deployable brobbot" ], [ "-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 brobbot's config to make sure it won't fail at startup"] [ "-v", "--version", "Displays the version of brobbot installed" ] ] Options = brain: process.env.BROBBOT_BRAIN or "dumb" adapter: process.env.BROBBOT_ADAPTER or "shell" alias: process.env.BROBBOT_ALIAS or false create: process.env.BROBBOT_CREATE or false enableHttpd: process.env.BROBBOT_HTTPD or true scripts: process.env.BROBBOT_SCRIPTS or [] name: process.env.BROBBOT_NAME or "Brobbot" path: process.env.BROBBOT_PATH or "." configCheck: false Parser = new OptParse.OptionParser(Switches) Parser.banner = "Usage brobbot [options]" Parser.on "brain", (opt, value) -> Options.brain = value 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.parse process.argv unless process.platform is "win32" process.on 'SIGTERM', -> process.exit 0 if Options.create console.error "'brobbot --create' is deprecated. Use the yeoman generator instead:" console.error " npm install -g yo generator-brobbot" console.error " mkdir -p #{Options.path}" console.error " yo brobbot" console.error "See https://github.com/github/brobbot/blob/master/docs/README.md for more details on getting started." process.exit 1 else adapterPath = Path.join __dirname, "..", "src", "adapters" brainPath = Path.join __dirname, "..", "src", "brains" robot = Brobbot.loadBot adapterPath, Options.adapter, brainPath, Options.brain, Options.enableHttpd, Options.name if Options.version console.log robot.version process.exit 0 robot.alias = Options.alias loadScripts = -> loadingScripts = [] scriptsPath = Path.resolve ".", "scripts" loadingScripts.push(robot.load scriptsPath) scriptsPath = Path.resolve ".", "src", "scripts" loadingScripts.push(robot.load scriptsPath) brobbotScripts = Path.resolve ".", "brobbot-scripts.json" if Fs.existsSync(brobbotScripts) data = Fs.readFileSync(brobbotScripts) if data.length > 0 try scripts = JSON.parse data scriptsPath = Path.resolve "node_modules", "brobbot-scripts", "src", "scripts" loadingScripts.push(robot.loadBrobbotScripts(scriptsPath, scripts)) catch err console.error "Error parsing JSON data from brobbot-scripts.json: #{err}" process.exit(1) 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) loadingScripts.push(robot.loadExternalScripts scripts) for path in Options.scripts if path[0] == '/' scriptsPath = path else scriptsPath = Path.resolve ".", path loadingScripts.push(robot.load scriptsPath) Q.all(loadingScripts) if Options.configCheck loadScripts().then -> console.log "OK" process.exit 0 robot.ready.then -> loadScripts().then(robot.run.bind(robot))