@@ -7,49 +7,14 @@ import { join } from 'node:path'
7
7
import process from 'node:process'
8
8
import { pipeline } from 'node:stream/promises'
9
9
import { createGzip } from 'node:zlib'
10
-
11
10
import { config as defaultConfig } from './config'
12
11
import { JsonFormatter } from './formatters/json'
12
+ import { bgRed , bgYellow , blue , bold , green , styles , white } from './style'
13
13
import { isBrowserProcess } from './utils'
14
14
15
15
// Define missing types
16
16
type BunTimer = ReturnType < typeof setTimeout >
17
17
18
- // Add terminal styling utilities
19
- const terminalStyles = {
20
- // Text colors
21
- red : ( text : string ) => `\x1B[31m${ text } \x1B[0m` ,
22
- green : ( text : string ) => `\x1B[32m${ text } \x1B[0m` ,
23
- yellow : ( text : string ) => `\x1B[33m${ text } \x1B[0m` ,
24
- blue : ( text : string ) => `\x1B[34m${ text } \x1B[0m` ,
25
- magenta : ( text : string ) => `\x1B[35m${ text } \x1B[0m` ,
26
- cyan : ( text : string ) => `\x1B[36m${ text } \x1B[0m` ,
27
- white : ( text : string ) => `\x1B[37m${ text } \x1B[0m` ,
28
- gray : ( text : string ) => `\x1B[90m${ text } \x1B[0m` ,
29
-
30
- // Background colors
31
- bgRed : ( text : string ) => `\x1B[41m${ text } \x1B[0m` ,
32
- bgYellow : ( text : string ) => `\x1B[43m${ text } \x1B[0m` ,
33
-
34
- // Text styles
35
- bold : ( text : string ) => `\x1B[1m${ text } \x1B[0m` ,
36
- dim : ( text : string ) => `\x1B[2m${ text } \x1B[0m` ,
37
- italic : ( text : string ) => `\x1B[3m${ text } \x1B[0m` ,
38
- underline : ( text : string ) => `\x1B[4m${ text } \x1B[0m` ,
39
-
40
- // Reset
41
- reset : '\x1B[0m' ,
42
- }
43
-
44
- // Log level icons/badges similar to consola
45
- const levelIcons = {
46
- debug : '🔍' ,
47
- info : terminalStyles . blue ( 'ℹ' ) ,
48
- success : terminalStyles . green ( '✓' ) ,
49
- warning : terminalStyles . bgYellow ( terminalStyles . white ( terminalStyles . bold ( ' WARN ' ) ) ) , // Use badge style for warnings
50
- error : terminalStyles . bgRed ( terminalStyles . white ( terminalStyles . bold ( ' ERROR ' ) ) ) , // Use badge style for errors
51
- }
52
-
53
18
interface FingersCrossedConfig {
54
19
activationLevel : LogLevel
55
20
bufferSize : number
@@ -85,6 +50,15 @@ interface NetworkError extends Error {
85
50
code ?: string
86
51
}
87
52
53
+ // Log level icons/badges similar to consola
54
+ const levelIcons = {
55
+ debug : '🔍' ,
56
+ info : blue ( 'ℹ' ) ,
57
+ success : green ( '✓' ) ,
58
+ warning : bgYellow ( white ( bold ( ' WARN ' ) ) ) , // Use badge style for warnings
59
+ error : bgRed ( white ( bold ( ' ERROR ' ) ) ) , // Use badge style for errors
60
+ }
61
+
88
62
export class Logger {
89
63
private name : string
90
64
private fileLocks : Map < string , number > = new Map ( )
@@ -857,7 +831,7 @@ export class Logger {
857
831
}
858
832
859
833
private formatConsoleTimestamp ( date : Date ) : string {
860
- return this . fancy ? terminalStyles . gray ( date . toLocaleTimeString ( ) ) : date . toLocaleTimeString ( )
834
+ return this . fancy ? styles . gray ( date . toLocaleTimeString ( ) ) : date . toLocaleTimeString ( )
861
835
}
862
836
863
837
private formatConsoleMessage ( parts : { timestamp : string , icon ?: string , tag ?: string , message : string , level ?: LogLevel , showTimestamp ?: boolean } ) : string {
@@ -898,7 +872,7 @@ export class Logger {
898
872
}
899
873
else {
900
874
// For other levels, apply standard formatting
901
- mainPart = `${ icon } ${ tag } ${ terminalStyles . cyan ( message ) } `
875
+ mainPart = `${ icon } ${ tag } ${ styles . cyan ( message ) } `
902
876
}
903
877
904
878
// If we don't need to show timestamp, just return the message
@@ -957,9 +931,6 @@ export class Logger {
957
931
}
958
932
959
933
private async log ( level : LogLevel , message : string | Error , ...args : any [ ] ) : Promise < void > {
960
- if ( ! this . shouldLog ( level ) )
961
- return
962
-
963
934
const timestamp = new Date ( )
964
935
const consoleTime = this . formatConsoleTimestamp ( timestamp )
965
936
const fileTime = this . formatFileTimestamp ( timestamp )
@@ -979,7 +950,7 @@ export class Logger {
979
950
// Format console output
980
951
if ( this . fancy && ! isBrowserProcess ( ) ) {
981
952
const icon = levelIcons [ level ]
982
- const tag = this . options . showTags !== false && this . name ? terminalStyles . gray ( this . formatTag ( this . name ) ) : ''
953
+ const tag = this . options . showTags !== false && this . name ? styles . gray ( this . formatTag ( this . name ) ) : ''
983
954
984
955
// Format the console output based on log level
985
956
let consoleMessage : string
@@ -989,7 +960,7 @@ export class Logger {
989
960
timestamp : consoleTime ,
990
961
icon,
991
962
tag,
992
- message : terminalStyles . gray ( formattedMessage ) ,
963
+ message : styles . gray ( formattedMessage ) ,
993
964
level,
994
965
} )
995
966
console . error ( consoleMessage )
@@ -1009,7 +980,7 @@ export class Logger {
1009
980
timestamp : consoleTime ,
1010
981
icon,
1011
982
tag,
1012
- message : terminalStyles . green ( formattedMessage ) ,
983
+ message : styles . green ( formattedMessage ) ,
1013
984
level,
1014
985
} )
1015
986
console . error ( consoleMessage )
@@ -1040,7 +1011,7 @@ export class Logger {
1040
1011
if ( line . trim ( ) && ! line . includes ( formattedMessage ) ) {
1041
1012
console . error ( this . formatConsoleMessage ( {
1042
1013
timestamp : consoleTime ,
1043
- message : terminalStyles . gray ( ` ${ line } ` ) ,
1014
+ message : styles . gray ( ` ${ line } ` ) ,
1044
1015
level,
1045
1016
showTimestamp : false , // Don't show timestamp for stack traces
1046
1017
} ) )
@@ -1058,11 +1029,15 @@ export class Logger {
1058
1029
}
1059
1030
}
1060
1031
1032
+ if ( ! this . shouldLog ( level ) )
1033
+ return
1034
+
1061
1035
// Create the log entry for file logging
1062
1036
let logEntry = `${ fileTime } ${ this . environment } .${ level . toUpperCase ( ) } : ${ formattedMessage } \n`
1063
1037
if ( errorStack ) {
1064
1038
logEntry += `${ errorStack } \n`
1065
1039
}
1040
+ logEntry = logEntry . replace ( this . ANSI_PATTERN , '' )
1066
1041
1067
1042
// Write to file
1068
1043
await this . writeToFile ( logEntry )
@@ -1078,13 +1053,13 @@ export class Logger {
1078
1053
1079
1054
// Show start message with spinner-like indicator
1080
1055
if ( this . fancy && ! isBrowserProcess ( ) ) {
1081
- const tag = this . options . showTags !== false && this . name ? terminalStyles . gray ( this . formatTag ( this . name ) ) : ''
1056
+ const tag = this . options . showTags !== false && this . name ? styles . gray ( this . formatTag ( this . name ) ) : ''
1082
1057
const consoleTime = this . formatConsoleTimestamp ( new Date ( ) )
1083
1058
console . error ( this . formatConsoleMessage ( {
1084
1059
timestamp : consoleTime ,
1085
- icon : terminalStyles . blue ( '◐' ) ,
1060
+ icon : styles . blue ( '◐' ) ,
1086
1061
tag,
1087
- message : `${ terminalStyles . cyan ( label ) } ...` ,
1062
+ message : `${ styles . cyan ( label ) } ...` ,
1088
1063
} ) )
1089
1064
}
1090
1065
@@ -1108,13 +1083,14 @@ export class Logger {
1108
1083
logEntry += ` ${ JSON . stringify ( metadata ) } `
1109
1084
}
1110
1085
logEntry += '\n'
1086
+ logEntry = logEntry . replace ( this . ANSI_PATTERN , '' )
1111
1087
1112
1088
// Show completion message with tag based on showTags option
1113
1089
if ( this . fancy && ! isBrowserProcess ( ) ) {
1114
- const tag = this . options . showTags !== false && this . name ? terminalStyles . gray ( this . formatTag ( this . name ) ) : ''
1090
+ const tag = this . options . showTags !== false && this . name ? styles . gray ( this . formatTag ( this . name ) ) : ''
1115
1091
console . error ( this . formatConsoleMessage ( {
1116
1092
timestamp : consoleTime ,
1117
- icon : terminalStyles . green ( '✓' ) ,
1093
+ icon : styles . green ( '✓' ) ,
1118
1094
tag,
1119
1095
message : `${ completionMessage } ${ metadata ? ` ${ JSON . stringify ( metadata ) } ` : '' } ` ,
1120
1096
} ) )
@@ -1367,24 +1343,24 @@ export class Logger {
1367
1343
if ( this . options . showTags !== false && this . name ) {
1368
1344
console . error ( this . formatConsoleMessage ( {
1369
1345
timestamp : consoleTime ,
1370
- message : terminalStyles . gray ( this . formatTag ( this . name ) ) ,
1346
+ message : styles . gray ( this . formatTag ( this . name ) ) ,
1371
1347
showTimestamp : false ,
1372
1348
} ) )
1373
1349
}
1374
1350
1375
1351
// Show timestamp only on the first line of the box
1376
1352
console . error ( this . formatConsoleMessage ( {
1377
1353
timestamp : consoleTime ,
1378
- message : terminalStyles . cyan ( top ) ,
1354
+ message : styles . cyan ( top ) ,
1379
1355
} ) )
1380
1356
boxedLines . forEach ( line => console . error ( this . formatConsoleMessage ( {
1381
1357
timestamp : consoleTime ,
1382
- message : terminalStyles . cyan ( line ) ,
1358
+ message : styles . cyan ( line ) ,
1383
1359
showTimestamp : false ,
1384
1360
} ) ) )
1385
1361
console . error ( this . formatConsoleMessage ( {
1386
1362
timestamp : consoleTime ,
1387
- message : terminalStyles . cyan ( bottom ) ,
1363
+ message : styles . cyan ( bottom ) ,
1388
1364
showTimestamp : false ,
1389
1365
} ) )
1390
1366
}
@@ -1394,7 +1370,7 @@ export class Logger {
1394
1370
}
1395
1371
1396
1372
// Write directly to file instead of using this.info()
1397
- const logEntry = `${ fileTime } ${ this . environment } .INFO: [BOX] ${ message } \n`
1373
+ const logEntry = `${ fileTime } ${ this . environment } .INFO: [BOX] ${ message } \n` . replace ( this . ANSI_PATTERN , '' )
1398
1374
await this . writeToFile ( logEntry )
1399
1375
}
1400
1376
@@ -1412,7 +1388,7 @@ export class Logger {
1412
1388
1413
1389
return new Promise ( ( resolve ) => {
1414
1390
// Use console.error for display
1415
- console . error ( `${ terminalStyles . cyan ( '?' ) } ${ message } (y/n) ` )
1391
+ console . error ( `${ styles . cyan ( '?' ) } ${ message } (y/n) ` )
1416
1392
1417
1393
const onData = ( data : Buffer ) => {
1418
1394
const input = data . toString ( ) . trim ( ) . toLowerCase ( )
@@ -1522,15 +1498,15 @@ export class Logger {
1522
1498
// Console output with fancy formatting
1523
1499
if ( this . fancy && ! isBrowserProcess ( ) ) {
1524
1500
// Make tag optional based on showTags property and use custom format
1525
- const tag = this . options . showTags !== false && this . name ? terminalStyles . gray ( this . formatTag ( this . name ) ) : ''
1526
- const spinnerChar = terminalStyles . blue ( '◐' )
1527
- console . error ( `${ spinnerChar } ${ tag } ${ terminalStyles . cyan ( formattedMessage ) } ` )
1501
+ const tag = this . options . showTags !== false && this . name ? styles . gray ( this . formatTag ( this . name ) ) : ''
1502
+ const spinnerChar = styles . blue ( '◐' )
1503
+ console . error ( `${ spinnerChar } ${ tag } ${ styles . cyan ( formattedMessage ) } ` )
1528
1504
}
1529
1505
1530
1506
// Log to file directly instead of using this.info()
1531
1507
const timestamp = new Date ( )
1532
1508
const formattedDate = timestamp . toISOString ( )
1533
- const logEntry = `[${ formattedDate } ] ${ this . environment } .INFO: [START] ${ formattedMessage } \n`
1509
+ const logEntry = `[${ formattedDate } ] ${ this . environment } .INFO: [START] ${ formattedMessage } \n` . replace ( this . ANSI_PATTERN , '' )
1534
1510
1535
1511
await this . writeToFile ( logEntry )
1536
1512
}
@@ -1634,18 +1610,18 @@ export class Logger {
1634
1610
const filledLength = Math . round ( ( barState . barLength * percent ) / 100 )
1635
1611
const emptyLength = barState . barLength - filledLength
1636
1612
1637
- const filledBar = terminalStyles . green ( '━' . repeat ( filledLength ) )
1638
- const emptyBar = terminalStyles . gray ( '━' . repeat ( emptyLength ) )
1613
+ const filledBar = styles . green ( '━' . repeat ( filledLength ) )
1614
+ const emptyBar = styles . gray ( '━' . repeat ( emptyLength ) )
1639
1615
const bar = `[${ filledBar } ${ emptyBar } ]`
1640
1616
1641
1617
const percentageText = `${ percent } %` . padStart ( 4 )
1642
1618
const messageText = barState . message ? ` ${ barState . message } ` : ''
1643
1619
1644
1620
// Use a simpler icon for progress
1645
- const icon = isFinished || percent === 100 ? terminalStyles . green ( '✓' ) : terminalStyles . blue ( '▶' )
1621
+ const icon = isFinished || percent === 100 ? styles . green ( '✓' ) : styles . blue ( '▶' )
1646
1622
1647
1623
// Add tag if enabled
1648
- const tag = this . options . showTags !== false && this . name ? ` ${ terminalStyles . gray ( this . formatTag ( this . name ) ) } ` : ''
1624
+ const tag = this . options . showTags !== false && this . name ? ` ${ styles . gray ( this . formatTag ( this . name ) ) } ` : ''
1649
1625
1650
1626
const line = `\r${ icon } ${ tag } ${ bar } ${ percentageText } ${ messageText } `
1651
1627
0 commit comments