1
1
'use strict'
2
2
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'
5
4
import readline from 'node:readline'
6
5
import Adapter from '../Adapter.mjs'
7
6
import { TextMessage } from '../Message.mjs'
@@ -23,15 +22,37 @@ const showHelp = () => {
23
22
}
24
23
25
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'
26
35
27
36
class Shell extends Adapter {
28
37
#rl = null
29
38
constructor ( robot ) {
30
39
super ( robot )
31
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
+ } )
32
52
}
33
53
34
54
async send ( envelope , ...strings ) {
55
+ this . #rl. prompt ( )
35
56
Array . from ( strings ) . forEach ( str => console . log ( bold ( str ) ) )
36
57
}
37
58
@@ -41,7 +62,7 @@ class Shell extends Adapter {
41
62
42
63
async reply ( envelope , ...strings ) {
43
64
strings = strings . map ( ( s ) => `${ envelope . user . name } : ${ s } ` )
44
- this . send ( envelope , ...strings )
65
+ await this . send ( envelope , ...strings )
45
66
}
46
67
47
68
async run ( ) {
@@ -59,7 +80,7 @@ class Shell extends Adapter {
59
80
this . #rl = readline . createInterface ( {
60
81
input : this . robot . stdin ?? process . stdin ,
61
82
output : this . robot . stdout ?? process . stdout ,
62
- prompt : `${ this . robot . name ?? this . robot . alias } > ` ,
83
+ prompt : green ( `${ this . robot . name ?? this . robot . alias } > ` ) ,
63
84
completer
64
85
} )
65
86
this . #rl. on ( 'line' , async ( line ) => {
@@ -73,6 +94,7 @@ class Shell extends Adapter {
73
94
case '\\?' :
74
95
case 'help' :
75
96
showHelp ( )
97
+ this . #rl. prompt ( )
76
98
break
77
99
case '\\c' :
78
100
case 'clear' :
@@ -99,14 +121,13 @@ class Shell extends Adapter {
99
121
100
122
this . #rl. on ( 'history' , async ( history ) => {
101
123
if ( history . length === 0 ) return
102
- await fs . promises . appendFile ( historyPath , `${ history [ 0 ] } \n` )
124
+ await appendFile ( historyPath , `${ history [ 0 ] } \n` )
103
125
} )
104
126
105
- const existingHistory = ( await fs . promises . readFile ( historyPath , 'utf8' ) ) . split ( '\n' )
127
+ const existingHistory = ( await readFile ( historyPath , 'utf8' ) ) . split ( '\n' )
106
128
existingHistory . reverse ( ) . forEach ( line => this . #rl. history . push ( line ) )
107
129
108
130
try {
109
- this . #rl. prompt ( )
110
131
this . emit ( 'connected' , this )
111
132
} catch ( error ) {
112
133
console . log ( error )
0 commit comments