@@ -4,6 +4,7 @@ import chalk from "chalk";
4
4
5
5
const tree = {
6
6
command : "tree" ,
7
+ aliases : [ "ls" , "list" ] ,
7
8
describe :
8
9
"Outputs the dependency tree of the current project to stdout. " +
9
10
"It takes all relevant parameters of ui5 build into account." ,
@@ -12,6 +13,15 @@ const tree = {
12
13
13
14
tree . builder = function ( cli ) {
14
15
return cli
16
+ . option ( "flat" , {
17
+ describe : "Output a flat list of all dependencies instead of a tree hierarchy" ,
18
+ type : "boolean" ,
19
+ default : false
20
+ } )
21
+ . option ( "level" , {
22
+ describe : "Limit the number of levels shown in the tree hierarchy" ,
23
+ type : "number"
24
+ } )
15
25
. option ( "framework-version" , {
16
26
describe :
17
27
"Overrides the framework version defined by the project" ,
@@ -56,18 +66,31 @@ tree.handler = async function(argv) {
56
66
elapsedTime = await getElapsedTime ( startTime ) ;
57
67
}
58
68
59
- const projects = Object . create ( null ) ;
69
+ let requestedLevels ;
70
+ if ( argv . level !== undefined && isNaN ( argv . level ) ) {
71
+ throw new Error ( `The provided 'level' option is not a number` ) ;
72
+ } else if ( argv . level !== undefined ) {
73
+ requestedLevels = argv . level ;
74
+ } else {
75
+ requestedLevels = Infinity ;
76
+ }
77
+
78
+ const projects = new Map ( ) ;
60
79
const indentWidth = 4 ;
61
80
await graph . traverseBreadthFirst ( async ( { project, dependencies} ) => {
62
- projects [ project . getName ( ) ] = {
63
- render : function ( indentation , connectorIndices , lastChild ) {
64
- let baseString = " " . repeat ( indentation * indentWidth ) ;
81
+ projects . set ( project . getName ( ) , {
82
+ render : function ( level , connectorIndices , lastChild , renderDeps = true ) {
83
+ let baseString = " " . repeat ( level * indentWidth ) ;
65
84
connectorIndices . forEach ( ( idx ) => {
66
85
baseString = `${ baseString . slice ( 0 , idx ) } │${ baseString . slice ( idx + 1 ) } ` ;
67
86
} ) ;
68
87
const connectorString = lastChild ? "╰─" : "├─" ;
88
+ let name = chalk . bold ( project . getName ( ) ) ;
89
+ if ( project . isFrameworkProject ( ) ) {
90
+ name = chalk . blue ( name ) ;
91
+ }
69
92
console . log (
70
- `${ baseString } ${ connectorString } ${ chalk . bold ( project . getName ( ) ) } ` +
93
+ `${ baseString } ${ connectorString } ${ name } ` +
71
94
`${ project . getNamespace ( ) ? chalk . inverse ( project . getNamespace ( ) ) + " " : "" } ` +
72
95
chalk . dim ( `(${ project . getVersion ( ) } , ${ project . getType ( ) } ) ` ) +
73
96
chalk . dim . italic ( `${ project . getRootPath ( ) } ` )
@@ -76,18 +99,39 @@ tree.handler = async function(argv) {
76
99
const lastIdx = dependencies . length - 1 ;
77
100
const newConnectorIndices = [ ...connectorIndices ] ;
78
101
if ( ! lastChild ) {
79
- newConnectorIndices . push ( indentation * indentWidth ) ;
102
+ newConnectorIndices . push ( level * indentWidth ) ;
103
+ }
104
+
105
+ if ( level >= requestedLevels ) {
106
+ const msg = chalk . dim . italic ( `Dependencies below Level ${ level } are hidden` ) ;
107
+ let nextBaseString = " " . repeat ( ( level + 1 ) * indentWidth ) ;
108
+ newConnectorIndices . forEach ( ( idx ) => {
109
+ nextBaseString = `${ nextBaseString . slice ( 0 , idx ) } │${ nextBaseString . slice ( idx + 1 ) } ` ;
110
+ } ) ;
111
+ console . log ( `${ nextBaseString } ╰─ ${ msg } ` ) ;
112
+ return ;
113
+ }
114
+ if ( renderDeps ) {
115
+ dependencies . forEach ( ( dep , i ) => {
116
+ projects . get ( dep ) . render ( level + 1 , newConnectorIndices , i === lastIdx ) ;
117
+ } ) ;
80
118
}
81
- dependencies . forEach ( ( dep , i ) => {
82
- projects [ dep ] . render ( indentation + 1 , newConnectorIndices , i === lastIdx ) ;
83
- } ) ;
84
119
}
85
- } ;
120
+ } ) ;
86
121
} ) ;
87
122
88
- const projectKeys = Object . keys ( projects ) ;
89
- console . log ( chalk . bold . underline ( `Dependencies (${ projectKeys . length } ):` ) ) ;
90
- projects [ projectKeys [ 0 ] ] . render ( 0 , [ ] , true ) ;
123
+ console . log ( chalk . bold . underline ( `Dependencies (${ projects . size } ):` ) ) ;
124
+ if ( argv . flat ) {
125
+ // Iterate over list of projects, rendering each individually
126
+ // We need to transform the map into an array in order to know the index
127
+ // for determining whether we are rendering the last entry (lastChild param)
128
+ Array . from ( projects . values ( ) ) . forEach ( ( { render : renderProject } , idx , arr ) => {
129
+ renderProject ( 0 , [ ] , idx == arr . length - 1 , false ) ;
130
+ } ) ;
131
+ } else {
132
+ // Recursively render the tree, starting with the first entry of the map
133
+ projects . values ( ) . next ( ) . value . render ( 0 , [ ] , true ) ;
134
+ }
91
135
console . log ( "" ) ;
92
136
93
137
const extensionNames = graph . getExtensionNames ( ) ;
0 commit comments