4
4
*/
5
5
'use strict' ;
6
6
7
- var path = require ( 'path' ) ;
7
+ const path = require ( 'path' ) ;
8
8
9
- var PACKAGE_NAME = require ( '../../package.json' ) . name ;
9
+ const PACKAGE_NAME = require ( '../../package.json' ) . name ;
10
+
11
+ /**
12
+ * Paths are formatted to have posix style path separators and those within the CWD are made relative to CWD.
13
+ *
14
+ * @param {string } absolutePath An absolute path to format
15
+ * @returns {string } the formatted path
16
+ */
17
+ const pathToString = ( absolutePath ) => {
18
+ if ( absolutePath === '' ) {
19
+ return '-empty-' ;
20
+ } else {
21
+ const relative = path . relative ( process . cwd ( ) , absolutePath ) . split ( path . sep ) ;
22
+ const segments =
23
+ ( relative [ 0 ] !== '..' ) ? [ '.' ] . concat ( relative ) . filter ( Boolean ) :
24
+ ( relative . lastIndexOf ( '..' ) < 2 ) ? relative :
25
+ absolutePath . split ( path . sep ) ;
26
+ return segments . join ( '/' ) ;
27
+ }
28
+ } ;
29
+
30
+ exports . pathToString = pathToString ;
10
31
11
32
/**
12
33
* Format a debug message.
13
34
*
14
- * @param {string } file The file being processed by webpack
35
+ * @param {string } filename The file being processed by webpack
15
36
* @param {string } uri A uri path, relative or absolute
16
- * @param {Array<string> } bases Absolute base paths up to and including the found one
17
- * @param {boolean } isFound Indicates the last base was a positive match
37
+ * @param {Array<{base:string,joined:string,isSuccess:boolean}> } attempts An array of attempts, possibly empty
18
38
* @return {string } Formatted message
19
39
*/
20
- function formatJoinMessage ( file , uri , bases , isFound ) {
21
- return [ PACKAGE_NAME + ': ' + pathToString ( file ) + ': ' + uri ]
22
- . concat ( bases . map ( pathToString ) )
23
- . concat ( isFound ? 'FOUND' : 'NOT FOUND' )
24
- . join ( '\n ' ) ;
40
+ const formatJoinMessage = ( filename , uri , attempts ) => {
41
+ const attemptToCells = ( _ , i , array ) => {
42
+ const { base : prev } = ( i === 0 ) ? { } : array [ i - 1 ] ;
43
+ const { base : curr , joined } = array [ i ] ;
44
+ return [ ( curr === prev ) ? '' : pathToString ( curr ) , pathToString ( joined ) ] ;
45
+ } ;
25
46
26
- /**
27
- * If given path is within `process.cwd()` then show relative posix path, otherwise show absolute posix path.
28
- *
29
- * @param {string } absolute An absolute path
30
- * @return {string } A relative or absolute path
31
- */
32
- function pathToString ( absolute ) {
33
- if ( ! absolute ) {
34
- return '-empty-' ;
35
- } else {
36
- var relative = path . relative ( process . cwd ( ) , absolute )
37
- . split ( path . sep ) ;
47
+ const formatCells = ( lines ) => {
48
+ const maxWidth = lines . reduce ( ( max , [ cellA ] ) => Math . max ( max , cellA . length ) , 0 ) ;
49
+ return lines . map ( ( [ cellA , cellB ] ) => [ cellA . padEnd ( maxWidth ) , cellB ] ) . map ( ( cells ) => cells . join ( ' --> ' ) ) ;
50
+ } ;
38
51
39
- return ( ( relative [ 0 ] === '..' ) ? absolute . split ( path . sep ) : [ '.' ] . concat ( relative ) . filter ( Boolean ) )
40
- . join ( '/' ) ;
41
- }
42
- }
43
- }
52
+ return [ PACKAGE_NAME + ': ' + pathToString ( filename ) + ': ' + uri ]
53
+ . concat ( attempts . length === 0 ? '-empty-' : formatCells ( attempts . map ( attemptToCells ) ) )
54
+ . concat ( attempts . some ( ( { isSuccess } ) => isSuccess ) ? 'FOUND' : 'NOT FOUND' )
55
+ . join ( '\n ' ) ;
56
+ } ;
44
57
45
58
exports . formatJoinMessage = formatJoinMessage ;
46
59
@@ -56,20 +69,18 @@ exports.formatJoinMessage = formatJoinMessage;
56
69
* @param {function|boolean } debug A boolean or debug function
57
70
* @return {function(function, array):void } A logging function possibly degenerate
58
71
*/
59
- function createDebugLogger ( debug ) {
60
- var log = ! ! debug && ( ( typeof debug === 'function' ) ? debug : console . log ) ,
61
- cache = { } ;
62
- return log ? actuallyLog : noop ;
63
-
64
- function noop ( ) { }
65
-
66
- function actuallyLog ( msgFn , params ) {
67
- var key = Function . prototype . toString . call ( msgFn ) + JSON . stringify ( params ) ;
68
- if ( ! cache [ key ] ) {
69
- cache [ key ] = true ;
70
- log ( msgFn . apply ( null , params ) ) ;
71
- }
72
- }
73
- }
72
+ const createDebugLogger = ( debug ) => {
73
+ const log = ! ! debug && ( ( typeof debug === 'function' ) ? debug : console . log ) ;
74
+ const cache = { } ;
75
+ return log ?
76
+ ( ( msgFn , params ) => {
77
+ const key = Function . prototype . toString . call ( msgFn ) + JSON . stringify ( params ) ;
78
+ if ( ! cache [ key ] ) {
79
+ cache [ key ] = true ;
80
+ log ( msgFn . apply ( null , params ) ) ;
81
+ }
82
+ } ) :
83
+ ( ( ) => undefined ) ;
84
+ } ;
74
85
75
86
exports . createDebugLogger = createDebugLogger ;
0 commit comments