* 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 }
)
| 2154 | * Print files as a tree |
| 2155 | */ |
| 2156 | function printFileTree( |
| 2157 | files: { path: string; language: string; nodeCount: number }[], |
| 2158 | includeMetadata: boolean, |
| 2159 | maxDepth: number | undefined, |
| 2160 | chalk: { dim: (s: string) => string; cyan: (s: string) => string } |
| 2161 | ): void { |
| 2162 | interface TreeNode { |
| 2163 | name: string; |
| 2164 | children: Map<string, TreeNode>; |
| 2165 | file?: { language: string; nodeCount: number }; |
| 2166 | } |
| 2167 | |
| 2168 | const root: TreeNode = { name: '', children: new Map() }; |
| 2169 | |
| 2170 | for (const file of files) { |
| 2171 | const parts = file.path.split('/'); |
| 2172 | let current = root; |
| 2173 | |
| 2174 | for (let i = 0; i < parts.length; i++) { |
| 2175 | const part = parts[i]; |
| 2176 | if (!part) continue; |
| 2177 | |
| 2178 | if (!current.children.has(part)) { |
| 2179 | current.children.set(part, { name: part, children: new Map() }); |
| 2180 | } |
| 2181 | current = current.children.get(part)!; |
| 2182 | |
| 2183 | if (i === parts.length - 1) { |
| 2184 | current.file = { language: file.language, nodeCount: file.nodeCount }; |
| 2185 | } |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | const renderNode = (node: TreeNode, prefix: string, isLast: boolean, depth: number): void => { |
| 2190 | if (maxDepth !== undefined && depth > maxDepth) return; |
| 2191 | |
| 2192 | const glyphs = getGlyphs(); |
| 2193 | const connector = isLast ? glyphs.treeLast : glyphs.treeBranch; |
| 2194 | const childPrefix = isLast ? ' ' : glyphs.treePipe; |
| 2195 | |
| 2196 | if (node.name) { |
| 2197 | let line = prefix + connector + node.name; |
| 2198 | if (node.file && includeMetadata) { |
| 2199 | line += chalk.dim(` (${node.file.language}, ${node.file.nodeCount} symbols)`); |
| 2200 | } |
| 2201 | console.log(line); |
| 2202 | } |
| 2203 | |
| 2204 | const children = [...node.children.values()]; |
| 2205 | children.sort((a, b) => { |
| 2206 | const aIsDir = a.children.size > 0 && !a.file; |
| 2207 | const bIsDir = b.children.size > 0 && !b.file; |
| 2208 | if (aIsDir !== bIsDir) return aIsDir ? -1 : 1; |
| 2209 | return a.name.localeCompare(b.name); |
| 2210 | }); |
| 2211 | |
| 2212 | for (let i = 0; i < children.length; i++) { |
| 2213 | const child = children[i]!; |
no test coverage detected