Skip to content

Instantly share code, notes, and snippets.

@saadh393
Created July 13, 2024 14:30
Show Gist options
  • Save saadh393/eca3e8fb0814cd3404d1cce9b4b8ed2b to your computer and use it in GitHub Desktop.
Save saadh393/eca3e8fb0814cd3404d1cce9b4b8ed2b to your computer and use it in GitHub Desktop.
Scan Project Directory for Codes, to Give it as a context to ChatGPT or Other AI Tools
const fs = require('fs').promises;
const path = require('path');
async function scanDirectory(dir) {
let results = '';
async function scan(currentPath) {
const files = await fs.readdir(currentPath);
for (const file of files) {
const filePath = path.join(currentPath, file);
const stats = await fs.stat(filePath);
console.log(file)
const avoidFileAndFolders = [".next", "node_modules", ".git", "public", "styles", ".eslintrc.json", "package.json", "package-lock.json", "README.md", ".gitignore", "components.json", "jsconfig.json", "next.config", "structure", "tailwind.config.js"];
if(avoidFileAndFolders.includes(file)) {
continue;
}
if (stats.isDirectory()) {
await scan(filePath);
} else {
const relativePath = path.relative('src', filePath);
const content = await fs.readFile(filePath, 'utf8');
results += `\npath - ${relativePath}\ncode - \n\`\`\`\n${content}\n\`\`\`\n`;
}
}
}
await scan(dir);
return results;
}
async function main() {
try {
const content = await scanDirectory('.');
await fs.writeFile('src_contents.md', content);
console.log('Markdown file generated successfully: src_contents.md');
} catch (error) {
console.error('An error occurred:', error);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment