* Print files as a tree
(
files: { path: string; language: string; nodeCount: number }[],
includeMetadata: boolean,
maxDepth: number | undefined,
chalk: { dim: (s: string) => string; cyan: (s: string) => string }
)
| 1371 | * Print files as a tree |
| 1372 | */ |
| 1373 | function printFileTree( |
| 1374 | files: { path: string; language: string; nodeCount: number }[], |
| 1375 | includeMetadata: boolean, |
| 1376 | maxDepth: number | undefined, |
| 1377 | chalk: { dim: (s: string) => string; cyan: (s: string) => string } |
| 1378 | ): void { |
| 1379 | interface TreeNode { |
| 1380 | name: string; |
| 1381 | children: Map<string, TreeNode>; |
| 1382 | file?: { language: string; nodeCount: number }; |
| 1383 | } |
| 1384 | |
| 1385 | const root: TreeNode = { name: '', children: new Map() }; |
| 1386 | |
| 1387 | for (const file of files) { |
| 1388 | const parts = file.path.split('/'); |
| 1389 | let current = root; |
| 1390 | |
| 1391 | for (let i = 0; i < parts.length; i++) { |
| 1392 | const part = parts[i]; |
| 1393 | if (!part) continue; |
| 1394 | |
| 1395 | if (!current.children.has(part)) { |
| 1396 | current.children.set(part, { name: part, children: new Map() }); |
| 1397 | } |
| 1398 | current = current.children.get(part)!; |
| 1399 | |
| 1400 | if (i === parts.length - 1) { |
| 1401 | current.file = { language: file.language, nodeCount: file.nodeCount }; |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => { |
| 1407 | if (maxDepth !== undefined && depth > maxDepth) return; |
| 1408 | |
| 1409 | const glyphs = getGlyphs(); |
| 1410 | const connector = isLast ? glyphs.treeLast : glyphs.treeBranch; |
| 1411 | const childPrefix = isLast ? ' ' : glyphs.treePipe; |
| 1412 | |
| 1413 | if (node.name) { |
| 1414 | let line = prefix + connector + node.name; |
| 1415 | if (node.file && includeMetadata) { |
| 1416 | line += chalk.dim(` (${node.file.language}, ${node.file.nodeCount} symbols)`); |
| 1417 | } |
| 1418 | console.log(line); |
| 1419 | } |
| 1420 | |
| 1421 | const children = [...node.children.values()]; |
| 1422 | children.sort((a, b) => { |
| 1423 | const aIsDir = a.children.size > 0 && !a.file; |
| 1424 | const bIsDir = b.children.size > 0 && !b.file; |
| 1425 | if (aIsDir !== bIsDir) return aIsDir ? -1 : 1; |
| 1426 | return a.name.localeCompare(b.name); |
| 1427 | }); |
| 1428 | |
| 1429 | for (let i = 0; i < children.length; i++) { |
| 1430 | const child = children[i]!; |
no test coverage detected