UNPKG

@dephub/cache

Version:

Simple file-based cache with persistent storage for Node.js applications

38 lines (37 loc) 1.62 kB
#!/usr/bin/env node import { cli as r } from "@dephub/cli"; import { logger as a } from "@dephub/logger"; import { FileCache as s } from "./core/file.js"; import { name as i, version as l, description as m } from "./utils/pkg.js"; r.name(i.split("/")[1] ?? "cache").version(l).description(m); r.command("get", "Get value from cache").argument("<key>", "Cache key to retrieve").action(async ({ args: t }) => { const [e] = t, n = await new s().get(e); n !== void 0 ? a.log(n.toString()) : (a.error(`Key not found: ${e}`), process.exit(1)); }); r.command("delete", "Delete value from cache").argument("<key>", "Cache key to delete").action(async ({ args: t }) => { const [e] = t; await new s().delete(e) ? a.success(`Deleted ${e}`) : (a.error(`Key not found: ${e}`), process.exit(1)); }); r.command("set", "Set value in cache").argument("<key>", "Cache key").argument("<value>", "Cache value (string, number, or boolean)").action(async ({ args: t }) => { const [e, c] = t, n = new s(); try { let o = c; c === "true" ? o = !0 : c === "false" ? o = !1 : isNaN(Number(c)) || (o = Number(c)), await n.set(e, o), a.success(`Set ${e} = ${o}`); } catch (o) { a.error(`Failed to set value: ${o.message}`), process.exit(1); } }); r.command("list", "List all cache entries").action(async () => { const e = await new s().entries(); if (e.length === 0) { a.log("Cache is empty"); return; } e.forEach(([c, n]) => { a.log(`${c} = ${n}`); }); }); r.command("clear", "Clear all cache entries").action(async () => { await new s().clear(), a.success("Cache cleared"); }); await r.run();