(options: CollectCodeOptions)
| 48 | } |
| 49 | |
| 50 | export async function collectCode(options: CollectCodeOptions): Promise<{ manifest: CodeCollectionManifest; files: CodeCollectedFile[] }> { |
| 51 | const root = path.resolve(options.root); |
| 52 | const filePaths: string[] = []; |
| 53 | await walk(root, filePaths, options.includeTests ?? false); |
| 54 | |
| 55 | // Sort: key files first, then by directory depth (shallow first) |
| 56 | let filtered = filePaths.sort((a, b) => { |
| 57 | const relA = toPosix(path.relative(root, a)); |
| 58 | const relB = toPosix(path.relative(root, b)); |
| 59 | const langA = languageFor(a); |
| 60 | const langB = languageFor(b); |
| 61 | const keyA = isKeyFile(relA, langA) ? 0 : 1; |
| 62 | const keyB = isKeyFile(relB, langB) ? 0 : 1; |
| 63 | if (keyA !== keyB) return keyA - keyB; |
| 64 | const depthA = relA.split('/').length; |
| 65 | const depthB = relB.split('/').length; |
| 66 | if (depthA !== depthB) return depthA - depthB; |
| 67 | return relA.localeCompare(relB); |
| 68 | }); |
| 69 | |
| 70 | // Filter to only changed files if specified |
| 71 | if (options.changedFiles && options.changedFiles.length > 0) { |
| 72 | const changedSet = new Set(options.changedFiles.map((f) => toPosix(f))); |
| 73 | filtered = filtered.filter((fp) => { |
| 74 | const relativePath = toPosix(path.relative(root, fp)); |
| 75 | return changedSet.has(relativePath); |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | const limited = filtered.slice(0, options.maxFiles ?? 200); |
| 80 | const files: CodeCollectedFile[] = []; |
| 81 | |
| 82 | for (const filePath of limited) { |
| 83 | const content = await readFile(filePath, "utf8"); |
| 84 | const relativePath = toPosix(path.relative(root, filePath)); |
| 85 | const language = languageFor(filePath); |
| 86 | files.push({ |
| 87 | path: filePath, |
| 88 | relativePath, |
| 89 | language, |
| 90 | sha256: createHash("sha256").update(content).digest("hex"), |
| 91 | content, |
| 92 | isKeyFile: isKeyFile(relativePath, language) |
| 93 | }); |
| 94 | } |
| 95 | |
| 96 | return { |
| 97 | manifest: { |
| 98 | schemaVersion: "team-wiki.code-collection.v1", |
| 99 | root, |
| 100 | commit: await gitCommit(root), |
| 101 | collectedAt: new Date().toISOString(), |
| 102 | files: files.map(({ content: _content, ...file }) => file) |
| 103 | }, |
| 104 | files |
| 105 | }; |
| 106 | } |
| 107 |
no test coverage detected