Forgot I can just paste it here.
It's around 150 lines, it SHOULD work with any project with node. It'll recursively add files that belong to any folder you put in the keep list and it will ignore any file extensions in the condition.
const fs = require('fs');
const path = require('path');
async function selectFiles(currentDir, keepList, isWithinKeptFolder = false) {
const selectedFiles = [];
const items = await fs.promises.readdir(currentDir);
for (const item of items) {
const itemPath = path.join(currentDir, item);
let stats;
try {
stats = await fs.promises.stat(itemPath);
} catch (error) {
console.warn(`Warning: Could not access ${itemPath}. Skipping.`);
continue;
}
if (stats.isDirectory()) {
if (isWithinKeptFolder || keepList.folders.includes(item)) {
// If already within a kept folder or the folder is in the keep list
selectedFiles.push(...await selectFiles(itemPath, keepList, true));
}
} else {
// Ignore files with .txt extension
if (path.extname(item) === '.txt' || itemPath.includes('index.json') || itemPath.includes('courses.json')) {
console.log(`Ignoring .txt file: ${itemPath}`);
continue;
}
if (isWithinKeptFolder || keepList.files.includes(item)) {
// If within a kept folder or the file is in the keep list
selectedFiles.push(itemPath);
}
}
}
return selectedFiles;
}
async function mergeFiles(selectedFiles, outputDirPath) {
let mergedContent = '';
let lineCount = 0;
let fileIndex = 1;
for (const filePath of selectedFiles) {
try {
let fileContent = await fs.promises.readFile(filePath, 'utf-8');
const relativePath = path.relative(process.cwd(), filePath);
// Remove leading/trailing whitespace and reduce multiple newlines to a single newline
fileContent = fileContent
.replace(/\s $/, '') // Remove trailing whitespace
.replace(/^\s /, '') // Remove leading whitespace
.replace(/\n{2,}/g, '\n'); // Replace multiple newlines with a single newline
const sectionHeader = `\n\n===== ${relativePath.toUpperCase()} CODE BELOW =====\n`;
const fullContent = sectionHeader fileContent '\n';
const contentLines = fullContent.split('\n').length;
// If adding this content will exceed 1000 lines, write to a new file
if (lineCount contentLines > 1000) {
await writeFile(outputDirPath, fileIndex, mergedContent);
fileIndex ;
mergedContent = ''; // Reset the content
lineCount = 0; // Reset the line count
}
mergedContent = fullContent;
lineCount = contentLines;
} catch (error) {
console.warn(`Warning: Could not read file ${filePath}. Skipping.`);
}
}
// Write the remaining content to a new file
if (mergedContent) {
await writeFile(outputDirPath, fileIndex, mergedContent);
}
}
async function writeFile(outputDirPath, fileIndex, content) {
const outputFilePath = path.join(outputDirPath, `repo-transcript-${fileIndex}.txt`);
try {
await fs.promises.writeFile(outputFilePath, content, 'utf-8');
console.log(`Saved part ${fileIndex} to: ${outputFilePath}`);
} catch (error) {
console.error(`Error writing to file ${outputFilePath}:`, error);
}
}
async function createOutputDirectory(outputDirPath) {
try {
await fs.promises.access(outputDirPath);
} catch (error) {
try {
await fs.promises.mkdir(outputDirPath, { recursive: true });
console.log(`Created output directory: ${outputDirPath}`);
} catch (mkdirError) {
console.error(`Error creating directory ${outputDirPath}:`, mkdirError);
process.exit(1);
}
}
}
async function main() {
const currentDir = process.cwd();
console.log('Generating merged transcript of the project...');
// Define folders and files to keep here
const keepList = {
folders: [
'src',
'environments'
// Add more folder names to keep as needed
],
files: [
'package.json',
'angular.json',
'ngsw-config.json',
'tailwind.config.json',
'
tsconfig.app.json',
'tsconfig.json',
'tsconfig.spec.json',
'index.html',
'main.ts'
// Add more file names to keep as needed
],
};
let selectedFiles;
try {
selectedFiles = await selectFiles(currentDir, keepList);
} catch (error) {
console.error('Error selecting files:', error);
process.exit(1);
}
const outputDirName = 'repo_transcripts';
const outputDirPath = path.join(currentDir, outputDirName);
await createOutputDirectory(outputDirPath);
await mergeFiles(selectedFiles, outputDirPath);
}
main().catch((error) => {
console.error('An unexpected error occurred:', error);
});