Created
April 13, 2025 04:37
-
-
Save LiamPerson/e84b761ccba31a48f54783d3e12aa6f0 to your computer and use it in GitHub Desktop.
Put all code in current directory into one file with markdown formatting with NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const path = require("path"); | |
// Directory to search for code files | |
const directoryPath = "./"; // Change this to your target directory if needed | |
// Files to exclude from processing | |
const excludedFiles = ["collect-files-for-ai.js"]; | |
// Create a new file called code-summary.md | |
const outputFilePath = "code-summary.md"; | |
fs.writeFileSync(outputFilePath, "", { flag: "w" }); | |
// Function to check if a file should be excluded | |
function shouldExcludeFile(fileName) { | |
return excludedFiles.includes(path.basename(fileName)); | |
} | |
// Function to read all files in the top-level directory | |
function getAllFiles(dir) { | |
let results = []; | |
const list = fs.readdirSync(dir); | |
list.forEach((file) => { | |
file = path.resolve(dir, file); | |
const stat = fs.statSync(file); | |
if (stat && stat.isDirectory()) { | |
// Skip directories to avoid recursion | |
return; | |
} else if ( | |
/\.js$/.test(file) || | |
/\.ts$/.test(file) || | |
/\.py$/.test(file) || | |
/\.java$/.test(file) || | |
/\.cpp$/.test(file) || | |
/\.c$/.test(file) || | |
/\.cs$/.test(file) || | |
/\.rb$/.test(file) || | |
/\.go$/.test(file) || | |
/\.html$/.test(file) || | |
/\.css$/.test(file) | |
) { | |
// Add the file to the results array if it's a code file | |
const baseName = path.basename(file); | |
if (!shouldExcludeFile(baseName)) { | |
results.push(file); | |
} | |
} | |
}); | |
return results; | |
} | |
// Get all code files in the specified directory | |
const codeFiles = getAllFiles(directoryPath); | |
// Read each file and append its contents to code-summary.md | |
codeFiles.forEach((file) => { | |
const data = fs.readFileSync(file, "utf8"); | |
const fileName = path.basename(file); | |
const extension = path.extname(file).slice(1); // Get the file extension without the dot | |
let languageIdentifier = ""; // Initialize language identifier | |
switch (extension) { | |
case "js": | |
languageIdentifier = "js"; | |
break; | |
case "ts": | |
languageIdentifier = "typescript"; | |
break; | |
case "py": | |
languageIdentifier = "python"; | |
break; | |
case "java": | |
languageIdentifier = "java"; | |
break; | |
case "cpp": | |
languageIdentifier = "cpp"; | |
break; | |
case "c": | |
languageIdentifier = "c"; | |
break; | |
case "cs": | |
languageIdentifier = "csharp"; | |
break; | |
case "rb": | |
languageIdentifier = "ruby"; | |
break; | |
case "go": | |
languageIdentifier = "go"; | |
break; | |
case "html": | |
languageIdentifier = "html"; | |
break; | |
case "css": | |
languageIdentifier = "css"; | |
break; | |
} | |
const contentBlock = `### ${fileName}\n\`\`\`${languageIdentifier}\n${data}\n\`\`\``; | |
// Append the contents to code-summary.md | |
fs.appendFileSync(outputFilePath, `${contentBlock}\n\n`); | |
}); | |
console.log("Code summary generated successfully!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output:
myFile.js
// javascript code here
index.html
custom.css