Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 15x 15x 14x 20x 3x 3x 3x 3x 29x 1x 1x 1x 1x 1x 1x 29x 14x 14x 29x 29x 29x 1x 1x 29x 29x 29x 1x 324x 324x 324x 9x 9x 315x 315x 315x 1x 58x 58x 58x 58x 58x 2x 2x 2x 2x 58x 58x 58x 58x 58x 58x 6x 6x 6x 6x 6x 58x 5x 5x 5x 5x 5x 47x 58x 8x 8x 39x 58x 58x 1x 10x 10x 10x 10x 10x 10x 10x 37x 37x 36x 36x 37x 142x 142x 142x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 142x 36x 36x 82x 82x 43x 36x 36x 37x 86x 86x 86x 86x 86x 86x 86x 86x 8x 8x 8x 8x 86x 86x 86x 27x 27x 86x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 10x 10x 10x 1x 19x 19x 19x 19x 19x 19x 19x 60x 60x 60x 182x 182x 182x 135x 135x 135x 41x 135x 93x 93x 135x 1x 1x 1x 1x 1x 1x 1x 1x 135x 182x 60x 1x 1x 1x 1x 1x 1x 1x 1x 60x 19x 19x 19x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 1x 1x 4x | import fs from "fs/promises";
import { statSync } from "fs";
import path from "path";
import ignore from "ignore";
import { isBinaryFileSync } from "isbinaryfile";
import { Config } from "./config.js";
import { theme } from "./theme.js";
// Parse gitignore files and create ignore instance
async function createIgnoreFilter(baseDir, options = {}) {
const config = options.config || new Config();
const ig = ignore();
try {
// Add default patterns from config (now async)
const ignorePatterns = await config.getIgnorePatterns(options.exclude);
ig.add(ignorePatterns);
// Read .gitignore file if it exists
const gitignorePath = path.join(baseDir, ".gitignore");
try {
// Use async fs.access instead of existsSync
await fs.access(gitignorePath);
try {
const gitignoreContent = await fs.readFile(gitignorePath, "utf8");
ig.add(gitignoreContent);
if (options.verbose) {
console.log(
theme.muted(`📋 Loaded .gitignore from ${gitignorePath}`)
);
}
} catch (error) {
if (options.verbose) {
console.warn(
theme.warning(`⚠️ Could not read .gitignore: ${error.message}`)
);
}
}
} catch {
// .gitignore doesn't exist, continue silently
}
// Handle include patterns (if specified, only include matching files)
let includeFilter = null;
if (options.include) {
includeFilter = ignore().add(options.include);
}
return { ignore: ig, includeFilter };
} catch (error) {
throw new Error(`Failed to create ignore filter: ${error.message}`);
}
}
// Check if a file should be ignored
function shouldIgnore(relativePath, ignoreFilter, includeFilter) {
// Check include filter first (if specified)
if (includeFilter && !includeFilter.ignores(relativePath)) {
return true; // Not in include list, so ignore it
}
// Check ignore patterns
return ignoreFilter.ignores(relativePath);
}
// Check if file is binary and should be skipped for content
function shouldSkipForContent(filePath, options = {}) {
try {
// Handle legacy maxSize option for backward compatibility
let config = options.config || new Config();
if (options.maxSize !== undefined && !options.config) {
config = new Config();
config.options.MAX_FILE_SIZE_MB = options.maxSize;
config.maxFileSizeBytes = config.options.MAX_FILE_SIZE_MB * 1024 * 1024;
}
// Use sync stat since isBinaryFileSync also uses sync operations
// This is a limitation of the binary detection library
let stats;
try {
stats = statSync(filePath);
} catch (error) {
return {
skip: true,
reason: `Cannot access file: ${error.message}`
};
}
if (stats.size > config.maxFileSizeBytes) {
return {
skip: true,
reason: `File too large (${config.formatFileSize(stats.size)})`
};
}
// Check if binary
if (isBinaryFileSync(filePath)) {
return { skip: true, reason: "Binary file" };
}
return { skip: false };
} catch (error) {
return { skip: true, reason: `Error checking file: ${error.message}` };
}
}
// Async directory tree generation with gitignore support
async function displayTreeWithGitignore(dirPath, options = {}) {
const output = [];
const { ignore: ignoreFilter, includeFilter } = await createIgnoreFilter(
dirPath,
options
);
async function traverse(currentPath, depth = 0, prefix = "") {
try {
const items = await fs.readdir(currentPath);
// Filter and sort items
const filteredItems = [];
for (const item of items) {
const fullPath = path.join(currentPath, item);
const relativePath = path.relative(dirPath, fullPath);
if (!shouldIgnore(relativePath, ignoreFilter, includeFilter)) {
try {
const stats = await fs.stat(fullPath);
filteredItems.push({
name: item,
fullPath,
isDirectory: stats.isDirectory(),
size: stats.size
});
} catch (statError) {
if (options.verbose) {
console.warn(
theme.warning(
`⚠️ Could not stat ${fullPath}: ${statError.message}`
)
);
}
}
}
}
// Sort: directories first, then files, alphabetically
filteredItems.sort((a, b) => {
if (a.isDirectory && !b.isDirectory) return -1;
if (!a.isDirectory && b.isDirectory) return 1;
return a.name.localeCompare(b.name);
});
// Process each item
for (let i = 0; i < filteredItems.length; i++) {
const item = filteredItems[i];
const isLast = i === filteredItems.length - 1;
// Tree symbols
const branch = isLast ? "└── " : "├── ";
const newPrefix = prefix + (isLast ? " " : "│ ");
// Add size info for large files
let displayName = item.name;
if (!item.isDirectory && item.size > 1024 * 1024) {
displayName += theme.muted(
` (${(item.size / 1024 / 1024).toFixed(1)}MB)`
);
}
output.push(`${prefix}${branch}${displayName}`);
// Recursively process directories
if (item.isDirectory) {
await traverse(item.fullPath, depth + 1, newPrefix);
}
}
} catch (error) {
if (options.verbose) {
console.warn(
theme.warning(
`⚠️ Could not read directory ${currentPath}: ${error.message}`
)
);
}
output.push(`${prefix}[Error reading directory: ${error.message}]`);
}
}
await traverse(dirPath);
return output;
}
// Async file path collection
async function getAllFilePaths(dirPath, options = {}) {
const filePaths = [];
const { ignore: ignoreFilter, includeFilter } = await createIgnoreFilter(
dirPath,
options
);
async function traverse(currentPath) {
try {
const items = await fs.readdir(currentPath);
for (const item of items) {
const fullPath = path.join(currentPath, item);
const relativePath = path.relative(dirPath, fullPath);
if (!shouldIgnore(relativePath, ignoreFilter, includeFilter)) {
try {
const stats = await fs.stat(fullPath);
if (stats.isDirectory()) {
await traverse(fullPath);
} else {
filePaths.push(fullPath);
}
} catch (statError) {
if (options.verbose) {
console.warn(
theme.warning(
`⚠️ Could not stat ${fullPath}: ${statError.message}`
)
);
}
}
}
}
} catch (error) {
if (options.verbose) {
console.warn(
theme.warning(
`⚠️ Could not read directory ${currentPath}: ${error.message}`
)
);
}
}
}
await traverse(dirPath);
return filePaths;
}
// Save tree to file with enhanced formatting
async function saveTreeToFile(dirPath, fileName, options = {}) {
try {
const output = await displayTreeWithGitignore(dirPath, options);
// Create header with metadata
const header = [
`Directory structure for: ${path.resolve(dirPath)}`,
`Generated on: ${new Date().toISOString()}`,
`Total items: ${output.length}`,
""
];
const content = [...header, ...output, "", ""].join("\n");
await fs.writeFile(fileName, content, "utf8");
if (options.verbose) {
console.log(theme.muted(`📁 Directory tree saved to ${fileName}`));
}
} catch (error) {
throw new Error(`Failed to save tree to file: ${error.message}`);
}
}
export {
displayTreeWithGitignore,
saveTreeToFile,
getAllFilePaths,
shouldSkipForContent
};
|