Skip to content

Instantly share code, notes, and snippets.

@wgminer
Created February 4, 2025 15:38
Show Gist options
  • Save wgminer/7dd4127342453414838dd924bf500707 to your computer and use it in GitHub Desktop.
Save wgminer/7dd4127342453414838dd924bf500707 to your computer and use it in GitHub Desktop.
A simple directory concatenator for easy copy and paste into a LLM
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import clipboardy from "clipboardy";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const INPUT_DIR =
process.argv[2] || "/Users/jsmith/MyExampleProject";
const OUTPUT_FILE = path.join(__dirname, "OUTPUT.txt");
const allowedExtensions = [".swift", ".plist"];
function isAllowedFile(filePath) {
return allowedExtensions.includes(path.extname(filePath).toLowerCase());
}
function mergeFiles() {
let mergedContent = "";
function traverseDir(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
traverseDir(filePath);
} else if (stat.isFile() && isAllowedFile(filePath)) {
try {
const content = fs.readFileSync(filePath, "utf-8");
mergedContent += `\n*********************************************\n`;
mergedContent += `FILE - ${path.basename(filePath)}\n`;
mergedContent += `*********************************************\n\n`;
mergedContent += content + "\n\n";
} catch (error) {
console.error(`Error reading file ${filePath}:`, error.message);
}
}
}
}
try {
traverseDir(INPUT_DIR);
if (mergedContent.trim() === "") {
console.log("No matching files found. OUTPUT.txt will not be created.");
} else {
fs.writeFileSync(OUTPUT_FILE, mergedContent);
console.log(`Files merged successfully! Output: ${OUTPUT_FILE}`);
// Copy to clipboard
clipboardy.writeSync(mergedContent);
console.log("Content copied to clipboard!");
}
} catch (error) {
console.error("Error merging files:", error.message);
}
}
mergeFiles();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment