4
4
* ------------------------------------------------------------------------------------------ */
5
5
/// <reference path="../../typings/thenable.d.ts" />
6
6
7
+ import { inspect } from 'node:util' ;
8
+
7
9
import * as Is from '../common/utils/is' ;
8
10
import { Connection , _ , _Connection , Features , WatchDog , createConnection as createCommonConnection } from '../common/server' ;
9
11
@@ -190,6 +192,7 @@ function _createConnection<PConsole = _, PTracer = _, PTelemetry = _, PClient =
190
192
options ?: ConnectionStrategy | ConnectionOptions ,
191
193
factories ?: Features < PConsole , PTracer , PTelemetry , PClient , PWindow , PWorkspace , PLanguages , PNotebooks > ,
192
194
) : _Connection < PConsole , PTracer , PTelemetry , PClient , PWindow , PWorkspace , PLanguages , PNotebooks > {
195
+ let stdio = false ;
193
196
if ( ! input && ! output && process . argv . length > 2 ) {
194
197
let port : number | undefined = void 0 ;
195
198
let pipeName : string | undefined = void 0 ;
@@ -201,6 +204,7 @@ function _createConnection<PConsole = _, PTracer = _, PTelemetry = _, PClient =
201
204
output = new IPCMessageWriter ( process ) ;
202
205
break ;
203
206
} else if ( arg === '--stdio' ) {
207
+ stdio = true ;
204
208
input = process . stdin ;
205
209
output = process . stdout ;
206
210
break ;
@@ -255,7 +259,76 @@ function _createConnection<PConsole = _, PTracer = _, PTelemetry = _, PClient =
255
259
256
260
const connectionFactory = ( logger : Logger ) : ProtocolConnection => {
257
261
const result = createProtocolConnection ( input as any , output as any , logger , options ) ;
262
+ if ( stdio ) {
263
+ patchConsole ( logger ) ;
264
+ }
258
265
return result ;
259
266
} ;
260
267
return createCommonConnection ( connectionFactory , watchDog , factories ) ;
261
- }
268
+ }
269
+
270
+ function patchConsole ( logger : Logger ) : undefined {
271
+ function serialize ( args : unknown [ ] ) {
272
+ return args . map ( arg => typeof arg === 'string' ? arg : inspect ( arg ) ) . join ( ' ' ) ;
273
+ }
274
+
275
+ const counters = new Map < string , number > ( ) ;
276
+
277
+ console . assert = function assert ( assertion , ...args ) {
278
+ if ( assertion ) {
279
+ return ;
280
+ }
281
+ if ( args . length === 0 ) {
282
+ logger . error ( 'Assertion failed' ) ;
283
+ } else {
284
+ const [ message , ...rest ] = args ;
285
+ logger . error ( `Assertion failed: ${ message } ${ serialize ( rest ) } ` ) ;
286
+ }
287
+ } ;
288
+
289
+ console . count = function count ( label = 'default' ) {
290
+ const message = String ( label ) ;
291
+ let counter = counters . get ( message ) ?? 0 ;
292
+ counter += 1 ;
293
+ counters . set ( message , counter ) ;
294
+ logger . log ( `${ message } : ${ message } ` ) ;
295
+ } ;
296
+
297
+ console . countReset = function countReset ( label ) {
298
+ if ( label === undefined ) {
299
+ counters . clear ( ) ;
300
+ } else {
301
+ counters . delete ( String ( label ) ) ;
302
+ }
303
+ } ;
304
+
305
+ console . debug = function debug ( ...args ) {
306
+ logger . log ( serialize ( args ) ) ;
307
+ } ;
308
+
309
+ console . dir = function dir ( arg , options ) {
310
+ // @ts -expect-error https://github.com/DefinitelyTyped/DefinitelyTyped/pull/66626
311
+ logger . log ( inspect ( arg , options ) ) ;
312
+ } ;
313
+
314
+ console . log = function log ( ...args ) {
315
+ logger . log ( serialize ( args ) ) ;
316
+ } ;
317
+
318
+ console . error = function error ( ...args ) {
319
+ logger . error ( serialize ( args ) ) ;
320
+ } ;
321
+
322
+ console . trace = function trace ( ...args ) {
323
+ const stack = new Error ( ) . stack ! . replace ( / ( .+ \n ) { 2 } / , '' ) ;
324
+ let message = 'Trace' ;
325
+ if ( args . length !== 0 ) {
326
+ message += `: ${ serialize ( args ) } ` ;
327
+ }
328
+ logger . log ( `${ message } \n${ stack } ` ) ;
329
+ } ;
330
+
331
+ console . warn = function warn ( ...args ) {
332
+ logger . warn ( serialize ( args ) ) ;
333
+ } ;
334
+ }
0 commit comments