@@ -24,7 +24,12 @@ export type LogEntry = {
24
24
id : string
25
25
}
26
26
| {
27
- kind : 'stdout' | 'stderr'
27
+ kind : 'stdout'
28
+ data : Buffer
29
+ id : string
30
+ }
31
+ | {
32
+ kind : 'stderr'
28
33
data : Buffer
29
34
id : string
30
35
}
@@ -59,39 +64,60 @@ export type LogEntry = {
59
64
}
60
65
)
61
66
62
- type LogFormatter = ( cmd ?: string ) => string
67
+ type LogFormatters = {
68
+ [ key in LogEntry [ 'kind' ] ] : (
69
+ entry : Extract < LogEntry , { kind : key } >
70
+ ) => string | Buffer
71
+ }
72
+
73
+ const formatters : LogFormatters = {
74
+ cmd ( { cmd } ) {
75
+ return formatCmd ( cmd )
76
+ } ,
77
+ stdout ( { data } ) {
78
+ return data
79
+ } ,
80
+ stderr ( { data } ) {
81
+ return data
82
+ } ,
83
+ custom ( { data } ) {
84
+ return data
85
+ } ,
86
+ fetch ( entry ) {
87
+ const init = entry . init ? ' ' + inspect ( entry . init ) : ''
88
+ return '$ ' + chalk . greenBright ( 'fetch' ) + ` ${ entry . url } ${ init } \n`
89
+ } ,
90
+ cd ( entry ) {
91
+ return '$ ' + chalk . greenBright ( 'cd' ) + ` ${ entry . dir } \n`
92
+ } ,
93
+ retry ( entry ) {
94
+ return (
95
+ chalk . bgRed . white ( ' FAIL ' ) +
96
+ ` Attempt: ${ entry . attempt } ${ entry . total == Infinity ? '' : `/${ entry . total } ` } ` +
97
+ ( entry . delay > 0 ? `; next in ${ entry . delay } ms` : '' ) +
98
+ '\n'
99
+ )
100
+ } ,
101
+ end ( ) {
102
+ return ''
103
+ } ,
104
+ }
105
+
63
106
type Log = {
64
107
( entry : LogEntry ) : void
65
- formatCmd ?: LogFormatter
108
+ formatters ?: Partial < LogFormatters >
66
109
output ?: NodeJS . WriteStream
67
110
}
111
+
68
112
export const log : Log = function ( entry ) {
69
113
if ( ! entry . verbose ) return
70
114
const stream = log . output || process . stderr
71
- switch ( entry . kind ) {
72
- case 'cmd' :
73
- stream . write ( ( log . formatCmd || formatCmd ) ( entry . cmd ) )
74
- break
75
- case 'stdout' :
76
- case 'stderr' :
77
- case 'custom' :
78
- stream . write ( entry . data )
79
- break
80
- case 'cd' :
81
- stream . write ( '$ ' + chalk . greenBright ( 'cd' ) + ` ${ entry . dir } \n` )
82
- break
83
- case 'fetch' :
84
- const init = entry . init ? ' ' + inspect ( entry . init ) : ''
85
- stream . write ( '$ ' + chalk . greenBright ( 'fetch' ) + ` ${ entry . url } ${ init } \n` )
86
- break
87
- case 'retry' :
88
- stream . write (
89
- chalk . bgRed . white ( ' FAIL ' ) +
90
- ` Attempt: ${ entry . attempt } ${ entry . total == Infinity ? '' : `/${ entry . total } ` } ` +
91
- ( entry . delay > 0 ? `; next in ${ entry . delay } ms` : '' ) +
92
- '\n'
93
- )
94
- }
115
+ const format = ( log . formatters ?. [ entry . kind ] || formatters [ entry . kind ] ) as (
116
+ entry : LogEntry
117
+ ) => string | Buffer
118
+ if ( ! format ) return // ignore unknown log entries
119
+ const data = format ( entry )
120
+ stream . write ( data )
95
121
}
96
122
97
123
const SYNTAX = '()[]{}<>;:+|&='
@@ -115,7 +141,7 @@ const RESERVED_WORDS = new Set([
115
141
'EOF' ,
116
142
] )
117
143
118
- export const formatCmd : LogFormatter = function ( cmd ) {
144
+ export function formatCmd ( cmd : string ) : string {
119
145
if ( cmd == undefined ) return chalk . grey ( 'undefined' )
120
146
let q = ''
121
147
let out = '$ '
0 commit comments