dump-directory-content.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // dump-directory-content.js
  2. // Accepts one argument (a directory path).
  3. // Recursively read the content of the given directory and then:
  4. // 1. print a tree-like structure of directories and files
  5. // 2. in a Markdown style, print each file (full path relative to arg) in bold and its content in a fenced code block (with the language if the extension is known)
  6. const fs = require('fs');
  7. const path = require('path');
  8. function printDirectoryContent(directoryPath, indent = '') {
  9. const files = fs.readdirSync(directoryPath);
  10. for (const file of files) {
  11. // Should exclude markdown files
  12. if (file.endsWith('.md')) {
  13. continue;
  14. }
  15. const filePath = path.join(directoryPath, file);
  16. const stats = fs.statSync(filePath);
  17. if (stats.isDirectory()) {
  18. console.log(indent + file + '/');
  19. printDirectoryContent(filePath, indent + ' ');
  20. } else {
  21. const fileContent = fs.readFileSync(filePath, 'utf8');
  22. const fileExtension = path.extname(filePath).toLowerCase();
  23. const language = getLanguageFromExtension(fileExtension);
  24. console.log(indent + '**' + file + '**\n');
  25. console.log('```' + language);
  26. console.log(fileContent);
  27. console.log('```');
  28. }
  29. }
  30. }
  31. function getLanguageFromExtension(extension) {
  32. // Add more extensions and their corresponding languages as needed
  33. switch (extension) {
  34. case '.yml':
  35. return 'javascript';
  36. case '.js':
  37. return 'javascript';
  38. case '.py':
  39. return 'python';
  40. case '.java':
  41. return 'java';
  42. default:
  43. return '';
  44. }
  45. }
  46. const directoryPath = process.argv[2];
  47. printDirectoryContent(directoryPath);