| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- // dump-directory-content.js
- // Accepts one argument (a directory path).
- // Recursively read the content of the given directory and then:
- // 1. print a tree-like structure of directories and files
- // 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)
- const fs = require('fs');
- const path = require('path');
- function printDirectoryContent(directoryPath, indent = '') {
- const files = fs.readdirSync(directoryPath);
- for (const file of files) {
- // Should exclude markdown files
- if (file.endsWith('.md')) {
- continue;
- }
- const filePath = path.join(directoryPath, file);
- const stats = fs.statSync(filePath);
- if (stats.isDirectory()) {
- console.log(indent + file + '/');
- printDirectoryContent(filePath, indent + ' ');
- } else {
- const fileContent = fs.readFileSync(filePath, 'utf8');
- const fileExtension = path.extname(filePath).toLowerCase();
- const language = getLanguageFromExtension(fileExtension);
- console.log(indent + '**' + file + '**\n');
- console.log('```' + language);
- console.log(fileContent);
- console.log('```');
- }
- }
- }
- function getLanguageFromExtension(extension) {
- // Add more extensions and their corresponding languages as needed
- switch (extension) {
- case '.yml':
- return 'javascript';
- case '.js':
- return 'javascript';
- case '.py':
- return 'python';
- case '.java':
- return 'java';
- default:
- return '';
- }
- }
- const directoryPath = process.argv[2];
- printDirectoryContent(directoryPath);
|