@@ -48,13 +48,6 @@ const getStdoutMethod = function(type) {
4848 return process . stdout . write . bind ( process . stdout ) ;
4949} ;
5050
51- const getNpmlogMethod = function ( type ) {
52- if ( [ 'debug' ] . includes ( type ) ) {
53- type = 'verbose' ;
54- }
55- return npmlog [ type ] . bind ( npmlog ) ;
56- } ;
57-
5851function dyeMessage ( type , message ) {
5952 switch ( type . toLowerCase ( ) ) {
6053 case 'debug' : {
@@ -81,7 +74,7 @@ function dyeMessage(type, message) {
8174 }
8275}
8376
84- const toString = {
77+ const format = {
8578 debug ( ) {
8679 const message = utils . format ( ...( arguments || [ ] ) ) ;
8780 return `${ chalk . bgMagenta ( ' DEBUG ' ) } ${ dyeMessage ( 'debug' , message ) } ${ os . EOL } ` ;
@@ -104,38 +97,31 @@ const toString = {
10497 } ,
10598} ;
10699
107- const getMethod = function ( type ) {
108- const logger = getNpmlogMethod ( type ) ;
109- return function ( ...args ) {
110- if ( args . length <= 1 ) {
111- return logger ( false , ...args . map ( arg => dyeMessage ( type , arg ) ) ) ;
112- }
113- return logger ( args [ 0 ] , ...args . splice ( 1 ) . map ( arg => dyeMessage ( type , arg ) ) ) ;
114- } ;
115- // const logger = getStdoutMethod(type);
116- // return function(...args) {
117- // return logger(toString[type].call(toString, ...args));
118- // };
119- } ;
100+ class Logger {
101+
102+ constructor ( log , { alias = new Map ( ) , customFormat = new Map ( ) } ) {
103+ this . npmlog = log ;
104+ this . aliasMap = new Map ( alias ) ;
105+ // 兼容
106+ this . customFormatMap = new Map ( customFormat ) ;
107+ }
120108
121- const logger = {
122- toString,
123109 debug ( ) {
124110 // if (!process.env.MICRO_APP_DEBUG_LOGGER) return; // 是否开启
125- return getMethod ( 'debug' ) ( ...arguments ) ;
126- } ,
111+ return this . getMethod ( 'debug' ) ( ...arguments ) ;
112+ }
127113 warn ( ) {
128- return getMethod ( 'warn' ) ( ...arguments ) ;
129- } ,
114+ return this . getMethod ( 'warn' ) ( ...arguments ) ;
115+ }
130116 error ( ) {
131- return getMethod ( 'error' ) ( ...arguments ) ;
132- } ,
117+ return this . getMethod ( 'error' ) ( ...arguments ) ;
118+ }
133119 info ( ) {
134- return getMethod ( 'info' ) ( ...arguments ) ;
135- } ,
120+ return this . getMethod ( 'info' ) ( ...arguments ) ;
121+ }
136122 success ( ) {
137- return getMethod ( 'success' ) ( ...arguments ) ;
138- } ,
123+ return this . getMethod ( 'success' ) ( ...arguments ) ;
124+ }
139125
140126 /**
141127 * spinner
@@ -150,7 +136,7 @@ const logger = {
150136 prefixText : `${ chalk . bgHex ( '#EE6B2C' ) ( ' PENDING ' ) } ` ,
151137 } ;
152138 return ora ( typeof message === 'string' ? defulatOpts : Object . assign ( { } , defulatOpts , message ) ) ;
153- } ,
139+ }
154140
155141 throw ( e ) {
156142 if ( e instanceof Error ) {
@@ -165,26 +151,83 @@ const logger = {
165151 getStdoutMethod ( 'error' ) ( chalk . grey ( stack . slice ( 2 ) . join ( os . EOL ) ) + os . EOL ) ;
166152 }
167153 process . exit ( 1 ) ;
168- } ,
154+ }
169155
170156 assert ( ...args ) {
171157 try {
172158 assert ( ...args ) ;
173159 } catch ( err ) {
174160 this . throw ( '[assert]' , err . message ) ;
175161 }
176- } ,
177- } ;
178- module . exports = new Proxy ( logger , {
179- get ( target , prop ) {
180- if ( ! ( prop in target ) && typeof prop === 'string' ) {
181- return getMethod ( prop ) . bind ( target ) ;
162+ }
163+
164+ getMethod ( type ) {
165+ const logger = this . getNpmlogMethod ( type ) ;
166+ return function ( ...args ) {
167+ if ( args . length <= 1 ) {
168+ return logger ( false , ...args . map ( arg => dyeMessage ( type , arg ) ) ) ;
169+ }
170+ return logger ( args [ 0 ] , ...args . splice ( 1 ) . map ( arg => dyeMessage ( type , arg ) ) ) ;
171+ } ;
172+ // const logger = getStdoutMethod(type);
173+ // return function(...args) {
174+ // return logger(format[type].call(format, ...args));
175+ // };
176+ }
177+
178+ getNpmlogMethod ( type ) {
179+ if ( [ 'debug' ] . includes ( type ) ) {
180+ type = 'verbose' ;
182181 }
183- return target [ prop ] ;
184- } ,
185- } ) ;
182+ return this . npmlog [ type ] . bind ( this . npmlog ) ;
183+ }
184+
185+ setAlias ( type , value ) {
186+ return this . aliasMap . set ( type , value ) ;
187+ }
188+
189+ removeAlias ( type ) {
190+ return this . aliasMap . delete ( type ) ;
191+ }
192+
193+ getAlias ( type ) {
194+ return this . aliasMap . get ( type ) ;
195+ }
196+
197+ newGroup ( name , ...args ) {
198+ const newLog = this . npmlog . newGroup ( name , ...args ) ;
199+ return factroy ( newLog , { alias : this . aliasMap , customFormat : this . customFormatMap } ) ;
200+ }
201+
202+ addCustomToString ( key , value ) {
203+ this . customFormatMap . set ( key , value ) ;
204+ }
205+
206+ get format ( ) { // 兼容 toString
207+ return new Proxy ( this , {
208+ get ( target , prop ) {
209+ return target . customFormatMap . get ( prop ) ;
210+ } ,
211+ } ) ;
212+ }
213+ }
214+
215+ function factroy ( log , opts = { } ) {
216+ return new Proxy ( new Logger ( log , opts ) , {
217+ get ( target , prop ) {
218+ if ( ! ( prop in target ) && typeof prop === 'string' && ( prop in npmlog ) ) {
219+ return target . getMethod ( prop ) ;
220+ }
221+ const alias = target . getAlias ( prop ) ;
222+ if ( alias ) {
223+ return target . getMethod ( alias ) ;
224+ }
225+ return target [ prop ] ;
226+ } ,
227+ } ) ;
228+ }
229+
230+ module . exports = factroy ( npmlog , { customFormat : Object . entries ( format ) } ) ;
186231
187- module . exports . npmlog = npmlog ;
188232module . exports . getStdoutMethod = getStdoutMethod ;
189- module . exports . getNpmlogMethod = getNpmlogMethod ;
190- module . exports . getMethod = getMethod ;
233+
0 commit comments