@@ -7,19 +7,24 @@ const nocolor = {
7
7
cyan : s => s ,
8
8
magenta : s => s ,
9
9
blue : s => s ,
10
+ green : s => s ,
10
11
}
11
12
13
+ const { relative } = require ( 'path' )
14
+
12
15
const explainNode = ( node , depth , color ) =>
13
16
printNode ( node , color ) +
14
- explainDependents ( node , depth , color )
17
+ explainDependents ( node , depth , color ) +
18
+ explainLinksIn ( node , depth , color )
15
19
16
20
const colorType = ( type , color ) => {
17
- const { red, yellow, cyan, magenta, blue } = color ? chalk : nocolor
21
+ const { red, yellow, cyan, magenta, blue, green } = color ? chalk : nocolor
18
22
const style = type === 'extraneous' ? red
19
23
: type === 'dev' ? yellow
20
24
: type === 'optional' ? cyan
21
25
: type === 'peer' ? magenta
22
26
: type === 'bundled' ? blue
27
+ : type === 'workspace' ? green
23
28
: /* istanbul ignore next */ s => s
24
29
return style ( type )
25
30
}
@@ -34,8 +39,9 @@ const printNode = (node, color) => {
34
39
optional,
35
40
peer,
36
41
bundled,
42
+ isWorkspace,
37
43
} = node
38
- const { bold, dim } = color ? chalk : nocolor
44
+ const { bold, dim, green } = color ? chalk : nocolor
39
45
const extra = [ ]
40
46
if ( extraneous )
41
47
extra . push ( ' ' + bold ( colorType ( 'extraneous' , color ) ) )
@@ -52,10 +58,23 @@ const printNode = (node, color) => {
52
58
if ( bundled )
53
59
extra . push ( ' ' + bold ( colorType ( 'bundled' , color ) ) )
54
60
55
- return `${ bold ( name ) } @${ bold ( version ) } ${ extra . join ( '' ) } ` +
61
+ const pkgid = isWorkspace
62
+ ? green ( `${ name } @${ version } ` )
63
+ : `${ bold ( name ) } @${ bold ( version ) } `
64
+
65
+ return `${ pkgid } ${ extra . join ( '' ) } ` +
56
66
( location ? dim ( `\n${ location } ` ) : '' )
57
67
}
58
68
69
+ const explainLinksIn = ( { linksIn } , depth , color ) => {
70
+ if ( ! linksIn || ! linksIn . length || depth <= 0 )
71
+ return ''
72
+
73
+ const messages = linksIn . map ( link => explainNode ( link , depth - 1 , color ) )
74
+ const str = '\n' + messages . join ( '\n' )
75
+ return str . split ( '\n' ) . join ( '\n ' )
76
+ }
77
+
59
78
const explainDependents = ( { name, dependents } , depth , color ) => {
60
79
if ( ! dependents || ! dependents . length || depth <= 0 )
61
80
return ''
@@ -88,18 +107,23 @@ const explainDependents = ({ name, dependents }, depth, color) => {
88
107
89
108
const explainEdge = ( { name, type, bundled, from, spec } , depth , color ) => {
90
109
const { bold } = color ? chalk : nocolor
110
+ const dep = type === 'workspace'
111
+ ? bold ( relative ( from . location , spec . slice ( 'file:' . length ) ) )
112
+ : `${ bold ( name ) } @"${ bold ( spec ) } "`
113
+ const fromMsg = ` from ${ explainFrom ( from , depth , color ) } `
114
+
91
115
return ( type === 'prod' ? '' : `${ colorType ( type , color ) } ` ) +
92
116
( bundled ? `${ colorType ( 'bundled' , color ) } ` : '' ) +
93
- `${ bold ( name ) } @"${ bold ( spec ) } " from ` +
94
- explainFrom ( from , depth , color )
117
+ `${ dep } ${ fromMsg } `
95
118
}
96
119
97
120
const explainFrom = ( from , depth , color ) => {
98
121
if ( ! from . name && ! from . version )
99
122
return 'the root project'
100
123
101
124
return printNode ( from , color ) +
102
- explainDependents ( from , depth - 1 , color )
125
+ explainDependents ( from , depth - 1 , color ) +
126
+ explainLinksIn ( from , depth - 1 , color )
103
127
}
104
128
105
129
module . exports = { explainNode, printNode, explainEdge }
0 commit comments