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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 1x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 1x 1x 1x 13x 13x 13x 2x 2x 2x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 18x 18x 18x 18x 8x 8x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x 8x 8x 10x 10x 10x 10x 10x 10x 10x 18x 18x 10x 10x 10x 10x 10x 10x 18x 18x 3x 3x 3x 3x 3x 3x 3x 18x 18x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 13x 11x 11x 11x 7x 7x 11x 11x 11x 13x 13x 1x 4x 4x 4x 4x 4x 4x 4x 4x 6x 6x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 6x 6x 4x 4x 4x 4x 1x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 11x 11x 7x 7x 11x 3x 2x 3x 1x 1x 11x 4x 4x 11x 4x 4x 11x 5x 5x 5x 5x 5x 5x 5x 5x 5x | import fs from "fs/promises";
import { createReadStream, createWriteStream } from "fs";
import path from "path";
import { pipeline } from "stream/promises";
import pLimit from "p-limit";
import { shouldSkipForContent } from "./tree-generator.js";
import { Config } from "./config.js";
import { theme } from "./theme.js";
import { PERFORMANCE_CONSTANTS, MESSAGES } from "./constants.js";
// Format file header with metadata
function formatFileHeader(filePath, config = null) {
const cfg = config || new Config();
const relativePath = path.relative(process.cwd(), filePath);
const separator = cfg.createSeparator();
return `${separator}\nFile: ${relativePath}\n${separator}\n`;
}
// Handle binary or large file placeholders
function createFilePlaceholder(filePath, skipInfo, config = null) {
const cfg = config || new Config();
const relativePath = path.relative(process.cwd(), filePath);
const separator = cfg.createSeparator();
return `${separator}\nFile: ${relativePath}\n${separator}\n[${skipInfo.reason}]\n\n`;
}
// Truncate large text files
async function readFileWithTruncation(filePath, config = null) {
const cfg = config || new Config();
const maxSize = cfg.options.TRUNCATE_SIZE_BYTES;
try {
const handle = await fs.open(filePath, "r");
const buffer = Buffer.alloc(maxSize);
const { bytesRead } = await handle.read(buffer, 0, maxSize, 0);
await handle.close();
const content = buffer.subarray(0, bytesRead).toString("utf8");
// Check if file was truncated
const stats = await fs.stat(filePath);
if (stats.size > maxSize) {
const truncatedMessage = `\n\n[File truncated - showing first ${cfg.formatFileSize(maxSize)} of ${cfg.formatFileSize(stats.size)} total]`;
return content + truncatedMessage;
}
return content;
} catch (error) {
throw new Error(`Error reading file ${filePath}: ${error.message}`);
}
}
// Stream-based file content appending for memory efficiency with concurrency control
async function appendFileContentsToTree(
filePaths,
outputFilePath,
options = {}
) {
if (!Array.isArray(filePaths) || filePaths.length === 0) {
if (options.verbose) {
console.log(theme.warning("š No files to process"));
}
return;
}
const config = options.config || new Config();
// Handle legacy maxSize option for backward compatibility
if (options.maxSize !== undefined && !options.config) {
config.options.MAX_FILE_SIZE_MB = options.maxSize;
config.maxFileSizeBytes = config.options.MAX_FILE_SIZE_MB * 1024 * 1024;
}
const { progress } = options;
// Create concurrency limiter based on performance constants
const concurrencyLimit = Math.min(
PERFORMANCE_CONSTANTS.DEFAULT_CONCURRENCY_LIMIT,
Math.max(1, Math.floor(filePaths.length / 10)) // Dynamic scaling
);
const limit = pLimit(concurrencyLimit);
let processedCount = 0;
let skippedCount = 0;
let errorCount = 0;
try {
// Create write stream for efficient appending
const writeStream = createWriteStream(outputFilePath, {
flags: "a",
encoding: "utf8"
});
// Process files with controlled concurrency
const processFile = async (filePath) => {
try {
// Check if file should be skipped (binary, too large, etc.)
const skipInfo = shouldSkipForContent(filePath, { ...options, config });
if (skipInfo.skip) {
// Write placeholder for skipped files
const placeholder = createFilePlaceholder(filePath, skipInfo, config);
writeStream.write(placeholder);
skippedCount++;
if (progress) {
progress.reportFileProgress(filePath, 0, "skipped");
}
if (options.verbose) {
console.log(
theme.skipWithIcon(
`Skipped ${path.relative(process.cwd(), filePath)}: ${skipInfo.reason}`
)
);
}
return;
}
// Write file header
const header = formatFileHeader(filePath, config);
writeStream.write(header);
// Read and write file content
try {
let content;
const stats = await fs.stat(filePath);
if (stats.size > config.maxFileSizeBytes) {
// Use truncation for very large files
content = await readFileWithTruncation(filePath, config);
} else {
// Read normal files completely
content = await fs.readFile(filePath, "utf8");
}
writeStream.write(content);
writeStream.write("\n\n");
processedCount++;
if (progress) {
progress.reportFileProgress(filePath, stats.size, "processed");
}
if (options.verbose) {
const sizeFormatted = config.formatFileSize(stats.size);
console.log(
theme.fileProcessed(
`Processed ${path.relative(process.cwd(), filePath)} (${sizeFormatted})`
)
);
}
} catch (readError) {
// Handle file read errors gracefully
const errorMessage = `Error reading file: ${readError.message}`;
writeStream.write(`${errorMessage}\n\n`);
errorCount++;
if (progress) {
progress.reportFileProgress(filePath, 0, "error");
}
if (options.verbose) {
console.warn(
theme.fileError(
`Error reading ${path.relative(process.cwd(), filePath)}: ${readError.message}`
)
);
}
}
} catch (fileError) {
errorCount++;
if (progress) {
progress.reportFileProgress(filePath, 0, "error");
}
if (options.verbose) {
console.warn(
theme.fileError(
`Error processing ${path.relative(process.cwd(), filePath)}: ${fileError.message}`
)
);
}
}
};
// Execute file processing with concurrency control
await Promise.allSettled(
filePaths.map((filePath) => limit(() => processFile(filePath)))
);
// Close the write stream
await new Promise((resolve, reject) => {
writeStream.end((error) => {
if (error) reject(error);
else resolve();
});
});
// Log summary
if (!options.quiet) {
console.log(theme.success("\nš File processing summary:"));
console.log(theme.successWithIcon(`Processed: ${processedCount} files`));
if (skippedCount > 0) {
console.log(theme.skipWithIcon(`Skipped: ${skippedCount} files`));
}
if (errorCount > 0) {
console.log(theme.errorWithIcon(`Errors: ${errorCount} files`));
}
console.log(theme.info(`Concurrency limit: ${concurrencyLimit}`));
}
} catch (error) {
throw new Error(`${MESSAGES.PROCESSING_COMPLETE}: ${error.message}`);
}
}
// Alternative streaming implementation for very large projects
async function appendFileContentsStreaming(
filePaths,
outputFilePath,
options = {}
) {
const writeStream = createWriteStream(outputFilePath, { flags: "a" });
try {
for (const filePath of filePaths) {
const skipInfo = shouldSkipForContent(filePath, options);
if (skipInfo.skip) {
const placeholder = createFilePlaceholder(filePath, skipInfo);
writeStream.write(placeholder);
continue;
}
// Write header
const header = formatFileHeader(filePath);
writeStream.write(header);
try {
// Stream file content directly
const readStream = createReadStream(filePath, { encoding: "utf8" });
await pipeline(readStream, writeStream, { end: false });
writeStream.write("\n\n");
} catch (streamError) {
// If the error is due to file access, write the expected message
writeStream.write("Cannot access file\n\n");
}
}
} finally {
writeStream.end();
}
}
// Utility function to get file processing statistics
async function getFileStats(filePaths, options = {}) {
// 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;
// eslint-disable-next-line no-param-reassign
options = { ...options, config };
}
let totalSize = 0;
let textFiles = 0;
let binaryFiles = 0;
let largeFiles = 0;
for (const filePath of filePaths) {
try {
const stats = await fs.stat(filePath);
totalSize += stats.size;
const skipInfo = shouldSkipForContent(filePath, options);
if (skipInfo.skip) {
if (skipInfo.reason.includes("Binary")) {
binaryFiles++;
} else if (skipInfo.reason.includes("too large")) {
largeFiles++;
}
} else {
textFiles++;
}
} catch {
// Skip files that can't be accessed
}
}
return {
totalFiles: filePaths.length,
textFiles,
binaryFiles,
largeFiles,
totalSizeBytes: totalSize,
totalSizeMB: totalSize / (1024 * 1024)
};
}
export {
appendFileContentsToTree,
appendFileContentsStreaming,
getFileStats,
readFileWithTruncation
};
|