generator-begcode
Version:
Spring Boot + Angular/React/Vue in one handy generator
52 lines (51 loc) • 2.39 kB
JavaScript
export class DirectoryChunker {
config;
constructor(config) {
this.config = config;
}
chunk(input) {
return this.chunkDirectory(input.workspace, input.directory || '');
}
async chunkDirectory(workspace, directory) {
const entries = await workspace.readdir(directory);
const chunks = [];
let currentChunk = [];
let currentSize = 0;
for (const entry of entries) {
const fullPath = `${directory}/${entry.name}`;
if (entry.type === 'file') {
const fileContents = (await workspace.readFile(fullPath)).split('\n');
let startLine = 0;
while (startLine < fileContents.length) {
const chunkHeader = `@@ { file: "${fullPath}", start: ${startLine + 1}, end: null } @@\n`;
let spaceForContent = this.config.maxChunkSize - (currentSize + chunkHeader.length);
const linesInThisChunk = [];
while (startLine < fileContents.length && fileContents[startLine].length + 1 <= spaceForContent) {
linesInThisChunk.push(fileContents[startLine]);
spaceForContent -= fileContents[startLine].length + 1;
startLine++;
}
const contentString = linesInThisChunk.join('\n');
const chunkWithPlaceholder = chunkHeader + contentString;
const finalChunk = chunkWithPlaceholder.replace('end: null', `end: ${startLine}`);
currentSize += finalChunk.length;
currentChunk.push(finalChunk);
if (currentSize >= this.config.maxChunkSize ||
(startLine < fileContents.length && currentSize + chunkHeader.length > this.config.maxChunkSize)) {
chunks.push(currentChunk.join(''));
currentChunk = [];
currentSize = 0;
}
}
}
else if (entry.type === 'directory') {
const subchunks = await this.chunkDirectory(workspace, fullPath);
chunks.push(...subchunks);
}
}
if (currentChunk.length > 0) {
chunks.push(currentChunk.join(''));
}
return chunks;
}
}