|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from "fs/promises"; |
| 3 | +import path from "path"; |
| 4 | +import process from "process"; |
| 5 | + |
| 6 | +// ANSI colors |
| 7 | +const colors = { |
| 8 | + reset: "\x1b[0m", |
| 9 | + blue: "\x1b[34m", // directories |
| 10 | + white: "\x1b[37m", // files |
| 11 | + cyan: "\x1b[36m", // symlinks |
| 12 | +}; |
| 13 | + |
| 14 | +// -------------------- |
| 15 | +// HELP MESSAGE |
| 16 | +// -------------------- |
| 17 | +function showHelp() { |
| 18 | + console.log(` |
| 19 | +Usage: better-tree [directory] [options] |
| 20 | +
|
| 21 | +Options: |
| 22 | + --ignore <patterns...> Ignore matching files/folders (supports *, **) |
| 23 | + --depth <n> Limit recursion depth |
| 24 | + --files-only Show only files |
| 25 | + --dirs-only Show only directories |
| 26 | + --help Show this help message |
| 27 | +
|
| 28 | +Examples: |
| 29 | + better-tree . |
| 30 | + better-tree src --ignore node_modules dist |
| 31 | + better-tree --depth 2 --files-only |
| 32 | + better-tree --dirs-only |
| 33 | +`); |
| 34 | + process.exit(0); |
| 35 | +} |
| 36 | + |
| 37 | +// -------------------- |
| 38 | +// CLI ARGUMENTS PARSING |
| 39 | +// -------------------- |
| 40 | +const args = process.argv.slice(2); |
| 41 | +if (args.includes("--help")) showHelp(); |
| 42 | + |
| 43 | +function extractOption(optionName, defaultValue = null, valueIndexOffset = 1) { |
| 44 | + const index = args.indexOf(optionName); |
| 45 | + if (index !== -1) { |
| 46 | + const value = args[index + valueIndexOffset]; |
| 47 | + args.splice(index, valueIndexOffset + 1); |
| 48 | + return value ?? defaultValue; |
| 49 | + } |
| 50 | + return defaultValue; |
| 51 | +} |
| 52 | + |
| 53 | +function hasFlag(flag) { |
| 54 | + return args.includes(flag); |
| 55 | +} |
| 56 | + |
| 57 | +const ignoreIndex = args.indexOf("--ignore"); |
| 58 | +const ignorePatterns = ignoreIndex !== -1 ? args.slice(ignoreIndex + 1) : []; |
| 59 | +if (ignoreIndex !== -1) args.splice(ignoreIndex); |
| 60 | + |
| 61 | +const maxDepth = parseInt(extractOption("--depth", Infinity), 10) || Infinity; |
| 62 | +const filesOnly = hasFlag("--files-only"); |
| 63 | +const dirsOnly = hasFlag("--dirs-only"); |
| 64 | +const targetDir = path.resolve(args[0] || "."); |
| 65 | + |
| 66 | +// -------------------- |
| 67 | +// IGNORE PATTERNS |
| 68 | +// -------------------- |
| 69 | +const compiledPatterns = ignorePatterns.map((pattern) => ({ |
| 70 | + regex: new RegExp( |
| 71 | + "^" + |
| 72 | + pattern |
| 73 | + .replace(/\./g, "\\.") |
| 74 | + .replace(/\*\*/g, ".*") |
| 75 | + .replace(/\*/g, "[^/]*") + |
| 76 | + "$", |
| 77 | + ), |
| 78 | +})); |
| 79 | + |
| 80 | +function shouldIgnore(relativePath) { |
| 81 | + return compiledPatterns.some( |
| 82 | + ({ regex }) => |
| 83 | + regex.test(relativePath) || regex.test(path.basename(relativePath)), |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +// -------------------- |
| 88 | +// CONCURRENCY LIMITER |
| 89 | +// -------------------- |
| 90 | +let activeCount = 0; |
| 91 | +const queue = []; |
| 92 | +const maxConcurrency = 20; |
| 93 | + |
| 94 | +function limitConcurrency(task) { |
| 95 | + return new Promise((resolve, reject) => { |
| 96 | + const run = async () => { |
| 97 | + activeCount++; |
| 98 | + try { |
| 99 | + const result = await task(); |
| 100 | + resolve(result); |
| 101 | + } catch (err) { |
| 102 | + reject(err); |
| 103 | + } finally { |
| 104 | + activeCount--; |
| 105 | + if (queue.length) queue.shift()(); |
| 106 | + } |
| 107 | + }; |
| 108 | + if (activeCount < maxConcurrency) { |
| 109 | + run(); |
| 110 | + } else { |
| 111 | + queue.push(run); |
| 112 | + } |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +// -------------------- |
| 117 | +// TREE PRINTING |
| 118 | +// -------------------- |
| 119 | +async function printTree(dir, prefix = "", depth = 0, relativeBase = "") { |
| 120 | + if (depth > maxDepth) return; |
| 121 | + |
| 122 | + let entries; |
| 123 | + try { |
| 124 | + entries = await fs.readdir(dir, { withFileTypes: true }); |
| 125 | + } catch { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + const filteredEntries = entries |
| 130 | + .map((entry) => ({ |
| 131 | + entry, |
| 132 | + relativePath: path.join(relativeBase, entry.name).replace(/\\/g, "/"), |
| 133 | + })) |
| 134 | + .filter((item) => !shouldIgnore(item.relativePath)) |
| 135 | + .filter((item) => { |
| 136 | + if (filesOnly && item.entry.isDirectory()) return false; |
| 137 | + if (dirsOnly && !item.entry.isDirectory()) return false; |
| 138 | + return true; |
| 139 | + }) |
| 140 | + .sort((a, b) => a.entry.name.localeCompare(b.entry.name)); |
| 141 | + |
| 142 | + const lastIndex = filteredEntries.length - 1; |
| 143 | + |
| 144 | + for (let i = 0; i < filteredEntries.length; i++) { |
| 145 | + const { entry } = filteredEntries[i]; |
| 146 | + const connector = i === lastIndex ? "└── " : "├── "; |
| 147 | + |
| 148 | + let nameColored = entry.name; |
| 149 | + if (entry.isDirectory()) { |
| 150 | + nameColored = colors.blue + entry.name + colors.reset; |
| 151 | + } else if (entry.isSymbolicLink()) { |
| 152 | + nameColored = colors.cyan + entry.name + colors.reset; |
| 153 | + } else { |
| 154 | + nameColored = colors.white + entry.name + colors.reset; |
| 155 | + } |
| 156 | + |
| 157 | + console.log(prefix + connector + nameColored); |
| 158 | + |
| 159 | + if (entry.isDirectory() && !filesOnly) { |
| 160 | + const nextPrefix = prefix + (i === lastIndex ? " " : "│ "); |
| 161 | + await limitConcurrency(() => |
| 162 | + printTree( |
| 163 | + path.join(dir, entry.name), |
| 164 | + nextPrefix, |
| 165 | + depth + 1, |
| 166 | + filteredEntries[i].relativePath, |
| 167 | + ), |
| 168 | + ); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// -------------------- |
| 174 | +// START |
| 175 | +// -------------------- |
| 176 | +await printTree(targetDir); |
0 commit comments