Skip to content

Commit bc55ff7

Browse files
committed
refactor: Enhance DevlogManager and SQLiteStorageProvider with improved devlog creation and dynamic import handling
1 parent a9fc506 commit bc55ff7

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

packages/core/src/__tests__/devlog-manager.test.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ describe('DevlogManager', () => {
88
let testWorkspace: string;
99

1010
beforeEach(async () => {
11-
// Create a temporary test workspace
12-
testWorkspace = path.join(process.cwd(), 'test-workspace-' + Date.now());
13-
manager = new DevlogManager({ workspaceRoot: testWorkspace });
11+
// Create a temporary test workspace with unique name
12+
testWorkspace = path.join(process.cwd(), 'test-workspace-' + Date.now() + '-' + Math.random().toString(36).substr(2, 9));
13+
14+
// Create isolated storage config for each test
15+
const sqliteDbPath = path.join(testWorkspace, 'test.db');
16+
manager = new DevlogManager({
17+
workspaceRoot: testWorkspace,
18+
storage: {
19+
type: 'sqlite',
20+
filePath: sqliteDbPath
21+
}
22+
});
1423
});
1524

1625
afterEach(async () => {
@@ -22,9 +31,9 @@ describe('DevlogManager', () => {
2231
}
2332
});
2433

25-
describe('createDevlog', () => {
34+
describe('findOrCreateDevlog', () => {
2635
it('should create a new devlog entry', async () => {
27-
const result = await manager.createDevlog({
36+
const result = await manager.findOrCreateDevlog({
2837
title: 'Test Feature',
2938
type: 'feature',
3039
description: 'A test feature',
@@ -44,7 +53,7 @@ describe('DevlogManager', () => {
4453

4554
it('should create a devlog with custom ID', async () => {
4655
const customId = 'my-custom-id';
47-
const result = await manager.createDevlog({
56+
const result = await manager.findOrCreateDevlog({
4857
id: customId,
4958
title: 'Custom ID Test',
5059
type: 'task',
@@ -230,10 +239,7 @@ describe('DevlogManager', () => {
230239
description: 'Testing notes'
231240
});
232241

233-
const result = await manager.addNote(created.id, {
234-
category: 'progress',
235-
content: 'Made some progress on this task'
236-
});
242+
const result = await manager.addNote(created.id, 'Made some progress on this task', 'progress');
237243

238244
expect(result.notes).toHaveLength(1);
239245
expect(result.notes[0].content).toBe('Made some progress on this task');

packages/core/src/devlog-manager.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,22 @@ export class DevlogManager {
7272
this.initialized = true;
7373
}
7474

75+
/**
76+
* Create a new devlog entry or find existing one with same title and type
77+
* Alias for findOrCreateDevlog to provide a cleaner API
78+
*/
79+
async createDevlog(request: CreateDevlogRequest): Promise<DevlogEntry> {
80+
return this.findOrCreateDevlog(request);
81+
}
82+
7583
/**
7684
* Create a new devlog entry or find existing one with same title and type
7785
*/
7886
async findOrCreateDevlog(request: CreateDevlogRequest): Promise<DevlogEntry> {
7987
await this.ensureInitialized();
8088

81-
const id = this.generateId(request.title, request.type);
89+
// Use provided ID if available, otherwise generate one
90+
const id = request.id || this.generateId(request.title, request.type);
8291

8392
// Check if entry already exists
8493
const existing = await this.storageProvider.get(id);
@@ -235,6 +244,12 @@ export class DevlogManager {
235244
*/
236245
async deleteDevlog(id: string): Promise<void> {
237246
await this.ensureInitialized();
247+
248+
const existing = await this.storageProvider.get(id);
249+
if (!existing) {
250+
throw new Error(`Devlog entry with ID '${id}' not found`);
251+
}
252+
238253
await this.storageProvider.delete(id);
239254
}
240255

@@ -262,6 +277,20 @@ export class DevlogManager {
262277
return await this.storageProvider.get(id);
263278
}
264279

280+
/**
281+
* Get active devlog entries for AI context
282+
*/
283+
async getActiveContext(limit?: number): Promise<DevlogEntry[]> {
284+
await this.ensureInitialized();
285+
286+
const filter = {
287+
status: ["todo", "in-progress", "review", "testing"] as any[]
288+
};
289+
290+
const entries = await this.storageProvider.list(filter);
291+
return entries.slice(0, limit || 10);
292+
}
293+
265294
/**
266295
* Update AI context for a devlog entry
267296
*/

packages/core/src/storage/sqlite-storage.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,20 @@ export class SQLiteStorageProvider implements StorageProvider {
4545
// Dynamic import to make better-sqlite3 optional
4646
try {
4747
console.log(`[SQLiteStorage] Attempting to import better-sqlite3...`);
48-
// Use eval to prevent TypeScript from transforming the import
49-
const dynamicImport = eval('(specifier) => import(specifier)');
50-
const sqlite3Module = await dynamicImport("better-sqlite3");
51-
console.log(`[SQLiteStorage] Successfully imported better-sqlite3`);
48+
49+
// Try different import approaches for better compatibility
50+
let sqlite3Module;
51+
try {
52+
// Standard dynamic import
53+
sqlite3Module = await import("better-sqlite3");
54+
console.log(`[SQLiteStorage] Successfully imported better-sqlite3 via standard import`);
55+
} catch (importError) {
56+
console.log(`[SQLiteStorage] Standard import failed, trying eval approach...`);
57+
// Fallback to eval approach for environments that require it
58+
const dynamicImport = eval('(specifier) => import(specifier)');
59+
sqlite3Module = await dynamicImport("better-sqlite3");
60+
console.log(`[SQLiteStorage] Successfully imported better-sqlite3 via eval import`);
61+
}
5262

5363
const Database = sqlite3Module.default;
5464
console.log(`[SQLiteStorage] Creating database instance...`);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'better-sqlite3';

packages/core/vitest.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ export default defineConfig({
55
environment: 'node',
66
globals: true,
77
testTimeout: 30000,
8+
// Handle dynamic imports better
9+
deps: {
10+
external: ['better-sqlite3']
11+
}
812
},
913
});

0 commit comments

Comments
 (0)