@@ -8,7 +8,14 @@ import { resolve } from 'import-meta-resolve'
88import { SevereServiceError } from 'webdriverio'
99import type { WebDriverCommands } from '@wdio/protocols'
1010
11- import { PAGE_TRANSITION_COMMANDS , ANSI_REGEX , CONSOLE_METHODS , LOG_LEVEL_PATTERNS , ERROR_INDICATORS , LOG_SOURCES } from './constants.js'
11+ import {
12+ PAGE_TRANSITION_COMMANDS ,
13+ ANSI_REGEX ,
14+ CONSOLE_METHODS ,
15+ LOG_LEVEL_PATTERNS ,
16+ ERROR_INDICATORS ,
17+ LOG_SOURCES
18+ } from './constants.js'
1219import { type CommandLog , type TraceLog , type LogLevel } from './types.js'
1320
1421const log = logger ( '@wdio/devtools-service:SessionCapturer' )
@@ -26,11 +33,17 @@ const detectLogLevel = (text: string): LogLevel => {
2633
2734 // Check log level patterns in priority order
2835 for ( const { level, pattern } of LOG_LEVEL_PATTERNS ) {
29- if ( pattern . test ( cleanText ) ) return level
36+ if ( pattern . test ( cleanText ) ) {
37+ return level
38+ }
3039 }
3140
3241 // Check for error indicators
33- if ( ERROR_INDICATORS . some ( indicator => cleanText . includes ( indicator . toLowerCase ( ) ) ) ) {
42+ if (
43+ ERROR_INDICATORS . some ( ( indicator ) =>
44+ cleanText . includes ( indicator . toLowerCase ( ) )
45+ )
46+ ) {
3447 return 'error'
3548 }
3649
@@ -40,7 +53,11 @@ const detectLogLevel = (text: string): LogLevel => {
4053/**
4154 * Generic helper to create a console log entry
4255 */
43- const createLogEntry = ( type : LogLevel , args : any [ ] , source : typeof LOG_SOURCES [ keyof typeof LOG_SOURCES ] ) : ConsoleLogs => ( {
56+ const createLogEntry = (
57+ type : LogLevel ,
58+ args : any [ ] ,
59+ source : ( typeof LOG_SOURCES ) [ keyof typeof LOG_SOURCES ]
60+ ) : ConsoleLogs => ( {
4461 timestamp : Date . now ( ) ,
4562 type,
4663 args,
@@ -50,7 +67,10 @@ const createLogEntry = (type: LogLevel, args: any[], source: typeof LOG_SOURCES[
5067export class SessionCapturer {
5168 #ws: WebSocket | undefined
5269 #isInjected = false
53- #originalConsoleMethods: Record < typeof CONSOLE_METHODS [ number ] , typeof console . log >
70+ #originalConsoleMethods: Record <
71+ ( typeof CONSOLE_METHODS ) [ number ] ,
72+ typeof console . log
73+ >
5474 #originalProcessMethods: {
5575 stdoutWrite : typeof process . stdout . write
5676 stderrWrite : typeof process . stderr . write
@@ -108,13 +128,23 @@ export class SessionCapturer {
108128 CONSOLE_METHODS . forEach ( ( method ) => {
109129 const originalMethod = this . #originalConsoleMethods[ method ]
110130 console [ method ] = ( ...args : any [ ] ) => {
111- const serializedArgs = args . map ( arg =>
131+ const serializedArgs = args . map ( ( arg ) =>
112132 typeof arg === 'object' && arg !== null
113- ? ( ( ) => { try { return JSON . stringify ( arg ) } catch { return String ( arg ) } } ) ( )
133+ ? ( ( ) => {
134+ try {
135+ return JSON . stringify ( arg )
136+ } catch {
137+ return String ( arg )
138+ }
139+ } ) ( )
114140 : String ( arg )
115141 )
116142
117- const logEntry = createLogEntry ( method , serializedArgs , LOG_SOURCES . TEST )
143+ const logEntry = createLogEntry (
144+ method ,
145+ serializedArgs ,
146+ LOG_SOURCES . TEST
147+ )
118148 this . consoleLogs . push ( logEntry )
119149 this . sendUpstream ( 'consoleLogs' , [ logEntry ] )
120150
@@ -129,22 +159,34 @@ export class SessionCapturer {
129159 #patchProcessOutput( ) {
130160 const captureOutput = ( data : string | Uint8Array ) => {
131161 const text = typeof data === 'string' ? data : data . toString ( )
132- if ( ! text ?. trim ( ) ) return
162+ if ( ! text ?. trim ( ) ) {
163+ return
164+ }
133165
134- text . split ( '\n' )
135- . filter ( line => line . trim ( ) )
136- . forEach ( line => {
137- const logEntry = createLogEntry ( detectLogLevel ( line ) , [ stripAnsi ( line ) ] , LOG_SOURCES . TERMINAL )
166+ text
167+ . split ( '\n' )
168+ . filter ( ( line ) => line . trim ( ) )
169+ . forEach ( ( line ) => {
170+ const logEntry = createLogEntry (
171+ detectLogLevel ( line ) ,
172+ [ stripAnsi ( line ) ] ,
173+ LOG_SOURCES . TERMINAL
174+ )
138175 this . consoleLogs . push ( logEntry )
139176 this . sendUpstream ( 'consoleLogs' , [ logEntry ] )
140177 } )
141178 }
142179
143- const patchStream = ( stream : NodeJS . WriteStream , originalWrite : ( ...args : any [ ] ) => boolean ) => {
180+ const patchStream = (
181+ stream : NodeJS . WriteStream ,
182+ originalWrite : ( ...args : any [ ] ) => boolean
183+ ) => {
144184 const self = this
145- stream . write = function ( data : any , ...rest : any [ ] ) : boolean {
185+ stream . write = function ( data : any , ...rest : any [ ] ) : boolean {
146186 const result = originalWrite . call ( stream , data , ...rest )
147- if ( data && ! self . #isCapturingConsole) captureOutput ( data )
187+ if ( data && ! self . #isCapturingConsole) {
188+ captureOutput ( data )
189+ }
148190 return result
149191 } as any
150192 }
@@ -154,7 +196,7 @@ export class SessionCapturer {
154196 }
155197
156198 #restoreConsole( ) {
157- CONSOLE_METHODS . forEach ( method => {
199+ CONSOLE_METHODS . forEach ( ( method ) => {
158200 console [ method ] = this . #originalConsoleMethods[ method ]
159201 } )
160202 }
0 commit comments