-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
226 lines (218 loc) · 6.24 KB
/
Copy pathmain.ts
File metadata and controls
226 lines (218 loc) · 6.24 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { Command } from "@cliffy/command";
import { resolve } from "@std/path";
import {
getDbStats,
getDirectoryOverview,
getSessionDetail,
getSessionsByDirectory,
openDb,
renameDirectory,
searchSessions,
} from "./lib/db.ts";
import { showDashboard } from "./lib/dashboard.ts";
import { showSpinner } from "./lib/spinner.ts";
import { VERSION } from "./version.ts";
import {
formatDbStats,
formatOverview,
formatRenameResult,
formatSearchResults,
formatSessionDetail,
formatSessionList,
} from "./lib/format.ts";
/**
* Resolve the opencode DB path from environment or default.
*/
function resolveDbPath(): string {
const envPath = Deno.env.get("OPENCODE_DB_PATH");
if (envPath) return resolve(envPath);
const home = Deno.env.get("HOME") ?? Deno.env.get("USERPROFILE");
if (!home) {
throw new Error(
"Cannot determine home directory. Set OPENCODE_DB_PATH explicitly.",
);
}
return resolve(home, ".local/share/opencode/opencode.db");
}
/**
* Format output as JSON or use the provided text formatter.
*/
function formatOutput<T>(
data: T,
format: string,
formatter: (d: T) => string,
): void {
if (format === "json") {
console.log(JSON.stringify(data, null, 2));
} else {
console.log(formatter(data));
}
}
/**
* Main entry point.
*/
async function main() {
const dbPath = resolveDbPath();
// No args → show dashboard (default behavior)
if (Deno.args.length === 0) {
showDashboard(dbPath, { top: 10, all: false, jsonMode: false });
return;
}
await new Command()
.name("opencode-visualizer")
.version(VERSION)
.description(
"OpenCode database visualizer and analytics. Reads ~/.local/share/opencode/opencode.db",
)
.globalOption(
"-o, --output <format:string>",
"Output format: text (default) or json",
{ default: "text" },
)
.command("dash", "Interactive dashboard with charts and stats")
.option("--top <n:number>", "Show top N items per section", { default: 10 })
.option("--all", "Show all items instead of top N")
.option(
"--exclude <dirs:string>",
"Exclude directories (comma-separated names)",
)
.option(
"--name <dirs:string>",
"Filter all panels to specific directories (comma-separated names)",
)
.action((options) => {
const names = options.name
? options.name.split(",").map((n: string) => n.trim()).filter((
n: string,
) => n.length > 0)
: undefined;
showDashboard(dbPath, {
top: options.top ?? 10,
all: options.all ?? false,
exclude: options.exclude,
names,
jsonMode: options.output === "json",
});
})
.command("sessions", "List sessions matching a directory path pattern")
.arguments("<path:string>")
.action((options, path: string) => {
const spinner = showSpinner("Loading data...");
try {
const db = openDb(dbPath);
const rows = getSessionsByDirectory(db, path);
spinner.stop();
formatOutput(rows, options.output, formatSessionList);
db.close();
} catch (cause) {
spinner.stop();
console.error(`Error: ${cause}`);
Deno.exit(1);
}
})
.command("session", "Show detailed info for a single session")
.arguments("<id:string>")
.action((options, id: string) => {
const spinner = showSpinner("Loading data...");
try {
const db = openDb(dbPath);
const detail = getSessionDetail(db, id);
spinner.stop();
if (options.output === "json") {
console.log(JSON.stringify(detail, null, 2));
} else {
console.log(
formatSessionDetail(
detail.session,
detail.message_count,
detail.todos,
),
);
}
db.close();
} catch (cause) {
spinner.stop();
console.error(`Error: ${cause}`);
Deno.exit(1);
}
})
.command("search", "Search sessions by title or directory")
.arguments("<query:string>")
.action((options, query: string) => {
const spinner = showSpinner("Loading data...");
try {
const db = openDb(dbPath);
const rows = searchSessions(db, query);
spinner.stop();
formatOutput(rows, options.output, formatSearchResults);
db.close();
} catch (cause) {
spinner.stop();
console.error(`Error: ${cause}`);
Deno.exit(1);
}
})
.command("rename", "Rename session directory (batch)")
.option(
"--from-dir <dir:string>",
"Current directory path to match",
{ required: true },
)
.option(
"-d, --directory <dir:string>",
"New directory path",
{ required: true },
)
.action((options) => {
const spinner = showSpinner("Renaming sessions...");
try {
const db = openDb(dbPath, false);
const result = renameDirectory(db, options.fromDir, options.directory);
spinner.stop();
formatOutput(result, options.output, formatRenameResult);
db.close();
} catch (cause) {
spinner.stop();
console.error(`Error: ${cause}`);
Deno.exit(1);
}
})
.command("stats", "Show overall database statistics")
.action((options) => {
const spinner = showSpinner("Loading data...");
try {
const db = openDb(dbPath);
const stats = getDbStats(db);
spinner.stop();
if (options.output === "json") {
console.log(JSON.stringify(stats, null, 2));
} else {
console.log(formatDbStats(stats));
}
db.close();
} catch (cause) {
spinner.stop();
console.error(`Error: ${cause}`);
Deno.exit(1);
}
})
.command("overview", "Show per-directory session overview table")
.action((options) => {
const spinner = showSpinner("Loading data...");
try {
const db = openDb(dbPath);
const rows = getDirectoryOverview(db);
spinner.stop();
formatOutput(rows, options.output, formatOverview);
db.close();
} catch (cause) {
spinner.stop();
console.error(`Error: ${cause}`);
Deno.exit(1);
}
})
.parse(Deno.args);
}
if (import.meta.main) {
main();
}