MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / open

Method open

src/index.ts:476–519  ·  view source on GitHub ↗

* 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 = {})

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 12

syncMethod · 0.95
destroyMethod · 0.95
initGrammarsFunction · 0.90
isInitializedFunction · 0.90
validateDirectoryFunction · 0.90
getDatabasePathFunction · 0.90
getDbMethod · 0.80
resolveMethod · 0.65
openMethod · 0.65
closeMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected