Skip to content

Commit 3f56644

Browse files
fix(cli): graceful error for cycles, export, embed when no graph.db exists
Use openReadonlyOrFail() instead of raw Database() constructor in the export and cycles command handlers. Add fs.existsSync() guard in buildEmbeddings(). Also export ./package.json in the exports map. Closes #77 Closes #78 Impact: 1 functions changed, 1 affected
1 parent 136421c commit 3f56644

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"exports": {
88
".": {
99
"import": "./src/index.js"
10-
}
10+
},
11+
"./package.json": "./package.json"
1112
},
1213
"bin": {
1314
"codegraph": "./src/cli.js"

src/cli.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
import fs from 'node:fs';
44
import path from 'node:path';
5-
import Database from 'better-sqlite3';
65
import { Command } from 'commander';
76
import { buildGraph } from './builder.js';
87
import { loadConfig } from './config.js';
98
import { findCycles, formatCycles } from './cycles.js';
10-
import { findDbPath } from './db.js';
9+
import { openReadonlyOrFail } from './db.js';
1110
import { buildEmbeddings, EMBEDDING_STRATEGIES, MODELS, search } from './embedder.js';
1211
import { exportDOT, exportJSON, exportMermaid } from './export.js';
1312
import { setVerbose } from './logger.js';
@@ -275,7 +274,7 @@ program
275274
.option('--min-confidence <score>', 'Minimum edge confidence threshold (default: 0.5)', '0.5')
276275
.option('-o, --output <file>', 'Write to file instead of stdout')
277276
.action((opts) => {
278-
const db = new Database(findDbPath(opts.db), { readonly: true });
277+
const db = openReadonlyOrFail(opts.db);
279278
const exportOpts = {
280279
fileLevel: !opts.functions,
281280
noTests: resolveNoTests(opts),
@@ -314,7 +313,7 @@ program
314313
.option('--include-tests', 'Include test/spec files (overrides excludeTests config)')
315314
.option('-j, --json', 'Output as JSON')
316315
.action((opts) => {
317-
const db = new Database(findDbPath(opts.db), { readonly: true });
316+
const db = openReadonlyOrFail(opts.db);
318317
const cycles = findCycles(db, { fileLevel: !opts.functions, noTests: resolveNoTests(opts) });
319318
db.close();
320319

src/embedder.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ export async function buildEmbeddings(rootDir, modelKey, customDbPath, options =
324324
const strategy = options.strategy || 'structured';
325325
const dbPath = customDbPath || findDbPath(null);
326326

327+
if (!fs.existsSync(dbPath)) {
328+
console.error(
329+
`No codegraph database found at ${dbPath}.\n` +
330+
`Run "codegraph build" first to analyze your codebase.`,
331+
);
332+
process.exit(1);
333+
}
334+
327335
const db = new Database(dbPath);
328336
initEmbeddingsSchema(db);
329337

0 commit comments

Comments
 (0)