All files / src/lib/db release-channel.ts

100% Statements 15/15
100% Branches 8/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                              15x                     99x 99x   99x 18x   81x                         30x 30x 30x 30x 23x                       11x 11x 9x   2x    
/**
 * Release channel persistence.
 *
 * Stores the user's chosen release channel ("stable" or "nightly") in the
 * metadata table. Defaults to "stable" if not set.
 *
 * The channel controls which version stream `upgrade` and `version-check` use:
 * - "stable": tracks the latest GitHub release (default)
 * - "nightly": tracks the rolling nightly prerelease built from main
 */
 
import { getDatabase } from "./index.js";
import { getMetadata, setMetadata } from "./utils.js";
import { clearVersionCheckCache } from "./version-check.js";
 
const KEY = "release_channel";
 
/** The release channel a user tracks for upgrades and version-check notifications. */
export type ReleaseChannel = "stable" | "nightly";
 
/**
 * Get the persisted release channel.
 *
 * @returns The stored channel, or "stable" if not yet set.
 */
export function getReleaseChannel(): ReleaseChannel {
  const db = getDatabase();
  const m = getMetadata(db, [KEY]);
 
  if (m.get(KEY) === "nightly") {
    return "nightly";
  }
  return "stable";
}
 
/**
 * Persist the release channel.
 *
 * If the channel has changed, the cached version-check result is also cleared
 * so the next notification does not display a stale version from the old channel
 * (e.g. a cached stable version labelled as a nightly update after switching).
 *
 * @param channel - Channel to store
 */
export function setReleaseChannel(channel: ReleaseChannel): void {
  const db = getDatabase();
  const current = getReleaseChannel();
  setMetadata(db, { [KEY]: channel });
  if (channel !== current) {
    clearVersionCheckCache();
  }
}
 
/**
 * Parse and validate a release channel from user input.
 *
 * @param value - Raw string from --channel flag or "nightly"/"stable" positional
 * @returns Validated ReleaseChannel
 * @throws {Error} When the value is not a recognized channel
 */
export function parseReleaseChannel(value: string): ReleaseChannel {
  const normalized = value.toLowerCase();
  if (normalized === "stable" || normalized === "nightly") {
    return normalized;
  }
  throw new Error(`Invalid channel: ${value}. Must be one of: stable, nightly`);
}