/ opencodehub / cli / src / lib / stack-store.ts
Swadhin Biswas feat(arch): phase 1-4 github parity (sync, daemons, ui, queues) · 4/1/2026
d0c5c9d
76 lines · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';

export interface TrackedBranch {
  name: string;
  parent: string | null;
  commitHash: string;
  prNumber?: number;
  syncedAt: string;
}

export class StackStore {
  private configDir: string;
  private dbPath: string;

  constructor() {
    this.configDir = path.join(os.homedir(), '.config', 'opencodehub');
    this.dbPath = path.join(this.configDir, 'stacks.json');
    this.ensureDb();
  }

  private ensureDb() {
    if (!fs.existsSync(this.configDir)) {
      fs.mkdirSync(this.configDir, { recursive: true });
    }
    if (!fs.existsSync(this.dbPath)) {
      this.writeDb({ branches: {} });
    }
  }

  private readDb(): { branches: Record<string, TrackedBranch> } {
    try {
      const data = fs.readFileSync(this.dbPath, 'utf8');
      return JSON.parse(data);
    } catch {
      return { branches: {} };
    }
  }

  private writeDb(data: { branches: Record<string, TrackedBranch> }) {
    fs.writeFileSync(this.dbPath, JSON.stringify(data, null, 2), 'utf8');
  }

  public getBranch(name: string): TrackedBranch | undefined {
    return this.readDb().branches[name];
  }

  public trackBranch(branch: TrackedBranch) {
    const db = this.readDb();
    db.branches[branch.name] = branch;
    this.writeDb(db);
  }

  public updateBranchHash(name: string, commitHash: string) {
    const db = this.readDb();
    if (db.branches[name]) {
      db.branches[name].commitHash = commitHash;
      db.branches[name].syncedAt = new Date().toISOString();
      this.writeDb(db);
    }
  }

  public removeBranch(name: string) {
    const db = this.readDb();
    delete db.branches[name];
    this.writeDb(db);
  }

  public getAllBranches(): TrackedBranch[] {
    return Object.values(this.readDb().branches);
  }
}

export const stackStore = new StackStore();