|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +function processFile(filePath) { |
| 5 | + try { |
| 6 | + const content = fs.readFileSync(filePath, "utf8"); |
| 7 | + // Preserve brackets inside code fences (```...```) |
| 8 | + const modifiedContent = content.replace(/```[\s\S]*?```|[{}]/g, (match) => { |
| 9 | + // If match contains newlines or starts with ```, it's a code block - preserve it |
| 10 | + return match.includes("\n") || match.startsWith("```") ? match : ""; |
| 11 | + }); |
| 12 | + fs.writeFileSync(filePath, modifiedContent, "utf8"); |
| 13 | + console.log(`Processed: ${filePath}`); |
| 14 | + } catch (error) { |
| 15 | + console.error(`Error processing ${filePath}: ${error.message}`); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +function crawlDirectory(dirPath) { |
| 20 | + try { |
| 21 | + const items = fs.readdirSync(dirPath); |
| 22 | + |
| 23 | + for (const item of items) { |
| 24 | + const itemPath = path.join(dirPath, item); |
| 25 | + const stats = fs.statSync(itemPath); |
| 26 | + |
| 27 | + if (stats.isDirectory()) { |
| 28 | + crawlDirectory(itemPath); |
| 29 | + } else if (stats.isFile()) { |
| 30 | + processFile(itemPath); |
| 31 | + } |
| 32 | + } |
| 33 | + } catch (error) { |
| 34 | + console.error(`Error crawling directory ${dirPath}: ${error.message}`); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Start crawling from current directory or specify a path |
| 39 | +const targetPath = process.argv[2] || "."; |
| 40 | +crawlDirectory(targetPath); |
0 commit comments