|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const pathResolve = require('path').resolve |
| 5 | + |
| 6 | +const OptParse = require('optparse') |
| 7 | + |
| 8 | +const Hubot = require('..') |
| 9 | + |
| 10 | +const switches = [ |
| 11 | + ['-a', '--adapter ADAPTER', 'The Adapter to use'], |
| 12 | + ['-c', '--create PATH', 'Create a deployable hubot'], |
| 13 | + ['-d', '--disable-httpd', 'Disable the HTTP server'], |
| 14 | + ['-h', '--help', 'Display the help information'], |
| 15 | + ['-l', '--alias ALIAS', "Enable replacing the robot's name with alias"], |
| 16 | + ['-n', '--name NAME', 'The name of the robot in chat'], |
| 17 | + ['-r', '--require PATH', 'Alternative scripts path'], |
| 18 | + ['-t', '--config-check', "Test hubot's config to make sure it won't fail at startup"], |
| 19 | + ['-v', '--version', 'Displays the version of hubot installed'] |
| 20 | +] |
| 21 | + |
| 22 | +const options = { |
| 23 | + adapter: process.env.HUBOT_ADAPTER || 'shell', |
| 24 | + alias: process.env.HUBOT_ALIAS || false, |
| 25 | + create: process.env.HUBOT_CREATE || false, |
| 26 | + enableHttpd: process.env.HUBOT_HTTPD || true, |
| 27 | + scripts: process.env.HUBOT_SCRIPTS || [], |
| 28 | + name: process.env.HUBOT_NAME || 'Hubot', |
| 29 | + path: process.env.HUBOT_PATH || '.', |
| 30 | + configCheck: false |
| 31 | +} |
| 32 | + |
| 33 | +const Parser = new OptParse.OptionParser(switches) |
| 34 | +Parser.banner = 'Usage hubot [options]' |
| 35 | + |
| 36 | +Parser.on('adapter', (opt, value) => { |
| 37 | + options.adapter = value |
| 38 | +}) |
| 39 | + |
| 40 | +Parser.on('create', function (opt, value) { |
| 41 | + options.path = value |
| 42 | + options.create = true |
| 43 | +}) |
| 44 | + |
| 45 | +Parser.on('disable-httpd', opt => { |
| 46 | + options.enableHttpd = false |
| 47 | +}) |
| 48 | + |
| 49 | +Parser.on('help', function (opt, value) { |
| 50 | + console.log(Parser.toString()) |
| 51 | + return process.exit(0) |
| 52 | +}) |
| 53 | + |
| 54 | +Parser.on('alias', function (opt, value) { |
| 55 | + if (!value) { |
| 56 | + value = '/' |
| 57 | + } |
| 58 | + options.alias = value |
| 59 | +}) |
| 60 | + |
| 61 | +Parser.on('name', (opt, value) => { |
| 62 | + options.name = value |
| 63 | +}) |
| 64 | + |
| 65 | +Parser.on('require', (opt, value) => { |
| 66 | + options.scripts.push(value) |
| 67 | +}) |
| 68 | + |
| 69 | +Parser.on('config-check', opt => { |
| 70 | + options.configCheck = true |
| 71 | +}) |
| 72 | + |
| 73 | +Parser.on('version', (opt, value) => { |
| 74 | + options.version = true |
| 75 | +}) |
| 76 | + |
| 77 | +Parser.on((opt, value) => { |
| 78 | + console.warn(`Unknown option: ${opt}`) |
| 79 | +}) |
| 80 | + |
| 81 | +Parser.parse(process.argv) |
| 82 | + |
| 83 | +if (process.platform !== 'win32') { |
| 84 | + process.on('SIGTERM', () => process.exit(0)) |
| 85 | +} |
| 86 | + |
| 87 | +if (options.create) { |
| 88 | + console.error("'hubot --create' is deprecated. Use the yeoman generator instead:") |
| 89 | + console.error(' npm install -g yo generator-hubot') |
| 90 | + console.error(` mkdir -p ${options.path}`) |
| 91 | + console.error(` cd ${options.path}`) |
| 92 | + console.error(' yo hubot') |
| 93 | + console.error('See https://github.com/github/hubot/blob/master/docs/index.md for more details on getting started.') |
| 94 | + process.exit(1) |
| 95 | +} |
| 96 | + |
| 97 | +const robot = Hubot.loadBot(undefined, options.adapter, options.enableHttpd, options.name, options.alias) |
| 98 | + |
| 99 | +if (options.version) { |
| 100 | + console.log(robot.version) |
| 101 | + process.exit(0) |
| 102 | +} |
| 103 | + |
| 104 | +if (options.configCheck) { |
| 105 | + loadScripts() |
| 106 | + console.log('OK') |
| 107 | + process.exit(0) |
| 108 | +} |
| 109 | + |
| 110 | +robot.adapter.once('connected', loadScripts) |
| 111 | + |
| 112 | +robot.run() |
| 113 | + |
| 114 | +function loadScripts () { |
| 115 | + robot.load(pathResolve('.', 'scripts')) |
| 116 | + robot.load(pathResolve('.', 'src', 'scripts')) |
| 117 | + |
| 118 | + loadHubotScripts() |
| 119 | + loadExternalScripts() |
| 120 | + |
| 121 | + options.scripts.forEach((scriptPath) => { |
| 122 | + if (scriptPath[0] === '/') { |
| 123 | + return robot.load(scriptPath) |
| 124 | + } |
| 125 | + |
| 126 | + robot.load(pathResolve('.', scriptPath)) |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +function loadHubotScripts () { |
| 131 | + const hubotScripts = pathResolve('.', 'hubot-scripts.json') |
| 132 | + let scripts |
| 133 | + let scriptsPath |
| 134 | + |
| 135 | + if (fs.existsSync(hubotScripts)) { |
| 136 | + let hubotScriptsWarning |
| 137 | + const data = fs.readFileSync(hubotScripts) |
| 138 | + |
| 139 | + if (data.length === 0) { |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + try { |
| 144 | + scripts = JSON.parse(data) |
| 145 | + scriptsPath = pathResolve('node_modules', 'hubot-scripts', 'src', 'scripts') |
| 146 | + robot.loadHubotScripts(scriptsPath, scripts) |
| 147 | + } catch (error) { |
| 148 | + const err = error |
| 149 | + robot.logger.error(`Error parsing JSON data from hubot-scripts.json: ${err}`) |
| 150 | + process.exit(1) |
| 151 | + } |
| 152 | + |
| 153 | + 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' |
| 154 | + |
| 155 | + if (scripts.length === 0) { |
| 156 | + hubotScriptsWarning += 'Your hubot-scripts.json is empty, so you just need to remove it.' |
| 157 | + return robot.logger.warning(hubotScriptsWarning) |
| 158 | + } |
| 159 | + |
| 160 | + const hubotScriptsReplacements = pathResolve('node_modules', 'hubot-scripts', 'replacements.json') |
| 161 | + const replacementsData = fs.readFileSync(hubotScriptsReplacements) |
| 162 | + const replacements = JSON.parse(replacementsData) |
| 163 | + const scriptsWithoutReplacements = [] |
| 164 | + |
| 165 | + if (!fs.existsSync(hubotScriptsReplacements)) { |
| 166 | + hubotScriptsWarning += 'To get a list of recommended replacements, update your hubot-scripts: npm install --save hubot-scripts@latest' |
| 167 | + return robot.logger.warning(hubotScriptsWarning) |
| 168 | + } |
| 169 | + |
| 170 | + hubotScriptsWarning += 'The following scripts have known replacements. Follow the link for installation instructions, then remove it from hubot-scripts.json:\n' |
| 171 | + |
| 172 | + scripts.forEach((script) => { |
| 173 | + const replacement = replacements[script] |
| 174 | + |
| 175 | + if (replacement) { |
| 176 | + hubotScriptsWarning += `* ${script}: ${replacement}\n` |
| 177 | + } else { |
| 178 | + scriptsWithoutReplacements.push(script) |
| 179 | + } |
| 180 | + }) |
| 181 | + |
| 182 | + hubotScriptsWarning += '\n' |
| 183 | + |
| 184 | + if (scriptsWithoutReplacements.length > 0) { |
| 185 | + 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' |
| 186 | + hubotScriptsWarning += scriptsWithoutReplacements.map((script) => `* ${script}\n`).join('') |
| 187 | + hubotScriptsWarning += '\nYou an also try updating hubot-scripts to get the latest list of replacements: npm install --save hubot-scripts@latest' |
| 188 | + } |
| 189 | + |
| 190 | + robot.logger.warning(hubotScriptsWarning) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +function loadExternalScripts () { |
| 195 | + const externalScripts = pathResolve('.', 'external-scripts.json') |
| 196 | + |
| 197 | + if (!fs.existsSync(externalScripts)) { |
| 198 | + return |
| 199 | + } |
| 200 | + |
| 201 | + fs.readFile(externalScripts, function (error, data) { |
| 202 | + if (error) { |
| 203 | + throw error |
| 204 | + } |
| 205 | + |
| 206 | + try { |
| 207 | + robot.loadExternalScripts(JSON.parse(data)) |
| 208 | + } catch (error) { |
| 209 | + console.error(`Error parsing JSON data from external-scripts.json: ${error}`) |
| 210 | + process.exit(1) |
| 211 | + } |
| 212 | + }) |
| 213 | +} |
0 commit comments