Benoît Hubert 1 éve
szülő
commit
286ac416d0
1 módosított fájl, 50 hozzáadás és 0 törlés
  1. 50 0
      dump-directory-content.js

+ 50 - 0
dump-directory-content.js

@@ -0,0 +1,50 @@
+// 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);