| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- // 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) {
- 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 + '**');
- 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);
|