Skip to content

Commit

Permalink
feat: Execute a script command via CLI like hubot -e some command (#…
Browse files Browse the repository at this point in the history
…1734)

* testing

* feat: Execute commands in scripts on the CLI via hubot -e <some command>
  • Loading branch information
joeyguerra authored Aug 31, 2024
1 parent 3647698 commit b50c39c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
16 changes: 13 additions & 3 deletions bin/Hubot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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()
})()
20 changes: 20 additions & 0 deletions test/Hubot_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit b50c39c

Please sign in to comment.