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