@collectui/cli 0.2.2 → 0.2.4
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 +11 -0
- package/package.json +1 -1
- package/src/args.mjs +3 -0
- package/src/cli.mjs +13 -0
- package/src/commands/setup.mjs +0 -2
- package/src/commands/update.mjs +48 -0
- package/src/http.mjs +2 -2
- package/src/login.mjs +0 -2
- package/src/update.mjs +172 -0
package/README.md
CHANGED
|
@@ -166,6 +166,17 @@ collectui doctor # 登录、account、Dash MCP、项目 registries/en
|
|
|
166
166
|
collectui doctor --deep # 再对每个已拥有库做一次 registry 探测(会消耗额度,慎用)
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
+
## 自动更新
|
|
170
|
+
|
|
171
|
+
CLI 每 12 小时后台查一次 npm(本地缓存 `~/.collectui/update-check.json`,绝不阻塞命令)。
|
|
172
|
+
交互终端下**默认自动更新**:发现新版即后台 `npm i -g @collectui/cli@latest`,
|
|
173
|
+
下次运行生效,当前命令不受影响。
|
|
174
|
+
|
|
175
|
+
- 关掉:`collectui update --off`(仍会提示新版,只是不自动动手);
|
|
176
|
+
或环境变量 `COLLECTUI_NO_AUTO_UPDATE=1`(CI / Agent 全静默)
|
|
177
|
+
- 手动:`collectui update`(前台看着装);`collectui update --check`(只查不装)
|
|
178
|
+
- `--json` / 非 TTY / `npx` 临时跑:完全不碰更新逻辑
|
|
179
|
+
|
|
169
180
|
## 支持的库
|
|
170
181
|
|
|
171
182
|
| 站点 slug | 库 | registry scope | MCP |
|
package/package.json
CHANGED
package/src/args.mjs
CHANGED
package/src/cli.mjs
CHANGED
|
@@ -14,6 +14,8 @@ import { CLI_NAME, CLI_VERSION } from "./version.mjs";
|
|
|
14
14
|
import { collectSecrets, readConfig, resolveToken } from "./config.mjs";
|
|
15
15
|
import { CliError, usageError } from "./errors.mjs";
|
|
16
16
|
import { createReporter } from "./report.mjs";
|
|
17
|
+
import { startBackgroundUpdate } from "./update.mjs";
|
|
18
|
+
import { showBanner } from "./banner.mjs";
|
|
17
19
|
import { loginCmd, logoutCmd } from "./commands/login.mjs";
|
|
18
20
|
import { whoamiCmd } from "./commands/whoami.mjs";
|
|
19
21
|
import { libsCmd } from "./commands/libs.mjs";
|
|
@@ -24,6 +26,7 @@ import { doctorCmd } from "./commands/doctor.mjs";
|
|
|
24
26
|
import { mcpCmd } from "./commands/mcp.mjs";
|
|
25
27
|
import { dashCmd } from "./commands/dash.mjs";
|
|
26
28
|
import { skillCmd } from "./commands/skill.mjs";
|
|
29
|
+
import { updateCmd } from "./commands/update.mjs";
|
|
27
30
|
|
|
28
31
|
const HELP = `collectui ${CLI_VERSION} — one CLI for every shadcn library on CollectUI Dash
|
|
29
32
|
|
|
@@ -42,6 +45,7 @@ Commands:
|
|
|
42
45
|
dash <sub> Dash MCP: me, repos, download, feedback, tools, call
|
|
43
46
|
skill [dir] Install the agent skill (.claude/skills/collectui)
|
|
44
47
|
doctor Verify login, registries, env and MCP config
|
|
48
|
+
update Update the CLI now (--check only / --off/--on toggles auto-update)
|
|
45
49
|
|
|
46
50
|
Global options:
|
|
47
51
|
--token <PAT> Dash PAT (dpat_...); overrides saved login
|
|
@@ -87,6 +91,7 @@ Environment:
|
|
|
87
91
|
COLLECTUI_DASH_URL default https://dash.collectui.pro
|
|
88
92
|
COLLECTUI_REGISTRY_URL default https://registry.collectui.pro
|
|
89
93
|
COLLECTUI_HOME config directory override
|
|
94
|
+
COLLECTUI_NO_AUTO_UPDATE=1 disable background update check (CI/Agent silent)
|
|
90
95
|
`;
|
|
91
96
|
|
|
92
97
|
const COMMANDS = {
|
|
@@ -101,6 +106,7 @@ const COMMANDS = {
|
|
|
101
106
|
dash: { run: dashCmd, aliases: [] },
|
|
102
107
|
skill: { run: skillCmd, aliases: ["install-skill"] },
|
|
103
108
|
doctor: { run: doctorCmd, aliases: ["check"] },
|
|
109
|
+
update: { run: updateCmd, aliases: ["upgrade"] },
|
|
104
110
|
};
|
|
105
111
|
|
|
106
112
|
function findCommand(name) {
|
|
@@ -158,6 +164,13 @@ async function main() {
|
|
|
158
164
|
const reporter = makeReporter(flags, token);
|
|
159
165
|
if (token) reporter.addSecrets([token]);
|
|
160
166
|
|
|
167
|
+
showBanner(reporter); // 开机画面:任何子命令(headless/--version/help 已在前面分流)
|
|
168
|
+
// 自更新:交互 TTY 下后台检查/自动更(headless/CI 由门控静默跳过);
|
|
169
|
+
// 手动 update 命令自身不触发(避免与前台安装并发写全局目录)
|
|
170
|
+
if (name !== "update" && name !== "upgrade") {
|
|
171
|
+
startBackgroundUpdate(reporter, { config: readConfig() });
|
|
172
|
+
}
|
|
173
|
+
|
|
161
174
|
await command.run(flags, rest, reporter);
|
|
162
175
|
}
|
|
163
176
|
|
package/src/commands/setup.mjs
CHANGED
|
@@ -10,7 +10,6 @@ import { fetchMe } from "../dash.mjs";
|
|
|
10
10
|
import { planMcpConfig, describeEntries } from "../mcp.mjs";
|
|
11
11
|
import { detectPackageManager, planProjectConfig } from "../project.mjs";
|
|
12
12
|
import { planManifest, planScaffold } from "../scaffold.mjs";
|
|
13
|
-
import { showBanner } from "../banner.mjs";
|
|
14
13
|
import { selectLibs } from "./common.mjs";
|
|
15
14
|
|
|
16
15
|
const DEFAULT_CLIENTS = ["claude", "cursor", "vscode"];
|
|
@@ -61,7 +60,6 @@ function isNonEmptyDir(dir) {
|
|
|
61
60
|
*/
|
|
62
61
|
export async function runSetup({ flags, rest = [], reporter, mode }) {
|
|
63
62
|
reporter.intro(`CollectUI CLI · ${mode}`);
|
|
64
|
-
if (mode === "init") showBanner(reporter);
|
|
65
63
|
|
|
66
64
|
let token = resolveToken(flags);
|
|
67
65
|
if (!token) {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { CLI_VERSION } from "../version.mjs";
|
|
2
|
+
import { compareVersions, fetchLatestVersion, runManualUpdate } from "../update.mjs";
|
|
3
|
+
import { writeConfig } from "../config.mjs";
|
|
4
|
+
import { CliError } from "../errors.mjs";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* collectui update 手动立即更新(前台,看着跑)
|
|
8
|
+
* collectui update --check 只查不更(打 registry,不走缓存)
|
|
9
|
+
* collectui update --off/--on 关/开自动更新(写 config.autoUpdate)
|
|
10
|
+
*/
|
|
11
|
+
export async function updateCmd(flags, _rest, reporter) {
|
|
12
|
+
if (flags.off || flags.on) {
|
|
13
|
+
if (flags.off && flags.on) {
|
|
14
|
+
throw new CliError("--off and --on are mutually exclusive.", { code: 2 });
|
|
15
|
+
}
|
|
16
|
+
const next = writeConfig({ autoUpdate: !flags.off });
|
|
17
|
+
reporter.result({ autoUpdate: next.autoUpdate !== false });
|
|
18
|
+
reporter.outro(next.autoUpdate !== false ? "Auto-update enabled." : "Auto-update disabled (still notifies on new versions).");
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
reporter.step(1, 2, "Checking npm for the latest @collectui/cli");
|
|
23
|
+
const latest = await fetchLatestVersion();
|
|
24
|
+
const cmp = compareVersions(latest, CLI_VERSION);
|
|
25
|
+
const summary = { current: CLI_VERSION, latest, updateAvailable: cmp > 0 };
|
|
26
|
+
|
|
27
|
+
if (cmp <= 0) {
|
|
28
|
+
reporter.result(summary);
|
|
29
|
+
reporter.outro(`Already on the latest version (${CLI_VERSION}).`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (flags.check) {
|
|
34
|
+
reporter.result(summary);
|
|
35
|
+
reporter.outro(`Update available: ${CLI_VERSION} → ${latest} (re-run without --check to install).`);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
reporter.step(2, 2, `Installing @collectui/cli@latest (${CLI_VERSION} → ${latest})`);
|
|
40
|
+
const res = runManualUpdate();
|
|
41
|
+
if (!res || res.status !== 0) {
|
|
42
|
+
throw new CliError(`npm install failed${res?.status ? ` (exit ${res.status})` : ""}.`, {
|
|
43
|
+
hint: "Run `npm i -g @collectui/cli` manually to see the npm error; check global-prefix permissions (sudo/nvm).",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
reporter.result({ ...summary, updated: true });
|
|
47
|
+
reporter.outro(`Updated to ${latest}. Re-run commands to pick it up.`);
|
|
48
|
+
}
|
package/src/http.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CliError } from "./errors.mjs";
|
|
2
|
+
import { CLI_VERSION } from "./version.mjs";
|
|
2
3
|
|
|
3
|
-
const
|
|
4
|
-
const UA = `collectui-cli/${VERSION} (Node.js ${process.version}; +https://dash.collectui.pro)`;
|
|
4
|
+
const UA = `collectui-cli/${CLI_VERSION} (Node.js ${process.version}; +https://dash.collectui.pro)`;
|
|
5
5
|
|
|
6
6
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
7
7
|
|
package/src/login.mjs
CHANGED
|
@@ -2,7 +2,6 @@ import { spawn } from "node:child_process";
|
|
|
2
2
|
import { randomBytes } from "node:crypto";
|
|
3
3
|
import { createServer } from "node:http";
|
|
4
4
|
import * as p from "@clack/prompts";
|
|
5
|
-
import { showBanner } from "./banner.mjs";
|
|
6
5
|
import { clearConfig, configFile, dashUrl, writeConfig } from "./config.mjs";
|
|
7
6
|
import { authError } from "./errors.mjs";
|
|
8
7
|
import { jsonRequest } from "./http.mjs";
|
|
@@ -52,7 +51,6 @@ export async function login({
|
|
|
52
51
|
pat = false,
|
|
53
52
|
}) {
|
|
54
53
|
reporter.intro("CollectUI CLI · login");
|
|
55
|
-
showBanner(reporter);
|
|
56
54
|
|
|
57
55
|
let token = providedToken || process.env.COLLECTUI_TOKEN || process.env.DASH_PAT || "";
|
|
58
56
|
token = token.trim();
|
package/src/update.mjs
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { configDir } from "./config.mjs";
|
|
5
|
+
import { httpRequest } from "./http.mjs";
|
|
6
|
+
import { CLI_VERSION } from "./version.mjs";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 自更新(2026-11-30):
|
|
10
|
+
* - 启动时后台异步检查(12h 间隔 + ~/.collectui/update-check.json 缓存),绝不阻塞命令
|
|
11
|
+
* - TTY 交互下默认自动后台更新(spawn npm i -g,当前进程不受影响);headless/--json
|
|
12
|
+
* 完全不碰(Agent/CI 输出契约不可被自更新搅动)
|
|
13
|
+
* - 开关:COLLECTUI_NO_AUTO_UPDATE=1(CI 显式意图 → 全静默)或 config.autoUpdate=false
|
|
14
|
+
* (`collectui update --off` → 仍提示新版,但不自动动手);npx 临时跑不自动更
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export const NPM_PACKAGE = "@collectui/cli";
|
|
18
|
+
const REGISTRY_LATEST = "https://registry.npmjs.org/@collectui/cli/latest";
|
|
19
|
+
const CHECK_INTERVAL_MS = 12 * 60 * 60 * 1000;
|
|
20
|
+
const SPAWN_BACKOFF_MS = 24 * 60 * 60 * 1000;
|
|
21
|
+
|
|
22
|
+
function cacheFile() {
|
|
23
|
+
return join(configDir(), "update-check.json");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function readUpdateCache() {
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(readFileSync(cacheFile(), "utf8"));
|
|
29
|
+
} catch {
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function writeUpdateCache(patch) {
|
|
35
|
+
try {
|
|
36
|
+
mkdirSync(configDir(), { recursive: true, mode: 0o700 });
|
|
37
|
+
writeFileSync(cacheFile(), `${JSON.stringify({ ...readUpdateCache(), ...patch }, null, 2)}\n`, {
|
|
38
|
+
mode: 0o600,
|
|
39
|
+
});
|
|
40
|
+
} catch {
|
|
41
|
+
// 缓存写失败只影响提醒节奏,不影响功能
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** 语义版本比较(忽略 -suffix):-1 / 0 / 1。 */
|
|
46
|
+
export function compareVersions(a, b) {
|
|
47
|
+
const parts = (v) =>
|
|
48
|
+
String(v)
|
|
49
|
+
.split("-")[0]
|
|
50
|
+
.split(".")
|
|
51
|
+
.map((n) => Number.parseInt(n, 10) || 0);
|
|
52
|
+
const [pa, pb] = [parts(a), parts(b)];
|
|
53
|
+
for (let i = 0; i < 3; i += 1) {
|
|
54
|
+
const x = pa[i] ?? 0;
|
|
55
|
+
const y = pb[i] ?? 0;
|
|
56
|
+
if (x !== y) return x > y ? 1 : -1;
|
|
57
|
+
}
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 纯决策函数(单测覆盖):本次启动对更新做点什么。
|
|
63
|
+
* 返回 { mode, latest?, reason },mode ∈
|
|
64
|
+
* off-silent(env 显式禁 → 全静默)| none | notify(只提示)| notify-spawn(提示+后台更)| check(到点检查)
|
|
65
|
+
*/
|
|
66
|
+
export function decideUpdate({
|
|
67
|
+
env = process.env,
|
|
68
|
+
config = {},
|
|
69
|
+
cache = {},
|
|
70
|
+
now = Date.now(),
|
|
71
|
+
isTTY = process.stdout.isTTY,
|
|
72
|
+
version = CLI_VERSION,
|
|
73
|
+
intervalMs = CHECK_INTERVAL_MS,
|
|
74
|
+
spawnBackoffMs = SPAWN_BACKOFF_MS,
|
|
75
|
+
} = {}) {
|
|
76
|
+
const noAuto = env.COLLECTUI_NO_AUTO_UPDATE;
|
|
77
|
+
if (noAuto === "1" || noAuto === "true" || noAuto === "yes") {
|
|
78
|
+
return { mode: "off-silent", reason: "env-disabled" };
|
|
79
|
+
}
|
|
80
|
+
if (String(env.npm_execpath ?? "").includes("_npx")) {
|
|
81
|
+
return { mode: "none", reason: "npx" };
|
|
82
|
+
}
|
|
83
|
+
if (!isTTY) {
|
|
84
|
+
return { mode: "none", reason: "non-tty" };
|
|
85
|
+
}
|
|
86
|
+
const autoOn = config.autoUpdate !== false;
|
|
87
|
+
const outdated = cache.latest ? compareVersions(cache.latest, version) > 0 : false;
|
|
88
|
+
if (outdated) {
|
|
89
|
+
if (!autoOn) return { mode: "notify", latest: cache.latest, reason: "manual-mode" };
|
|
90
|
+
const spawnedRecently = cache.spawnedAt && now - cache.spawnedAt < spawnBackoffMs;
|
|
91
|
+
return spawnedRecently
|
|
92
|
+
? { mode: "notify", latest: cache.latest, reason: "spawn-pending" }
|
|
93
|
+
: { mode: "notify-spawn", latest: cache.latest, reason: "cached-outdated" };
|
|
94
|
+
}
|
|
95
|
+
if (now - (cache.lastCheck ?? 0) >= intervalMs) {
|
|
96
|
+
return { mode: "check", reason: "due" };
|
|
97
|
+
}
|
|
98
|
+
return { mode: "none", reason: "recent" };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** registry latest → 版本号;失败抛错(调用方决定吞还是报)。 */
|
|
102
|
+
export async function fetchLatestVersion({ timeout = 8_000 } = {}) {
|
|
103
|
+
const out = await httpRequest(REGISTRY_LATEST, { timeout, retries: 1 });
|
|
104
|
+
if (!out.res.ok) throw new Error(`registry ${out.res.status}`);
|
|
105
|
+
const parsed = JSON.parse(out.text);
|
|
106
|
+
const v = parsed?.version;
|
|
107
|
+
if (typeof v !== "string" || !v) throw new Error("registry response has no version");
|
|
108
|
+
return v;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 后台静默 spawn npm i -g;npm 不存在返回 false。 */
|
|
112
|
+
export function spawnGlobalInstall() {
|
|
113
|
+
const probe = spawnSync("npm", ["--version"], { timeout: 10_000, encoding: "utf8" });
|
|
114
|
+
if (probe.status !== 0 || probe.error) return false;
|
|
115
|
+
const child = spawn("npm", ["install", "-g", `${NPM_PACKAGE}@latest`], {
|
|
116
|
+
detached: true,
|
|
117
|
+
stdio: "ignore",
|
|
118
|
+
shell: process.platform === "win32",
|
|
119
|
+
});
|
|
120
|
+
child.unref();
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 后台异步检查 + 视开关自动更;fire-and-forget,绝不抛。 */
|
|
125
|
+
export function backgroundCheck({ autoOn = true, reporter } = {}) {
|
|
126
|
+
const started = Date.now();
|
|
127
|
+
return fetchLatestVersion()
|
|
128
|
+
.then((latest) => {
|
|
129
|
+
writeUpdateCache({ lastCheck: started, latest });
|
|
130
|
+
if (autoOn && compareVersions(latest, CLI_VERSION) > 0) {
|
|
131
|
+
const ok = spawnGlobalInstall();
|
|
132
|
+
writeUpdateCache({ spawnedAt: Date.now(), spawnTo: ok ? latest : null });
|
|
133
|
+
if (!ok && reporter) reporter.warn(`Auto-update failed (npm not found). Run \`npm i -g ${NPM_PACKAGE}\` manually.`);
|
|
134
|
+
}
|
|
135
|
+
return latest;
|
|
136
|
+
})
|
|
137
|
+
.catch(() => undefined);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 启动钩子(cli.main 里 fire-and-forget 调):按 decideUpdate 执行提示/检查/自动更。
|
|
142
|
+
* 提示走 reporter(TTY 人类输出);headless 由 decideUpdate 的 isTTY 门挡掉。
|
|
143
|
+
*/
|
|
144
|
+
export function startBackgroundUpdate(reporter, deps = {}) {
|
|
145
|
+
const decision = decideUpdate(deps);
|
|
146
|
+
if (decision.mode === "off-silent" || decision.mode === "none") return;
|
|
147
|
+
if (decision.mode === "notify" || decision.mode === "notify-spawn") {
|
|
148
|
+
const to = decision.latest;
|
|
149
|
+
if (decision.mode === "notify-spawn") {
|
|
150
|
+
const ok = spawnGlobalInstall();
|
|
151
|
+
writeUpdateCache({ spawnedAt: Date.now(), spawnTo: ok ? to : null });
|
|
152
|
+
reporter.info(
|
|
153
|
+
ok
|
|
154
|
+
? `⬆ ${NPM_PACKAGE} ${CLI_VERSION} → ${to}: updating in background — takes effect on next run.`
|
|
155
|
+
: `⬆ ${NPM_PACKAGE} ${to} available (auto-update failed; run \`npm i -g ${NPM_PACKAGE}\`).`
|
|
156
|
+
);
|
|
157
|
+
} else {
|
|
158
|
+
reporter.info(
|
|
159
|
+
`⬆ ${NPM_PACKAGE} ${CLI_VERSION} → ${to} available. Background update was already attempted — if your version didn't change, run \`collectui update\`.`
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
// mode === "check"
|
|
165
|
+
backgroundCheck({ autoOn: deps.config?.autoUpdate !== false, reporter });
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 手动更新(`collectui update`):前台跑,人看着进度。runner 可注入(测试)。 */
|
|
169
|
+
export function runManualUpdate({ runner } = {}) {
|
|
170
|
+
const run = runner ?? ((args) => spawnSync("npm", args, { stdio: "inherit", shell: process.platform === "win32" }));
|
|
171
|
+
return run(["install", "-g", `${NPM_PACKAGE}@latest`]);
|
|
172
|
+
}
|