cve-lite-cli 1.17.2 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,102 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import os from "node:os";
4
+ import { chalk, stripAnsi } from "./chalk.js";
5
+ import { getCliVersion } from "./version-info.js";
6
+ const NPM_REGISTRY_URL = "https://registry.npmjs.org/cve-lite-cli/latest";
7
+ const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
8
+ const BOX_INNER_WIDTH = 50;
9
+ export function isNewer(latest, current) {
10
+ const parse = (v) => {
11
+ const parts = v.replace(/^v/, "").split(".").map(Number);
12
+ return parts.length === 3 && parts.every(n => !isNaN(n)) ? parts : [];
13
+ };
14
+ const l = parse(latest);
15
+ const c = parse(current);
16
+ if (l.length === 0 || c.length === 0)
17
+ return false;
18
+ if (l[0] !== c[0])
19
+ return l[0] > c[0];
20
+ if (l[1] !== c[1])
21
+ return l[1] > c[1];
22
+ return l[2] > c[2];
23
+ }
24
+ function getCacheFilePath(cacheDir) {
25
+ const dir = cacheDir ? path.resolve(cacheDir) : path.join(os.homedir(), ".cache", "cve-lite");
26
+ fs.mkdirSync(dir, { recursive: true });
27
+ return path.join(dir, "update-check.json");
28
+ }
29
+ export function readCache(cacheDir) {
30
+ try {
31
+ const raw = fs.readFileSync(getCacheFilePath(cacheDir), "utf8");
32
+ const parsed = JSON.parse(raw);
33
+ if (typeof parsed.latestVersion === "string" && typeof parsed.checkedAt === "string") {
34
+ return { latestVersion: parsed.latestVersion, checkedAt: parsed.checkedAt };
35
+ }
36
+ return null;
37
+ }
38
+ catch {
39
+ return null;
40
+ }
41
+ }
42
+ export function writeCache(latestVersion, cacheDir) {
43
+ try {
44
+ fs.writeFileSync(getCacheFilePath(cacheDir), JSON.stringify({ latestVersion, checkedAt: new Date().toISOString() }), "utf8");
45
+ }
46
+ catch {
47
+ // silently ignore write failures
48
+ }
49
+ }
50
+ export function isCacheStale(cache) {
51
+ return Date.now() - new Date(cache.checkedAt).getTime() > CACHE_TTL_MS;
52
+ }
53
+ function fetchAndUpdateCache(cacheDir) {
54
+ void fetch(NPM_REGISTRY_URL)
55
+ .then(res => {
56
+ if (!res.ok)
57
+ return undefined;
58
+ return res.json();
59
+ })
60
+ .then((data) => {
61
+ if (data === undefined)
62
+ return;
63
+ if (data !== null &&
64
+ typeof data === "object" &&
65
+ "version" in data &&
66
+ typeof data.version === "string") {
67
+ writeCache(data.version, cacheDir);
68
+ }
69
+ })
70
+ .catch(() => { });
71
+ }
72
+ function renderBox(current, latest) {
73
+ const top = chalk.yellow("╭" + "─".repeat(BOX_INNER_WIDTH) + "╮");
74
+ const empty = chalk.yellow("│") + " ".repeat(BOX_INNER_WIDTH) + chalk.yellow("│");
75
+ const bottom = chalk.yellow("╰" + "─".repeat(BOX_INNER_WIDTH) + "╯");
76
+ const line1 = ` ${chalk.bold.yellow("Update available!")} ${chalk.gray(current)} ${chalk.yellow("→")} ${chalk.green(latest)}`;
77
+ const line2 = ` ${chalk.white("Run: npm install -g cve-lite-cli")}`;
78
+ const padLine = (content) => {
79
+ const visual = stripAnsi(content).length;
80
+ return chalk.yellow("│") + content + " ".repeat(Math.max(0, BOX_INNER_WIDTH - visual)) + chalk.yellow("│");
81
+ };
82
+ return [top, empty, padLine(line1), padLine(line2), empty, bottom].join("\n");
83
+ }
84
+ export function checkForUpdate(options) {
85
+ if (options?.json)
86
+ return;
87
+ if (options?.offline || options?.offlineDb)
88
+ return;
89
+ if (process.env["CI"])
90
+ return;
91
+ if (process.env["NO_UPDATE_NOTIFIER"])
92
+ return;
93
+ const cache = readCache(options?.cacheDir);
94
+ const current = getCliVersion();
95
+ if (cache !== null && isNewer(cache.latestVersion, current)) {
96
+ console.log(renderBox(current, cache.latestVersion));
97
+ console.log("");
98
+ }
99
+ if (cache === null || isCacheStale(cache)) {
100
+ fetchAndUpdateCache(options?.cacheDir);
101
+ }
102
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cve-lite-cli",
3
- "version": "1.17.2",
3
+ "version": "1.18.0",
4
4
  "description": "Developer-friendly CLI for scanning JS/TS projects for dependency vulnerabilities using local lockfiles and OSV",
5
5
  "type": "module",
6
6
  "bin": {