(project: ProjectState)
| 1503 | }; |
| 1504 | |
| 1505 | async function ensureProjectInitialized(project: ProjectState): Promise<void> { |
| 1506 | if (project.initPromise) { |
| 1507 | await project.initPromise; |
| 1508 | return; |
| 1509 | } |
| 1510 | |
| 1511 | if (project.indexState.status !== 'idle') { |
| 1512 | return; |
| 1513 | } |
| 1514 | |
| 1515 | project.initPromise = (async () => { |
| 1516 | // Migrate legacy structure |
| 1517 | try { |
| 1518 | const legacyPaths = makeLegacyPaths(project.rootPath); |
| 1519 | const migrated = await migrateToNewStructure(project.paths, legacyPaths); |
| 1520 | if (migrated && process.env.CODEBASE_CONTEXT_DEBUG) { |
| 1521 | console.error(`[DEBUG] Migrated to .codebase-context/ structure: ${project.rootPath}`); |
| 1522 | } |
| 1523 | } catch { |
| 1524 | // Non-fatal |
| 1525 | } |
| 1526 | |
| 1527 | // Check if indexing is needed |
| 1528 | const needsIndex = await shouldReindex(project.paths); |
| 1529 | if (needsIndex) { |
| 1530 | if (process.env.CODEBASE_CONTEXT_DEBUG) { |
| 1531 | console.error(`[DEBUG] Starting indexing: ${project.rootPath}`); |
| 1532 | } |
| 1533 | void performIndexing(project); |
| 1534 | } else { |
| 1535 | if (process.env.CODEBASE_CONTEXT_DEBUG) { |
| 1536 | console.error(`[DEBUG] Index found. Ready: ${project.rootPath}`); |
| 1537 | } |
| 1538 | project.indexState.status = 'ready'; |
| 1539 | project.indexState.lastIndexed = new Date(); |
| 1540 | } |
| 1541 | })().finally(() => { |
| 1542 | project.initPromise = undefined; |
| 1543 | }); |
| 1544 | |
| 1545 | await project.initPromise; |
| 1546 | } |
| 1547 | |
| 1548 | function ensureProjectWatcher(project: ProjectState, debounceMs: number): void { |
| 1549 | if (project.stopWatcher) { |
no test coverage detected