hubot 13.0.1 → 13.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +1 -1
- package/package.json +1 -1
- package/src/adapters/Shell.mjs +37 -12
package/CONTRIBUTING.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Contributions to this project are [released](https://help.github.com/articles/github-terms-of-service/#6-contributions-under-repository-license) to the public under the [project's open source license](LICENSE.md).
|
|
4
4
|
|
|
5
|
-
Everyone is welcome to contribute to Hubot. Contributing doesn’t just mean submitting pull requests—there are many different ways for you to get involved, including answering questions
|
|
5
|
+
Everyone is welcome to contribute to Hubot. Contributing doesn’t just mean submitting pull requests—there are many different ways for you to get involved, including answering questions, reporting or triaging [issues](https://github.com/github/hubot/issues), and participating in using Hubot.
|
|
6
6
|
|
|
7
7
|
No matter how you want to get involved, we ask that you first learn what’s expected of anyone who participates in the project by reading the [Contributor Covenant Code of Conduct](http://contributor-covenant.org). By participating, you are expected to uphold this code.
|
|
8
8
|
|
package/package.json
CHANGED
package/src/adapters/Shell.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
import
|
|
3
|
+
import { stat, writeFile, unlink, appendFile, readFile } from 'node:fs/promises'
|
|
4
4
|
import readline from 'node:readline'
|
|
5
5
|
import Adapter from '../Adapter.mjs'
|
|
6
6
|
import { TextMessage } from '../Message.mjs'
|
|
@@ -22,15 +22,37 @@ const showHelp = () => {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
const bold = str => `\x1b[1m${str}\x1b[22m`
|
|
25
|
+
const green = str => `\x1b[32m${str}\x1b[0m`
|
|
26
|
+
const levelColors = {
|
|
27
|
+
error: '\x1b[31m',
|
|
28
|
+
warn: '\x1b[33m',
|
|
29
|
+
debug: '\x1b[35m',
|
|
30
|
+
info: '\x1b[34m',
|
|
31
|
+
trace: '\x1b[36m',
|
|
32
|
+
fatal: '\x1b[91m'
|
|
33
|
+
}
|
|
34
|
+
const reset = '\x1b[0m'
|
|
25
35
|
|
|
26
36
|
class Shell extends Adapter {
|
|
27
37
|
#rl = null
|
|
28
38
|
constructor (robot) {
|
|
29
39
|
super(robot)
|
|
30
40
|
this.name = 'Shell'
|
|
41
|
+
const levels = ['trace', 'debug', 'info', 'warn', 'error', 'fatal']
|
|
42
|
+
levels.forEach(level => {
|
|
43
|
+
robot.logger[level] = async (...args) => {
|
|
44
|
+
const color = levelColors[level] || ''
|
|
45
|
+
const msg = `${color}[${level}]${reset} ${args.map(a => typeof a === 'object' ? JSON.stringify(a) : a).join(' ')}`
|
|
46
|
+
await this.send({ user: { name: 'Logger', room: 'Shell' } }, msg)
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
this.robot.on('scripts have loaded', () => {
|
|
50
|
+
this.#rl.prompt()
|
|
51
|
+
})
|
|
31
52
|
}
|
|
32
53
|
|
|
33
54
|
async send (envelope, ...strings) {
|
|
55
|
+
this.#rl.prompt()
|
|
34
56
|
Array.from(strings).forEach(str => console.log(bold(str)))
|
|
35
57
|
}
|
|
36
58
|
|
|
@@ -40,22 +62,25 @@ class Shell extends Adapter {
|
|
|
40
62
|
|
|
41
63
|
async reply (envelope, ...strings) {
|
|
42
64
|
strings = strings.map((s) => `${envelope.user.name}: ${s}`)
|
|
43
|
-
this.send(envelope, ...strings)
|
|
65
|
+
await this.send(envelope, ...strings)
|
|
44
66
|
}
|
|
45
67
|
|
|
46
68
|
async run () {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
69
|
+
try {
|
|
70
|
+
const stats = await stat(historyPath)
|
|
71
|
+
if (stats.size > historySize) {
|
|
72
|
+
await unlink(historyPath)
|
|
73
|
+
await writeFile(historyPath, '')
|
|
74
|
+
}
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.log(error)
|
|
77
|
+
await writeFile(historyPath, '')
|
|
53
78
|
}
|
|
54
79
|
|
|
55
80
|
this.#rl = readline.createInterface({
|
|
56
81
|
input: this.robot.stdin ?? process.stdin,
|
|
57
82
|
output: this.robot.stdout ?? process.stdout,
|
|
58
|
-
prompt: `${this.robot.name ?? this.robot.alias}>
|
|
83
|
+
prompt: green(`${this.robot.name ?? this.robot.alias}> `),
|
|
59
84
|
completer
|
|
60
85
|
})
|
|
61
86
|
this.#rl.on('line', async (line) => {
|
|
@@ -69,6 +94,7 @@ class Shell extends Adapter {
|
|
|
69
94
|
case '\\?':
|
|
70
95
|
case 'help':
|
|
71
96
|
showHelp()
|
|
97
|
+
this.#rl.prompt()
|
|
72
98
|
break
|
|
73
99
|
case '\\c':
|
|
74
100
|
case 'clear':
|
|
@@ -95,14 +121,13 @@ class Shell extends Adapter {
|
|
|
95
121
|
|
|
96
122
|
this.#rl.on('history', async (history) => {
|
|
97
123
|
if (history.length === 0) return
|
|
98
|
-
await
|
|
124
|
+
await appendFile(historyPath, `${history[0]}\n`)
|
|
99
125
|
})
|
|
100
126
|
|
|
101
|
-
const existingHistory = (await
|
|
127
|
+
const existingHistory = (await readFile(historyPath, 'utf8')).split('\n')
|
|
102
128
|
existingHistory.reverse().forEach(line => this.#rl.history.push(line))
|
|
103
129
|
|
|
104
130
|
try {
|
|
105
|
-
this.#rl.prompt()
|
|
106
131
|
this.emit('connected', this)
|
|
107
132
|
} catch (error) {
|
|
108
133
|
console.log(error)
|