@baclet/cli 0.1.5 → 0.1.6

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.
package/README.md CHANGED
@@ -32,6 +32,7 @@ Node.js 18 or later is required.
32
32
  | `baclet db status` | Print the schema diff without applying it. |
33
33
  | `baclet db push [--yes]` | Apply the schema diff after confirmation. |
34
34
  | `baclet db migrate [--yes]` | Alias for `db push`. |
35
+ | `baclet upgrade` | Upgrade `@baclet/cli` to the latest npm version. |
35
36
  | `baclet --version` | Print the CLI version. |
36
37
 
37
38
  ## Login
@@ -82,6 +83,14 @@ baclet db push
82
83
 
83
84
  Pushes are additive: new tables, columns, constraints, and indexes are applied in one transaction. Columns that exist in the database but not in your models are reported as warnings and are never dropped.
84
85
 
86
+ ## Updates
87
+
88
+ BACLET CLI checks npm for a newer `@baclet/cli` version at most every 12 hours (TTY only). When an update is available, it prints a notice. Disable with `BACLET_NO_UPDATE_NOTIFIER=1`.
89
+
90
+ ```bash
91
+ baclet upgrade
92
+ ```
93
+
85
94
  ## Environment
86
95
 
87
96
  | Variable | Default | Purpose |
@@ -89,3 +98,4 @@ Pushes are additive: new tables, columns, constraints, and indexes are applied i
89
98
  | `BACLET_API_URL` | `https://api.baclet.app` | API host. |
90
99
  | `BACLET_DASHBOARD_URL` | `https://baclet.app` | Fallback activation link host. |
91
100
  | `XDG_CONFIG_HOME` | `~/.config` | Parent directory for credentials. |
101
+ | `BACLET_NO_UPDATE_NOTIFIER` | unset | Set to `1` to disable update checks. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baclet/cli",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "BACLET command line tool for authentication and schema pushes",
5
5
  "type": "module",
6
6
  "bin": {
package/src/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  import { readFileSync } from "node:fs";
3
3
  import { login, logout, whoami } from "./commands/auth.js";
4
4
  import { init, listProjects, dbStatus, dbPush } from "./commands/project.js";
5
+ import { upgrade, maybeNotifyUpdate } from "./update.js";
5
6
 
6
7
  const { version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
7
8
 
@@ -16,12 +17,15 @@ Usage:
16
17
  baclet db status Show the schema diff without applying it
17
18
  baclet db push [--yes] Apply the schema to your project database
18
19
  baclet db migrate [--yes] Alias for db push
20
+ baclet upgrade Upgrade @baclet/cli to the latest version
19
21
  baclet --version Print the CLI version
20
22
  baclet --help Show this message
21
23
  `;
22
24
 
23
25
  async function main() {
24
26
  const [command, ...args] = process.argv.slice(2);
27
+ const noNotify = new Set(["upgrade", "--version", "-v", "--help", "-h", undefined]);
28
+ if (!noNotify.has(command)) await maybeNotifyUpdate();
25
29
  switch (command) {
26
30
  case "login": return login(args);
27
31
  case "logout": return logout();
@@ -34,6 +38,9 @@ async function main() {
34
38
  if (args[0] === "status") return dbStatus();
35
39
  if (args[0] === "push" || args[0] === "migrate") return dbPush(args.slice(1));
36
40
  throw new Error("Usage: baclet db status|push|migrate");
41
+ case "upgrade":
42
+ case "update":
43
+ return upgrade();
37
44
  case "--version":
38
45
  case "-v":
39
46
  process.stdout.write(`${version}\n`);
package/src/update.js ADDED
@@ -0,0 +1,87 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { createRequire } from "node:module";
5
+ import { configDir } from "./paths.js";
6
+
7
+ const require = createRequire(import.meta.url);
8
+ const pkg = require("../package.json");
9
+ const stateFile = path.join(configDir, "update-state.json");
10
+ const CHECK_TTL_MS = 12 * 60 * 60 * 1000;
11
+
12
+ function parse(v = "") {
13
+ return String(v).replace(/^v/, "").split(/[.-]/).slice(0, 3).map((n) => Number.parseInt(n, 10) || 0);
14
+ }
15
+
16
+ export function isNewer(latest, current = pkg.version) {
17
+ const a = parse(latest), b = parse(current);
18
+ for (let i = 0; i < 3; i++) {
19
+ if (a[i] > b[i]) return true;
20
+ if (a[i] < b[i]) return false;
21
+ }
22
+ return false;
23
+ }
24
+
25
+ async function readState() {
26
+ try { return JSON.parse(await readFile(stateFile, "utf8")); } catch { return {}; }
27
+ }
28
+
29
+ async function writeState(state) {
30
+ await mkdir(configDir, { recursive: true, mode: 0o700 });
31
+ await writeFile(stateFile, JSON.stringify(state, null, 2), { mode: 0o600 });
32
+ }
33
+
34
+ export async function getLatestVersion() {
35
+ const res = await fetch(`https://registry.npmjs.org/${encodeURIComponent(pkg.name)}/latest`, {
36
+ headers: { Accept: "application/json" },
37
+ signal: AbortSignal.timeout(4000),
38
+ });
39
+ if (!res.ok) throw new Error(`npm registry returned ${res.status}`);
40
+ const data = await res.json();
41
+ if (!data?.version) throw new Error("No version in registry response");
42
+ return data.version;
43
+ }
44
+
45
+ export async function checkForUpdate({ force = false } = {}) {
46
+ const state = await readState();
47
+ const now = Date.now();
48
+ if (!force && state.checkedAt && now - new Date(state.checkedAt).getTime() < CHECK_TTL_MS && state.latest) {
49
+ return { current: pkg.version, latest: state.latest, updateAvailable: isNewer(state.latest) };
50
+ }
51
+ try {
52
+ const latest = await getLatestVersion();
53
+ const next = { checkedAt: new Date().toISOString(), latest };
54
+ await writeState(next);
55
+ return { current: pkg.version, latest, updateAvailable: isNewer(latest) };
56
+ } catch {
57
+ return { current: pkg.version, latest: state.latest || null, updateAvailable: state.latest ? isNewer(state.latest) : false };
58
+ }
59
+ }
60
+
61
+ export function notifyUpdate(info) {
62
+ if (!info?.updateAvailable) return;
63
+ console.error(`\nUpdate available: ${pkg.name} ${info.current} -> ${info.latest}`);
64
+ console.error(`Run \`baclet upgrade\` to update.\n`);
65
+ }
66
+
67
+ export async function maybeNotifyUpdate() {
68
+ if (!process.stdout.isTTY || process.env.BACLET_NO_UPDATE_NOTIFIER === "1") return;
69
+ const info = await checkForUpdate();
70
+ notifyUpdate(info);
71
+ }
72
+
73
+ export async function upgrade() {
74
+ const info = await checkForUpdate({ force: true });
75
+ if (!info.updateAvailable) {
76
+ console.log(`${pkg.name} is up to date (${info.current}).`);
77
+ return;
78
+ }
79
+ console.log(`Updating ${pkg.name} ${info.current} -> ${info.latest}...`);
80
+ const cmd = process.platform === "win32" ? "npm.cmd" : "npm";
81
+ const result = spawnSync(cmd, ["install", "-g", `${pkg.name}@latest`], { stdio: "inherit" });
82
+ if (result.status !== 0) {
83
+ throw new Error(`Upgrade failed. Try: npm install -g ${pkg.name}@latest`);
84
+ }
85
+ await writeState({ checkedAt: new Date().toISOString(), latest: info.latest });
86
+ console.log(`Updated to ${info.latest}.`);
87
+ }