diff --git a/package.json b/package.json index 53e378a1..67c32e07 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "exports": { ".": { "import": "./src/index.js" - } + }, + "./package.json": "./package.json" }, "bin": { "codegraph": "./src/cli.js" diff --git a/src/cli.js b/src/cli.js index b5a48221..ccf8fbc4 100644 --- a/src/cli.js +++ b/src/cli.js @@ -2,12 +2,11 @@ import fs from 'node:fs'; import path from 'node:path'; -import Database from 'better-sqlite3'; import { Command } from 'commander'; import { buildGraph } from './builder.js'; import { loadConfig } from './config.js'; import { findCycles, formatCycles } from './cycles.js'; -import { findDbPath } from './db.js'; +import { openReadonlyOrFail } from './db.js'; import { buildEmbeddings, EMBEDDING_STRATEGIES, MODELS, search } from './embedder.js'; import { exportDOT, exportJSON, exportMermaid } from './export.js'; import { setVerbose } from './logger.js'; @@ -275,7 +274,7 @@ program .option('--min-confidence ', 'Minimum edge confidence threshold (default: 0.5)', '0.5') .option('-o, --output ', 'Write to file instead of stdout') .action((opts) => { - const db = new Database(findDbPath(opts.db), { readonly: true }); + const db = openReadonlyOrFail(opts.db); const exportOpts = { fileLevel: !opts.functions, noTests: resolveNoTests(opts), @@ -314,7 +313,7 @@ program .option('--include-tests', 'Include test/spec files (overrides excludeTests config)') .option('-j, --json', 'Output as JSON') .action((opts) => { - const db = new Database(findDbPath(opts.db), { readonly: true }); + const db = openReadonlyOrFail(opts.db); const cycles = findCycles(db, { fileLevel: !opts.functions, noTests: resolveNoTests(opts) }); db.close(); diff --git a/src/embedder.js b/src/embedder.js index 67eb39e5..20882e9b 100644 --- a/src/embedder.js +++ b/src/embedder.js @@ -324,6 +324,14 @@ export async function buildEmbeddings(rootDir, modelKey, customDbPath, options = const strategy = options.strategy || 'structured'; const dbPath = customDbPath || findDbPath(null); + if (!fs.existsSync(dbPath)) { + console.error( + `No codegraph database found at ${dbPath}.\n` + + `Run "codegraph build" first to analyze your codebase.`, + ); + process.exit(1); + } + const db = new Database(dbPath); initEmbeddingsSchema(db);