Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Execute a script command via CLI like hubot -e some command #1734

Merged
merged 3 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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