diff --git a/bin/Hubot.mjs b/bin/Hubot.mjs index 01f9965f1..5032cbf46 100755 --- a/bin/Hubot.mjs +++ b/bin/Hubot.mjs @@ -16,7 +16,8 @@ const switches = [ ['-n', '--name HUBOT_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'] + ['-v', '--version', 'Displays the version of hubot installed'], + ['-e', '--execute', 'Runs the command as if it were a hubot command'] ] const options = { @@ -66,6 +67,10 @@ Parser.on('name', (opt, value) => { options.name = value }) +Parser.on('execute', (opt, value) => { + options.execute = value +}) + Parser.on('require', (opt, value) => { options.scripts.push(value) }) @@ -141,7 +146,12 @@ async function loadExternalScripts () { process.exit(0) } - robot.adapter.once('connected', loadScripts) - + robot.adapter.once('connected', async () => { + await loadScripts() + if (options.execute) { + await robot.receive(new Hubot.TextMessage(new Hubot.User('shell', { room: '#shell' }), `@${robot.name} ${options.execute.trim()}`)) + robot.shutdown() + } + }) await robot.run() })() diff --git a/test/Hubot_test.mjs b/test/Hubot_test.mjs index cbb6881fe..c476cec7c 100644 --- a/test/Hubot_test.mjs +++ b/test/Hubot_test.mjs @@ -48,6 +48,7 @@ describe('Running bin/Hubot.mjs', () => { -r, --require PATH -t, --config-check -v, --version + -e, --execute ` let actual = '' hubot.stdout.on('data', (data) => { @@ -61,6 +62,25 @@ describe('Running bin/Hubot.mjs', () => { done() }) }) + it('should execute the command when run with --execute or -e', (t, done) => { + const expected = "HELO World! I'm Hubot." + const commandText = 'helo' + const env = Object.assign({}, process.env, { NOLOG: 'off' }) + const hubot = process.platform === 'win32' + ? spawn('node', ['./bin/Hubot.mjs', '-d', '--execute', commandText, '-r', 'test/scripts'], { env }) + : spawn('./bin/hubot', ['-d', '--execute', commandText, '-r', 'test/scripts'], { env }) + let actual = '' + hubot.stdout.on('data', (data) => { + actual += data.toString() + }) + hubot.stderr.on('data', (data) => { + actual += data.toString() + }) + hubot.on('close', (code) => { + assert.ok(actual.includes(expected)) + done() + }) + }) }) describe('Running hubot with args', () => {