@@ -12,7 +12,7 @@ import { THEME } from './theme.js'
1212
1313export type Logs = BufferedLogs | StreamedLogs
1414export type BufferedLogs = { stdout : string [ ] ; stderr : string [ ] ; outputFlusher ?: OutputFlusher }
15- export type StreamedLogs = { outputFlusher ?: OutputFlusher }
15+ export type StreamedLogs = { outputFlusher ?: OutputFlusher ; logFunction ?: ( message : string ) => void }
1616
1717export const logsAreBuffered = ( logs : Logs | undefined ) : logs is BufferedLogs => {
1818 return logs !== undefined && 'stdout' in logs
@@ -31,13 +31,16 @@ const EMPTY_LINE = '\u{200B}'
3131 * When the `buffer` option is true, we return logs instead of printing them
3232 * on the console. The logs are accumulated in a `logs` array variable.
3333 */
34- export const getBufferLogs = ( config : { buffer ?: boolean } ) : BufferedLogs | undefined => {
35- const { buffer = false } = config
36- if ( ! buffer ) {
37- return
34+ export const getBufferLogs = ( config : { buffer ?: boolean ; logger ?: ( message : string ) => void } ) : Logs | undefined => {
35+ const { buffer = false , logger } = config
36+
37+ if ( logger ) {
38+ return { logFunction : logger }
3839 }
3940
40- return { stdout : [ ] , stderr : [ ] }
41+ if ( buffer ) {
42+ return { stdout : [ ] , stderr : [ ] }
43+ }
4144}
4245
4346// Core logging utility, used by the other methods.
@@ -64,9 +67,28 @@ export const log = function (
6467 return
6568 }
6669
70+ if ( typeof logs ?. logFunction === 'function' ) {
71+ logs . logFunction ( stringC )
72+
73+ return
74+ }
75+
6776 console . log ( stringC )
6877}
6978
79+ export type LogOutput = Pick < BufferedLogs , 'stderr' | 'stdout' >
80+
81+ // Returns a `logs` object to be returned in the public interface,
82+ // always containing a `stderr` and `stdout` arrays, regardless of
83+ // whether the `buffer` input property was used.
84+ export const getLogsOutput = ( logs : Logs | undefined ) : LogOutput => {
85+ if ( ! logs || ! logsAreBuffered ( logs ) ) {
86+ return { stdout : [ ] , stderr : [ ] }
87+ }
88+
89+ return { stdout : logs . stdout , stderr : logs . stderr }
90+ }
91+
7092const serializeIndentedArray = function ( array ) {
7193 return serializeArray ( array . map ( serializeIndentedItem ) )
7294}
@@ -75,61 +97,61 @@ const serializeIndentedItem = function (item) {
7597 return indentString ( item , INDENT_SIZE + 1 ) . trimStart ( )
7698}
7799
78- export const logError = function ( logs : BufferedLogs | undefined , string : string , opts = { } ) {
100+ export const logError = function ( logs : Logs | undefined , string : string , opts = { } ) {
79101 log ( logs , string , { color : THEME . errorLine , ...opts } )
80102}
81103
82- export const logWarning = function ( logs : BufferedLogs | undefined , string : string , opts = { } ) {
104+ export const logWarning = function ( logs : Logs | undefined , string : string , opts = { } ) {
83105 log ( logs , string , { color : THEME . warningLine , ...opts } )
84106}
85107
86108// Print a message that is under a header/subheader, i.e. indented
87- export const logMessage = function ( logs : BufferedLogs | undefined , string : string , opts = { } ) {
109+ export const logMessage = function ( logs : Logs | undefined , string : string , opts = { } ) {
88110 log ( logs , string , { indent : true , ...opts } )
89111}
90112
91113// Print an object
92- export const logObject = function ( logs : BufferedLogs | undefined , object , opts ) {
114+ export const logObject = function ( logs : Logs | undefined , object , opts ) {
93115 logMessage ( logs , serializeObject ( object ) , opts )
94116}
95117
96118// Print an array
97- export const logArray = function ( logs : BufferedLogs | undefined , array , opts = { } ) {
119+ export const logArray = function ( logs : Logs | undefined , array , opts = { } ) {
98120 logMessage ( logs , serializeIndentedArray ( array ) , { color : THEME . none , ...opts } )
99121}
100122
101123// Print an array of errors
102- export const logErrorArray = function ( logs : BufferedLogs | undefined , array , opts = { } ) {
124+ export const logErrorArray = function ( logs : Logs | undefined , array , opts = { } ) {
103125 logMessage ( logs , serializeIndentedArray ( array ) , { color : THEME . errorLine , ...opts } )
104126}
105127
106128// Print an array of warnings
107- export const logWarningArray = function ( logs : BufferedLogs | undefined , array , opts = { } ) {
129+ export const logWarningArray = function ( logs : Logs | undefined , array , opts = { } ) {
108130 logMessage ( logs , serializeIndentedArray ( array ) , { color : THEME . warningLine , ...opts } )
109131}
110132
111133// Print a main section header
112- export const logHeader = function ( logs : BufferedLogs | undefined , string : string , opts = { } ) {
134+ export const logHeader = function ( logs : Logs | undefined , string : string , opts = { } ) {
113135 log ( logs , `\n${ getHeader ( string ) } ` , { color : THEME . header , ...opts } )
114136}
115137
116138// Print a main section header, when an error happened
117- export const logErrorHeader = function ( logs : BufferedLogs | undefined , string : string , opts = { } ) {
139+ export const logErrorHeader = function ( logs : Logs | undefined , string : string , opts = { } ) {
118140 logHeader ( logs , string , { color : THEME . errorHeader , ...opts } )
119141}
120142
121143// Print a sub-section header
122- export const logSubHeader = function ( logs : BufferedLogs | undefined , string : string , opts = { } ) {
144+ export const logSubHeader = function ( logs : Logs | undefined , string : string , opts = { } ) {
123145 log ( logs , `\n${ figures . pointer } ${ string } ` , { color : THEME . subHeader , ...opts } )
124146}
125147
126148// Print a sub-section header, when an error happened
127- export const logErrorSubHeader = function ( logs : BufferedLogs | undefined , string : string , opts = { } ) {
149+ export const logErrorSubHeader = function ( logs : Logs | undefined , string : string , opts = { } ) {
128150 logSubHeader ( logs , string , { color : THEME . errorSubHeader , ...opts } )
129151}
130152
131153// Print a sub-section header, when a warning happened
132- export const logWarningSubHeader = function ( logs : BufferedLogs | undefined , string : string , opts = { } ) {
154+ export const logWarningSubHeader = function ( logs : Logs | undefined , string : string , opts = { } ) {
133155 logSubHeader ( logs , string , { color : THEME . warningSubHeader , ...opts } )
134156}
135157
@@ -162,7 +184,7 @@ export const reduceLogLines = function (lines) {
162184 * the user-facing build logs)
163185 */
164186export const getSystemLogger = function (
165- logs : BufferedLogs | undefined ,
187+ logs : Logs | undefined ,
166188 debug : boolean ,
167189 /** A system log file descriptor, if non is provided it will be a noop logger */
168190 systemLogFile ?: number ,
@@ -192,15 +214,7 @@ export const getSystemLogger = function (
192214 return ( ...args ) => fileDescriptor . write ( `${ reduceLogLines ( args ) } \n` )
193215}
194216
195- export const addOutputFlusher = ( logs : Logs , outputFlusher : OutputFlusher ) : Logs => {
196- if ( logsAreBuffered ( logs ) ) {
197- return {
198- ...logs ,
199- outputFlusher,
200- }
201- }
202-
203- return {
204- outputFlusher,
205- }
206- }
217+ export const addOutputFlusher = ( logs : Logs , outputFlusher : OutputFlusher ) : Logs => ( {
218+ ...logs ,
219+ outputFlusher,
220+ } )
0 commit comments