* 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 }
)
| 1709 | * Print files as a tree |
| 1710 | */ |
| 1711 | function printFileTree( |
| 1712 | files: { path: string; language: string; nodeCount: number }[], |
| 1713 | includeMetadata: boolean, |
| 1714 | maxDepth: number | undefined, |
| 1715 | chalk: { dim: (s: string) => string; cyan: (s: string) => string } |
| 1716 | ): void { |
| 1717 | interface TreeNode { |
| 1718 | name: string; |
| 1719 | children: Map<string, TreeNode>; |
| 1720 | file?: { language: string; nodeCount: number }; |
| 1721 | } |
| 1722 | |
| 1723 | const root: TreeNode = { name: '', children: new Map() }; |
| 1724 | |
| 1725 | for (const file of files) { |
| 1726 | const parts = file.path.split('/'); |
| 1727 | let current = root; |
| 1728 | |
| 1729 | for (let i = 0; i < parts.length; i++) { |
| 1730 | const part = parts[i]; |
| 1731 | if (!part) continue; |
| 1732 | |
| 1733 | if (!current.children.has(part)) { |
| 1734 | current.children.set(part, { name: part, children: new Map() }); |
| 1735 | } |
| 1736 | current = current.children.get(part)!; |
| 1737 | |
| 1738 | if (i === parts.length - 1) { |
| 1739 | current.file = { language: file.language, nodeCount: file.nodeCount }; |
| 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => { |
| 1745 | if (maxDepth !== undefined && depth > maxDepth) return; |
| 1746 | |
| 1747 | const glyphs = getGlyphs(); |
| 1748 | const connector = isLast ? glyphs.treeLast : glyphs.treeBranch; |
| 1749 | const childPrefix = isLast ? ' ' : glyphs.treePipe; |
| 1750 | |
| 1751 | if (node.name) { |
| 1752 | let line = prefix + connector + node.name; |
| 1753 | if (node.file && includeMetadata) { |
| 1754 | line += chalk.dim(` (${node.file.language}, ${node.file.nodeCount} symbols)`); |
| 1755 | } |
| 1756 | console.log(line); |
| 1757 | } |
| 1758 | |
| 1759 | const children = [...node.children.values()]; |
| 1760 | children.sort((a, b) => { |
| 1761 | const aIsDir = a.children.size > 0 && !a.file; |
| 1762 | const bIsDir = b.children.size > 0 && !b.file; |
| 1763 | if (aIsDir !== bIsDir) return aIsDir ? -1 : 1; |
| 1764 | return a.name.localeCompare(b.name); |
| 1765 | }); |
| 1766 | |
| 1767 | for (let i = 0; i < children.length; i++) { |
| 1768 | const child = children[i]!; |
no test coverage detected