| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { getDbInstance, isBuildPhase, isCloud } from "./core"; |
|
|
| type JsonRecord = Record<string, unknown>; |
|
|
| interface StatementLike<TRow = unknown> { |
| all: (...params: unknown[]) => TRow[]; |
| get: (...params: unknown[]) => TRow | undefined; |
| run: (...params: unknown[]) => { changes?: number }; |
| } |
|
|
| interface DbLike { |
| prepare: <TRow = unknown>(sql: string) => StatementLike<TRow>; |
| } |
|
|
| interface KeyValueRow { |
| key: string; |
| value: string; |
| } |
|
|
| function parseJsonValue(raw: string): unknown { |
| try { |
| return JSON.parse(raw) as unknown; |
| } catch { |
| return null; |
| } |
| } |
|
|
| function toRecord(value: unknown): JsonRecord | null { |
| return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : null; |
| } |
|
|
| |
|
|
| |
| |
| |
| export function saveCliToolLastConfigured( |
| toolId: string, |
| timestamp: string = new Date().toISOString() |
| ): void { |
| if (isBuildPhase || isCloud) return; |
| const db = getDbInstance() as unknown as DbLike; |
| db.prepare("INSERT OR REPLACE INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run( |
| "cliToolLastConfig", |
| toolId, |
| JSON.stringify(timestamp) |
| ); |
| } |
|
|
| |
| |
| |
| |
| export function getCliToolLastConfigured(toolId: string): string | null { |
| if (isBuildPhase || isCloud) return null; |
| const db = getDbInstance() as unknown as DbLike; |
| const row = db |
| .prepare("SELECT value FROM key_value WHERE namespace = ? AND key = ?") |
| .get("cliToolLastConfig", toolId); |
| if (!row) return null; |
| const parsed = parseJsonValue((row as KeyValueRow).value); |
| return typeof parsed === "string" ? parsed : null; |
| } |
|
|
| |
| |
| |
| |
| export function getAllCliToolLastConfigured(): Record<string, string> { |
| if (isBuildPhase || isCloud) return {}; |
| const db = getDbInstance() as unknown as DbLike; |
| const rows = db |
| .prepare("SELECT key, value FROM key_value WHERE namespace = ?") |
| .all("cliToolLastConfig") as KeyValueRow[]; |
| const result: Record<string, string> = {}; |
| for (const row of rows) { |
| const parsed = parseJsonValue(row.value); |
| if (typeof parsed === "string") { |
| result[row.key] = parsed; |
| } |
| } |
| return result; |
| } |
|
|
| |
| |
| |
| export function deleteCliToolLastConfigured(toolId: string): void { |
| if (isBuildPhase || isCloud) return; |
| const db = getDbInstance() as unknown as DbLike; |
| db.prepare("DELETE FROM key_value WHERE namespace = ? AND key = ?").run( |
| "cliToolLastConfig", |
| toolId |
| ); |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| |
| export function saveCliToolInitialConfig(toolId: string, config: JsonRecord): boolean { |
| if (isBuildPhase || isCloud) return false; |
| const db = getDbInstance() as unknown as DbLike; |
| |
| const existing = db |
| .prepare("SELECT value FROM key_value WHERE namespace = ? AND key = ?") |
| .get("cliToolInitialConfig", toolId); |
| if (existing) return false; |
|
|
| db.prepare("INSERT OR REPLACE INTO key_value (namespace, key, value) VALUES (?, ?, ?)").run( |
| "cliToolInitialConfig", |
| toolId, |
| JSON.stringify(config) |
| ); |
| return true; |
| } |
|
|
| |
| |
| |
| |
| export function getCliToolInitialConfig(toolId: string): JsonRecord | null { |
| if (isBuildPhase || isCloud) return null; |
| const db = getDbInstance() as unknown as DbLike; |
| const row = db |
| .prepare("SELECT value FROM key_value WHERE namespace = ? AND key = ?") |
| .get("cliToolInitialConfig", toolId); |
| if (!row) return null; |
| const parsed = parseJsonValue((row as KeyValueRow).value); |
| return toRecord(parsed); |
| } |
|
|
| |
| |
| |
| export function deleteCliToolInitialConfig(toolId: string): void { |
| if (isBuildPhase || isCloud) return; |
| const db = getDbInstance() as unknown as DbLike; |
| db.prepare("DELETE FROM key_value WHERE namespace = ? AND key = ?").run( |
| "cliToolInitialConfig", |
| toolId |
| ); |
| } |
|
|