1
+ import fs from 'fs' ;
2
+ import path from 'path' ;
3
+
1
4
import { config } from './config' ;
5
+ import { getProgramPath } from './directories' ;
2
6
3
7
const colors = {
4
8
info : '\x1b[1m\x1b[40m\x1b[42m' ,
@@ -15,32 +19,59 @@ export function colorText(status: string) {
15
19
const timestamp = new Date ( ) . toISOString ( ) . split ( 'T' ) [ 1 ] . replace ( 'Z' , '' ) ;
16
20
17
21
const time = `${ colors . grey } ${ timestamp } ${ colors . reset } ` ;
18
- return `${ time } ${ colorCode } ${ status . toUpperCase ( ) } ${ colors . reset } ` ;
22
+ const version = `${ colors . grey } v${ config . currentVersion } ${ colors . reset } ` ;
23
+ return `${ time } ${ version } ${ colorCode } ${ status . toUpperCase ( ) } ${ colors . reset } ` ;
19
24
}
20
25
21
26
export const wLogger = {
22
27
info : ( ...args : any ) => {
23
28
const coloredText = colorText ( 'info' ) ;
24
29
console . log ( coloredText , ...args ) ;
30
+
31
+ if ( config . debugLogging === true ) writeLog ( 'info' , args ) ;
25
32
} ,
26
33
debug : ( ...args : any ) => {
27
34
if ( config . debugLogging !== true ) return ;
28
35
29
36
const coloredText = colorText ( 'debug' ) ;
30
37
console . log ( coloredText , ...args ) ;
38
+
39
+ writeLog ( 'debug' , args ) ;
31
40
} ,
32
41
debugError : ( ...args : any ) => {
33
42
if ( config . debugLogging !== true ) return ;
34
43
35
44
const coloredText = colorText ( 'debugError' ) ;
36
45
console . log ( coloredText , ...args ) ;
46
+
47
+ if ( config . debugLogging === true ) writeLog ( 'debugError' , args ) ;
37
48
} ,
38
49
error : ( ...args : any ) => {
39
50
const coloredText = colorText ( 'error' ) ;
40
51
console . log ( coloredText , ...args ) ;
52
+
53
+ if ( config . debugLogging === true ) writeLog ( 'error' , args ) ;
41
54
} ,
42
55
warn : ( ...args : any ) => {
43
56
const coloredText = colorText ( 'warn' ) ;
44
57
console . log ( coloredText , ...args ) ;
58
+
59
+ if ( config . debugLogging === true ) writeLog ( 'error' , args ) ;
45
60
}
46
61
} ;
62
+
63
+ function writeLog ( type : string , ...args ) {
64
+ if ( config . logsPath === '' ) {
65
+ const logsPath = path . join ( getProgramPath ( ) , 'logs' ) ;
66
+ if ( ! fs . existsSync ( logsPath ) )
67
+ fs . mkdirSync ( logsPath , { recursive : true } ) ;
68
+
69
+ config . logsPath = path . join ( logsPath , `${ Date . now ( ) } .txt` ) ;
70
+ }
71
+
72
+ fs . appendFileSync (
73
+ config . logsPath ,
74
+ `${ new Date ( ) . toISOString ( ) } ${ type } ${ args . join ( ' ' ) } \n` ,
75
+ 'utf8'
76
+ ) ;
77
+ }
0 commit comments