1
0

dump-directory-content.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. const filePath = path.join(directoryPath, file);
  12. const stats = fs.statSync(filePath);
  13. if (stats.isDirectory()) {
  14. console.log(indent + file + '/');
  15. printDirectoryContent(filePath, indent + ' ');
  16. } else {
  17. const fileContent = fs.readFileSync(filePath, 'utf8');
  18. const fileExtension = path.extname(filePath).toLowerCase();
  19. const language = getLanguageFromExtension(fileExtension);
  20. console.log(indent + '**' + file + '**');
  21. console.log('```' + language);
  22. console.log(fileContent);
  23. console.log('```');
  24. }
  25. }
  26. }
  27. function getLanguageFromExtension(extension) {
  28. // Add more extensions and their corresponding languages as needed
  29. switch (extension) {
  30. case '.yml':
  31. return 'javascript';
  32. case '.js':
  33. return 'javascript';
  34. case '.py':
  35. return 'python';
  36. case '.java':
  37. return 'java';
  38. default:
  39. return '';
  40. }
  41. }
  42. const directoryPath = process.argv[2];
  43. printDirectoryContent(directoryPath);