* Walk a directory and return scored file entries. * * @param {string} rootDir - Directory to walk * @param {string} taskHint - Optional keywords from user task for bonus scoring * @param {object} opts * @param {number} opts.maxDepth - Max directory depth (default 6) * @param {number}
(rootDir, taskHint, opts = {})
| 81 | * @param {number} opts.maxFiles - Max files to collect before scoring (default 2000) |
| 82 | */ |
| 83 | function scoredFileListing(rootDir, taskHint, opts = {}) { |
| 84 | const maxDepth = opts.maxDepth || 6; |
| 85 | const maxCollect = opts.maxFiles || 2000; |
| 86 | const now = Date.now(); |
| 87 | const taskTokens = taskHint |
| 88 | ? taskHint.toLowerCase().split(/\W+/).filter(t => t.length > 2) |
| 89 | : []; |
| 90 | |
| 91 | const entries = []; |
| 92 | |
| 93 | function walk(dir, depth) { |
| 94 | if (depth > maxDepth || entries.length >= maxCollect) return; |
| 95 | let listing; |
| 96 | try { listing = fs.readdirSync(dir, { withFileTypes: true }); } |
| 97 | catch { return; } |
| 98 | |
| 99 | for (const ent of listing) { |
| 100 | if (entries.length >= maxCollect) break; |
| 101 | const name = ent.name; |
| 102 | |
| 103 | if (ent.isDirectory()) { |
| 104 | if (SKIP_DIRS.has(name)) continue; |
| 105 | // Skip most dot-dirs but allow .marrow (MarrowScript source) |
| 106 | if (name.startsWith('.') && name !== '.marrow') continue; |
| 107 | walk(path.join(dir, name), depth + 1); |
| 108 | } else if (ent.isFile()) { |
| 109 | const full = path.join(dir, name); |
| 110 | const rel = path.relative(rootDir, full); |
| 111 | const ext = path.extname(name).toLowerCase(); |
| 112 | const base = name.toLowerCase(); |
| 113 | |
| 114 | let score = 0; |
| 115 | |
| 116 | // Extension scoring |
| 117 | if (SOURCE_EXTS.has(ext)) score += 2; |
| 118 | |
| 119 | // Config files |
| 120 | if (CONFIG_FILES.has(name)) score += 1; |
| 121 | |
| 122 | // Test files |
| 123 | if (/^test_|\.test\.|\.spec\.|_test\./i.test(name)) score += 1; |
| 124 | |
| 125 | // Generated output penalty |
| 126 | if (GENERATED_PATTERNS.some(p => p.test(name))) score -= 2; |
| 127 | |
| 128 | // Recent modification bonus |
| 129 | let mtime = 0; |
| 130 | try { |
| 131 | const stat = fs.statSync(full); |
| 132 | mtime = stat.mtimeMs; |
| 133 | const ageMs = now - mtime; |
| 134 | if (ageMs < 86400000) score += 3; // < 24h |
| 135 | else if (ageMs < 604800000) score += 2; // < 7d |
| 136 | else if (ageMs < 2592000000) score += 1; // < 30d |
| 137 | } catch {} |
| 138 | |
| 139 | // Task keyword bonus |
| 140 | if (taskTokens.length > 0) { |
no test coverage detected