All files / src update-checker.ts

100% Statements 18/18
100% Branches 16/16
100% Functions 2/2
100% Lines 14/14

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 311x 14x 14x 14x 30x 22x   4x     1x       4x 4x     3x 2x 2x 1x                  
export const isNewerVersion = (latest: string, current: string): boolean => {
  const l = latest.split(".").map(Number);
  const c = current.split(".").map(Number);
  for (let i = 0; i < 3; i++) {
    if ((l[i] ?? 0) > (c[i] ?? 0)) return true;
    if ((l[i] ?? 0) < (c[i] ?? 0)) return false;
  }
  return false;
};
 
export const checkForUpdate = async (
  packageName: string,
  currentVersion: string,
): Promise<void> => {
  try {
    const response = await fetch(
      `https://registry.npmjs.org/${packageName}/latest`,
    );
    if (!response.ok) return;
    const data = (await response.json()) as { version: string };
    if (isNewerVersion(data.version, currentVersion)) {
      process.stderr.write(
        `\n  Update available: ${currentVersion} → ${data.version}\n` +
          `  Run: npm install -g ${packageName}@latest\n\n`,
      );
    }
  } catch {
    // Best-effort — silently ignore network errors
  }
};