@collectui/cli 0.2.9 → 0.2.11
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 +2 -2
- package/package.json +1 -1
- package/src/cli.mjs +9 -4
- package/src/commands/update.mjs +29 -5
- package/src/update.mjs +49 -40
package/README.md
CHANGED
|
@@ -168,8 +168,8 @@ collectui doctor --deep # 再对每个已拥有库做一次 registry 探测
|
|
|
168
168
|
|
|
169
169
|
## 更新检查
|
|
170
170
|
|
|
171
|
-
|
|
172
|
-
|
|
171
|
+
**每次运行命令时即时异步查一次 npm**(与命令并行,不阻塞;结束时最多等 1 秒收结果)。
|
|
172
|
+
发现新版会在启动或收尾时打一行提醒,装不装由你决定:
|
|
173
173
|
|
|
174
174
|
```bash
|
|
175
175
|
collectui update # 手动更新(前台看着装)
|
package/package.json
CHANGED
package/src/cli.mjs
CHANGED
|
@@ -165,12 +165,17 @@ async function main() {
|
|
|
165
165
|
if (token) reporter.addSecrets([token]);
|
|
166
166
|
|
|
167
167
|
showBanner(reporter); // 开机画面:任何子命令(headless/--version/help 已在前面分流)
|
|
168
|
-
//
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
168
|
+
// 更新检查:与命令并行即时异步查一次 npm(TTY 才查),有新版打一行提醒;
|
|
169
|
+
// 手动 update 命令自身不触发(避免与前台安装并发写全局目录)
|
|
170
|
+
const updateNotice =
|
|
171
|
+
name !== "update" && name !== "upgrade" ? startBackgroundUpdate(reporter) : null;
|
|
172
|
+
const race = (p, ms) => Promise.race([p, new Promise((r) => setTimeout(r, ms))]);
|
|
173
|
+
const settleUpdate = updateNotice
|
|
174
|
+
? () => race(updateNotice.done, 1_000).catch(() => {})
|
|
175
|
+
: () => Promise.resolve();
|
|
172
176
|
|
|
173
177
|
await command.run(flags, rest, reporter);
|
|
178
|
+
await settleUpdate();
|
|
174
179
|
}
|
|
175
180
|
|
|
176
181
|
/** 命令完成后 flush stdout 并退出:一次性进程不留后台尾巴(callback server 的
|
package/src/commands/update.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CLI_VERSION } from "../version.mjs";
|
|
2
2
|
import { compareVersions, fetchLatestVersion, runManualUpdate } from "../update.mjs";
|
|
3
3
|
import { CliError } from "../errors.mjs";
|
|
4
|
+
import * as p from "@clack/prompts";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* collectui update 手动立即更新(前台,看着跑)
|
|
@@ -24,13 +25,36 @@ export async function updateCmd(flags, _rest, reporter) {
|
|
|
24
25
|
return;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
// TTY 下 spinner 转着等 npm(输出捕获不插框);headless 直接 info
|
|
29
|
+
if (reporter.headless) {
|
|
30
|
+
reporter.info(`Installing @collectui/cli@latest (${CLI_VERSION} → ${latest}) via npm …`);
|
|
31
|
+
const res = await runManualUpdate();
|
|
32
|
+
if (!res || res.status !== 0) {
|
|
33
|
+
throw npmFail(res);
|
|
34
|
+
}
|
|
35
|
+
if (reporter.headless) reporter.result({ ...summary, updated: true });
|
|
36
|
+
reporter.outro(`Updated to ${latest}. Re-run commands to pick it up.`);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const sp = p.spinner();
|
|
41
|
+
sp.start(`Installing @collectui/cli@latest (${CLI_VERSION} → ${latest})`);
|
|
42
|
+
const res = await runManualUpdate();
|
|
29
43
|
if (!res || res.status !== 0) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
});
|
|
44
|
+
sp.stop("npm install failed", 1);
|
|
45
|
+
throw npmFail(res);
|
|
33
46
|
}
|
|
47
|
+
sp.stop(`Installed ${latest}`, 0);
|
|
34
48
|
if (reporter.headless) reporter.result({ ...summary, updated: true });
|
|
35
49
|
reporter.outro(`Updated to ${latest}. Re-run commands to pick it up.`);
|
|
36
50
|
}
|
|
51
|
+
|
|
52
|
+
function npmFail(res) {
|
|
53
|
+
const detail = `${res?.stderr ?? ""}${res?.stdout ?? ""}`.trim().split("\n").slice(-5).join("\n");
|
|
54
|
+
return new CliError(
|
|
55
|
+
`npm install failed${res?.status ? ` (exit ${res.status})` : ""}${detail ? `:\n${detail}` : "."}`,
|
|
56
|
+
{
|
|
57
|
+
hint: "Check global-prefix permissions (sudo/nvm), or run `npm i -g @collectui/cli` directly.",
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
}
|
package/src/update.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
2
|
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { configDir } from "./config.mjs";
|
|
@@ -7,15 +7,15 @@ import { CLI_VERSION } from "./version.mjs";
|
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* 更新检查(2026-12-01 起只提醒不自动装):
|
|
10
|
-
* -
|
|
11
|
-
*
|
|
10
|
+
* - CLI 是一次性进程,没有「后台周期任务」——每次运行命令时即时异步查一次 npm
|
|
11
|
+
* (与命令并行,命令结束时最多再等 1 秒收结果)
|
|
12
|
+
* - 发现新版 → 打一行显式提醒,装不装由用户跑 `collectui update` 决定
|
|
12
13
|
* - headless/--json 完全不碰(Agent/CI 输出契约不被搅动);npx 临时跑不需要
|
|
13
14
|
* - COLLECTUI_NO_AUTO_UPDATE=1:连检查带提示全静默(CI 显式意图)
|
|
14
15
|
*/
|
|
15
16
|
|
|
16
17
|
export const NPM_PACKAGE = "@collectui/cli";
|
|
17
18
|
const REGISTRY_LATEST = "https://registry.npmjs.org/@collectui/cli/latest";
|
|
18
|
-
const CHECK_INTERVAL_MS = 12 * 60 * 60 * 1000;
|
|
19
19
|
|
|
20
20
|
function cacheFile() {
|
|
21
21
|
return join(configDir(), "update-check.json");
|
|
@@ -57,16 +57,12 @@ export function compareVersions(a, b) {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
|
-
*
|
|
61
|
-
* mode ∈ off-silent(env 显式禁 → 全静默)| none
|
|
60
|
+
* 纯决策函数(单测覆盖):本次启动是否检查更新。
|
|
61
|
+
* mode ∈ off-silent(env 显式禁 → 全静默)| none(npx / 非 TTY)| run(即时异步查)
|
|
62
62
|
*/
|
|
63
63
|
export function decideUpdate({
|
|
64
64
|
env = process.env,
|
|
65
|
-
cache = {},
|
|
66
|
-
now = Date.now(),
|
|
67
65
|
isTTY = process.stdout.isTTY,
|
|
68
|
-
version = CLI_VERSION,
|
|
69
|
-
intervalMs = CHECK_INTERVAL_MS,
|
|
70
66
|
} = {}) {
|
|
71
67
|
const noAuto = env.COLLECTUI_NO_AUTO_UPDATE;
|
|
72
68
|
if (noAuto === "1" || noAuto === "true" || noAuto === "yes") {
|
|
@@ -78,14 +74,7 @@ export function decideUpdate({
|
|
|
78
74
|
if (!isTTY) {
|
|
79
75
|
return { mode: "none", reason: "non-tty" };
|
|
80
76
|
}
|
|
81
|
-
|
|
82
|
-
if (outdated) {
|
|
83
|
-
return { mode: "notify", latest: cache.latest, reason: "cached-outdated" };
|
|
84
|
-
}
|
|
85
|
-
if (now - (cache.lastCheck ?? 0) >= intervalMs) {
|
|
86
|
-
return { mode: "check", reason: "due" };
|
|
87
|
-
}
|
|
88
|
-
return { mode: "none", reason: "recent" };
|
|
77
|
+
return { mode: "run", reason: "interactive" };
|
|
89
78
|
}
|
|
90
79
|
|
|
91
80
|
/** registry latest → 版本号;失败抛错(调用方决定吞还是报)。 */
|
|
@@ -98,36 +87,56 @@ export async function fetchLatestVersion({ timeout = 8_000 } = {}) {
|
|
|
98
87
|
return v;
|
|
99
88
|
}
|
|
100
89
|
|
|
101
|
-
/** 后台异步检查:只写缓存,不装任何东西。fire-and-forget,绝不抛。 */
|
|
102
|
-
export function backgroundCheck() {
|
|
103
|
-
const started = Date.now();
|
|
104
|
-
return fetchLatestVersion()
|
|
105
|
-
.then((latest) => {
|
|
106
|
-
writeUpdateCache({ lastCheck: started, latest });
|
|
107
|
-
return latest;
|
|
108
|
-
})
|
|
109
|
-
.catch(() => undefined);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
90
|
/**
|
|
113
|
-
* 启动钩子(cli.main
|
|
114
|
-
*
|
|
91
|
+
* 启动钩子(cli.main 里调):与命令并行即时查一次 npm。
|
|
92
|
+
* - 缓存里已有新版 → 立即提醒(不用等本次查询)
|
|
93
|
+
* - 查询完成 → 写缓存;若发现有新版且还没提醒过,补一行提醒
|
|
94
|
+
* 返回 { done }:cli.main 在命令结束后最多等它 1 秒,没查完就放弃退出(绝不挂尾)。
|
|
115
95
|
*/
|
|
116
96
|
export function startBackgroundUpdate(reporter, deps = {}) {
|
|
117
97
|
const decision = decideUpdate(deps);
|
|
118
|
-
if (decision.mode
|
|
119
|
-
|
|
98
|
+
if (decision.mode !== "run") return null;
|
|
99
|
+
let notified = false;
|
|
100
|
+
const cache = readUpdateCache();
|
|
101
|
+
if (cache.latest && compareVersions(cache.latest, CLI_VERSION) > 0) {
|
|
120
102
|
reporter.info(
|
|
121
|
-
`⬆ ${NPM_PACKAGE} ${CLI_VERSION} → ${
|
|
103
|
+
`⬆ ${NPM_PACKAGE} ${CLI_VERSION} → ${cache.latest} available — run \`collectui update\` to upgrade.`
|
|
122
104
|
);
|
|
123
|
-
|
|
105
|
+
notified = true;
|
|
124
106
|
}
|
|
125
|
-
|
|
126
|
-
|
|
107
|
+
const done = fetchLatestVersion()
|
|
108
|
+
.then((latest) => {
|
|
109
|
+
writeUpdateCache({ lastCheck: Date.now(), latest });
|
|
110
|
+
if (!notified && compareVersions(latest, CLI_VERSION) > 0) {
|
|
111
|
+
reporter.info(
|
|
112
|
+
`⬆ ${NPM_PACKAGE} ${CLI_VERSION} → ${latest} available — run \`collectui update\` to upgrade.`
|
|
113
|
+
);
|
|
114
|
+
notified = true;
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
.catch(() => undefined);
|
|
118
|
+
return { done };
|
|
127
119
|
}
|
|
128
120
|
|
|
129
|
-
/** 手动更新(`collectui update
|
|
121
|
+
/** 手动更新(`collectui update`):异步 spawn + 捕获输出(不 inherit——往 clack 的
|
|
122
|
+
* frame 里插行会把边框打断;捕获后失败时调用方直接展示 stderr)。
|
|
123
|
+
* runner 可注入(测试):返回 { status, stdout, stderr }(或其 Promise)。 */
|
|
130
124
|
export function runManualUpdate({ runner } = {}) {
|
|
131
|
-
|
|
132
|
-
return
|
|
125
|
+
if (runner) return runner(["install", "-g", `${NPM_PACKAGE}@latest`]);
|
|
126
|
+
return new Promise((resolve) => {
|
|
127
|
+
const child = spawn("npm", ["install", "-g", `${NPM_PACKAGE}@latest`], {
|
|
128
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
129
|
+
shell: process.platform === "win32",
|
|
130
|
+
});
|
|
131
|
+
let out = "";
|
|
132
|
+
let err = "";
|
|
133
|
+
child.stdout?.on("data", (d) => {
|
|
134
|
+
out += d;
|
|
135
|
+
});
|
|
136
|
+
child.stderr?.on("data", (d) => {
|
|
137
|
+
err += d;
|
|
138
|
+
});
|
|
139
|
+
child.on("close", (code) => resolve({ status: code, stdout: out, stderr: err }));
|
|
140
|
+
child.on("error", (e) => resolve({ status: 1, stderr: String(e) }));
|
|
141
|
+
});
|
|
133
142
|
}
|