Skip to content

Instantly share code, notes, and snippets.

@kumarasinghe
Last active October 21, 2024 10:48
Show Gist options
  • Select an option

  • Save kumarasinghe/b1d78ccaa3d5faeebf68d9924801fcc4 to your computer and use it in GitHub Desktop.

Select an option

Save kumarasinghe/b1d78ccaa3d5faeebf68d9924801fcc4 to your computer and use it in GitHub Desktop.
Puts your repo's source codes into a single file for AI's to process
const fs = require("fs");
const path = require("path");
// Get the command-line arguments
const directory = process.argv[2];
const extArg = process.argv.find((arg) => arg.startsWith("--ext="));
// Validate inputs
if (!directory || !extArg) {
console.log("Usage: codesoup <directory> --ext=<extensions>");
console.log("\nExample: node codesoup.js src --ext=ts,tsx");
process.exit(1);
}
// Extract extensions and convert to an array
const extensions = extArg
.split("=")[1]
.split(",")
.map((ext) => ext.trim().toLowerCase());
// File to output content
const outputFile = "out.txt";
// Helper function to check if a file has the correct extension
function hasValidExtension(file) {
const ext = path.extname(file).toLowerCase().slice(1); // Get extension without dot
return extensions.includes(ext);
}
// Function to recursively iterate through the directory
function processDirectory(dir) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const fullPath = path.join(dir, file);
const relativePath = path.relative(directory, fullPath);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
// If it's a directory, recursively process it
processDirectory(fullPath);
} else if (stat.isFile() && hasValidExtension(file)) {
// If it's a file with a valid extension, process it
const content = fs.readFileSync(fullPath, "utf-8");
// Write to codebase.txt in the specified format
fs.appendFileSync(
outputFile,
`/********** file: ${relativePath} **********/\n\n`
);
fs.appendFileSync(outputFile, `${content}\n\n`);
}
});
}
// Clear the output file before writing
fs.writeFileSync(outputFile, "");
// Start processing the given directory
processDirectory(directory);
console.log(`Files have been processed and written to ${outputFile}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment