UNPKG

vils

Version:

Recursively outputs file paths and contents in various formats

32 lines (31 loc) 1.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TreeFormatter = void 0; class TreeFormatter { format(entries) { const tree = {}; for (const e of entries) { const parts = e.relativePath.split(/[\\/]/); let node = tree; for (let i = 0; i < parts.length; i++) { const part = parts[i]; if (!node[part]) node[part] = i === parts.length - 1 ? null : {}; if (node[part] !== null) node = node[part]; } } function render(node, prefix = '') { return Object.entries(node).map(([name, child], idx, arr) => { const isLast = idx === arr.length - 1; const pointer = isLast ? '└── ' : '├── '; const newPrefix = prefix + (isLast ? ' ' : '│ '); if (child === null) return `${prefix}${pointer}${name}`; return `${prefix}${pointer}${name}\n${render(child, newPrefix)}`; }).join('\n'); } return render(tree); } } exports.TreeFormatter = TreeFormatter;