Skip to content

Commit 6e43007

Browse files
committed
fix: Having to hit enter to get the Shell adapter prompt to show up when Hubot starts
1 parent 1ab39ab commit 6e43007

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

src/adapters/Shell.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

3-
import fs from 'node:fs'
4-
import { stat, writeFile, unlink } from 'node:fs/promises'
3+
import { stat, writeFile, unlink, appendFile, readFile } from 'node:fs/promises'
54
import readline from 'node:readline'
65
import Adapter from '../Adapter.mjs'
76
import { TextMessage } from '../Message.mjs'
@@ -44,15 +43,17 @@ class Shell extends Adapter {
4443
robot.logger[level] = async (...args) => {
4544
const color = levelColors[level] || ''
4645
const msg = `${color}[${level}]${reset} ${args.map(a => typeof a === 'object' ? JSON.stringify(a) : a).join(' ')}`
47-
this.#rl.prompt()
4846
await this.send({ user: { name: 'Logger', room: 'Shell' } }, msg)
4947
}
5048
})
49+
this.robot.on('scripts have loaded', () => {
50+
this.#rl.prompt()
51+
})
5152
}
5253

5354
async send (envelope, ...strings) {
54-
Array.from(strings).forEach(str => console.log(bold(str)))
5555
this.#rl.prompt()
56+
Array.from(strings).forEach(str => console.log(bold(str)))
5657
}
5758

5859
async emote (envelope, ...strings) {
@@ -120,14 +121,13 @@ class Shell extends Adapter {
120121

121122
this.#rl.on('history', async (history) => {
122123
if (history.length === 0) return
123-
await fs.promises.appendFile(historyPath, `${history[0]}\n`)
124+
await appendFile(historyPath, `${history[0]}\n`)
124125
})
125126

126-
const existingHistory = (await fs.promises.readFile(historyPath, 'utf8')).split('\n')
127+
const existingHistory = (await readFile(historyPath, 'utf8')).split('\n')
127128
existingHistory.reverse().forEach(line => this.#rl.history.push(line))
128129

129130
try {
130-
this.#rl.prompt()
131131
this.emit('connected', this)
132132
} catch (error) {
133133
console.log(error)

test/Shell_test.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ describe('Shell Adapter', () => {
8080
let robot = null
8181
beforeEach(async () => {
8282
robot = new Robot('Shell', false, 'TestHubot')
83+
robot.stdin = new stream.Readable()
84+
robot.stdin._read = () => {}
8385
await robot.loadAdapter()
86+
await robot.run()
8487
})
8588
afterEach(() => {
8689
robot.shutdown()

0 commit comments

Comments
 (0)