Skip to content

Commit 284d9ea

Browse files
RandomByteKlattG
andauthored
[FEATURE] ui5 tree: Add 'level' and 'flat' parameters (#635)
Also introduce aliases 'ui5 ls' and 'ui5 list' (similar to npm commands) `--flat`: Instead of rendering a tree hierarchy, output all dependencies in a flat list `--level`: When rendering the tree hierarchy, limit the number of levels shown --------- Co-authored-by: Günter Klatt <[email protected]>
1 parent 61d0865 commit 284d9ea

File tree

2 files changed

+272
-31
lines changed

2 files changed

+272
-31
lines changed

lib/cli/commands/tree.js

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import chalk from "chalk";
44

55
const tree = {
66
command: "tree",
7+
aliases: ["ls", "list"],
78
describe:
89
"Outputs the dependency tree of the current project to stdout. " +
910
"It takes all relevant parameters of ui5 build into account.",
@@ -12,6 +13,15 @@ const tree = {
1213

1314
tree.builder = function(cli) {
1415
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+
})
1525
.option("framework-version", {
1626
describe:
1727
"Overrides the framework version defined by the project",
@@ -56,18 +66,31 @@ tree.handler = async function(argv) {
5666
elapsedTime = await getElapsedTime(startTime);
5767
}
5868

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();
6079
const indentWidth = 4;
6180
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);
6584
connectorIndices.forEach((idx) => {
6685
baseString = `${baseString.slice(0, idx)}${baseString.slice(idx + 1)}`;
6786
});
6887
const connectorString = lastChild ? "╰─" : "├─";
88+
let name = chalk.bold(project.getName());
89+
if (project.isFrameworkProject()) {
90+
name = chalk.blue(name);
91+
}
6992
console.log(
70-
`${baseString}${connectorString} ${chalk.bold(project.getName())} ` +
93+
`${baseString}${connectorString} ${name} ` +
7194
`${project.getNamespace() ? chalk.inverse(project.getNamespace()) + " " : ""}` +
7295
chalk.dim(`(${project.getVersion()}, ${project.getType()}) `) +
7396
chalk.dim.italic(`${project.getRootPath()}`)
@@ -76,18 +99,39 @@ tree.handler = async function(argv) {
7699
const lastIdx = dependencies.length - 1;
77100
const newConnectorIndices = [...connectorIndices];
78101
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+
});
80118
}
81-
dependencies.forEach((dep, i) => {
82-
projects[dep].render(indentation + 1, newConnectorIndices, i === lastIdx);
83-
});
84119
}
85-
};
120+
});
86121
});
87122

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+
}
91135
console.log("");
92136

93137
const extensionNames = graph.getExtensionNames();

0 commit comments

Comments
 (0)