All files / src/lib/db install-info.ts

100% Statements 15/15
62.5% Branches 5/8
100% Functions 3/3
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                        15x 15x 15x 15x   15x                                     71x 71x   71x 71x 16x     55x                               42x 42x                         61x 61x    
/**
 * Installation info persistence.
 *
 * Stores how the CLI was installed (method, path, version) in the metadata table.
 * This is used by the upgrade command to determine the appropriate upgrade method
 * without re-detecting every time.
 */
 
import type { InstallationMethod } from "../upgrade.js";
import { getDatabase } from "./index.js";
import { clearMetadata, getMetadata, setMetadata } from "./utils.js";
 
const KEY_METHOD = "install.method";
const KEY_PATH = "install.path";
const KEY_VERSION = "install.version";
const KEY_RECORDED_AT = "install.recorded_at";
 
const ALL_KEYS = [KEY_METHOD, KEY_PATH, KEY_VERSION, KEY_RECORDED_AT];
 
export type StoredInstallInfo = {
  /** How the CLI was installed */
  method: InstallationMethod;
  /** Absolute path to the binary */
  path: string;
  /** Version when installed or last upgraded */
  version: string;
  /** Unix timestamp (ms) when this info was recorded */
  recordedAt: number;
};
 
/**
 * Get the stored installation info.
 *
 * @returns Installation info if recorded, null otherwise
 */
export function getInstallInfo(): StoredInstallInfo | null {
  const db = getDatabase();
  const m = getMetadata(db, ALL_KEYS);
 
  const method = m.get(KEY_METHOD);
  if (!method) {
    return null;
  }
 
  return {
    method: method as InstallationMethod,
    path: m.get(KEY_PATH) ?? "",
    version: m.get(KEY_VERSION) ?? "",
    recordedAt: m.has(KEY_RECORDED_AT) ? Number(m.get(KEY_RECORDED_AT)) : 0,
  };
}
 
/**
 * Store installation info.
 *
 * @param info - Installation info to store (recordedAt is auto-set to now)
 */
export function setInstallInfo(
  info: Omit<StoredInstallInfo, "recordedAt">
): void {
  const db = getDatabase();
  setMetadata(db, {
    [KEY_METHOD]: info.method,
    [KEY_PATH]: info.path,
    [KEY_VERSION]: info.version,
    [KEY_RECORDED_AT]: String(Date.now()),
  });
}
 
/**
 * Clear stored installation info.
 * Useful for testing or when user wants to re-detect.
 */
export function clearInstallInfo(): void {
  const db = getDatabase();
  clearMetadata(db, ALL_KEYS);
}