* Open an existing CodeGraph project * * @param projectRoot - Path to the project root directory * @param options - Open options * @returns A CodeGraph instance
(projectRoot: string, options: OpenOptions = {})
| 470 | * @returns A CodeGraph instance |
| 471 | */ |
| 472 | static async open(projectRoot: string, options: OpenOptions = {}): Promise<CodeGraph> { |
| 473 | await initGrammars(); |
| 474 | const resolvedRoot = path.resolve(projectRoot); |
| 475 | |
| 476 | // Check if initialized |
| 477 | if (!isInitialized(resolvedRoot)) { |
| 478 | throw new Error(`CodeGraph not initialized in ${resolvedRoot}. Run init() first.`); |
| 479 | } |
| 480 | |
| 481 | // Validate directory structure |
| 482 | const validation = validateDirectory(resolvedRoot); |
| 483 | if (!validation.valid) { |
| 484 | throw new Error(`Invalid CodeGraph directory: ${validation.errors.join(', ')}`); |
| 485 | } |
| 486 | |
| 487 | // Open database. The secondary indexes an interrupted bulk load dropped are |
| 488 | // rebuilt off this thread, and finished before the instance is returned |
| 489 | // (#1887): rebuilt in line on a large index, they blocked the thread longer |
| 490 | // than the liveness watchdog allows, and every restart was killed the same |
| 491 | // way. |
| 492 | const dbPath = getDatabasePath(resolvedRoot); |
| 493 | const db = DatabaseConnection.open(dbPath, { readOnly: options.readOnly, deferIndexRecovery: true }); |
| 494 | try { |
| 495 | await db.recoverIndexesOffThread(); |
| 496 | } catch (err) { |
| 497 | db.close(); |
| 498 | throw err; |
| 499 | } |
| 500 | const queries = new QueryBuilder(db.getDb()); |
| 501 | |
| 502 | const instance = new CodeGraph(db, queries, resolvedRoot); |
| 503 | |
| 504 | // Sync if requested |
| 505 | if (options.sync && !options.readOnly) { |
| 506 | try { |
| 507 | await instance.sync(); |
| 508 | } catch (err) { |
| 509 | instance.destroy(); |
| 510 | throw err; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | return instance; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Rebuild the project's database from scratch and return a fresh, empty |
nothing calls this directly
no test coverage detected