@manturhub/cli 0.1.2 → 0.1.3
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/bin/cli.js +2 -0
- package/lib/setup.js +2 -0
- package/lib/update-check.js +51 -0
- package/lib/update-fetch.js +38 -0
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -3,6 +3,7 @@ import { saveConfig, loadConfig } from "../lib/config.js";
|
|
|
3
3
|
import { apiFetch } from "../lib/api.js";
|
|
4
4
|
import { runMcpBridge } from "../lib/mcp.js";
|
|
5
5
|
import { runInit, runMcpInstall } from "../lib/setup.js";
|
|
6
|
+
import { maybeNotifyUpdate } from "../lib/update-check.js";
|
|
6
7
|
import { readFileSync } from "node:fs";
|
|
7
8
|
import { fileURLToPath } from "node:url";
|
|
8
9
|
import { dirname, join } from "node:path";
|
|
@@ -50,6 +51,7 @@ const HELP = `manturhub — ManturHub 算子广场 CLI v${VERSION}
|
|
|
50
51
|
`;
|
|
51
52
|
|
|
52
53
|
async function main() {
|
|
54
|
+
maybeNotifyUpdate(VERSION);
|
|
53
55
|
switch (cmd) {
|
|
54
56
|
case "login": {
|
|
55
57
|
const key = getFlag("key");
|
package/lib/setup.js
CHANGED
|
@@ -22,6 +22,8 @@ function guideBlock() {
|
|
|
22
22
|
- 每个算子的入参字段见 ${HUB}/marketplace/<算子ID>
|
|
23
23
|
|
|
24
24
|
注意:视频 / 图像等**异步算子**返回 jobId,需按算子文档轮询取结果;这类场景更建议用 MCP(见 \`manturhub mcp-install\`,工具更完整、含任务状态查询)。
|
|
25
|
+
|
|
26
|
+
若运行 \`manturhub\` 时 stderr 出现「有新版」提示,请主动告知用户可运行 \`npm i -g @manturhub/cli@latest\` 更新。
|
|
25
27
|
${MARK_END}`;
|
|
26
28
|
}
|
|
27
29
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
2
|
+
import { join, dirname } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
const CACHE = join(homedir(), ".manturhub", "update-check.json");
|
|
8
|
+
const ONE_DAY = 24 * 60 * 60 * 1000;
|
|
9
|
+
|
|
10
|
+
function isNewer(latest, current) {
|
|
11
|
+
const a = String(latest).split(".").map((n) => parseInt(n, 10) || 0);
|
|
12
|
+
const b = String(current).split(".").map((n) => parseInt(n, 10) || 0);
|
|
13
|
+
for (let i = 0; i < 3; i++) {
|
|
14
|
+
if (a[i] > b[i]) return true;
|
|
15
|
+
if (a[i] < b[i]) return false;
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 启动时调用:读本地缓存→落后则 stderr 提示;缓存超过一天→后台异步刷新(不阻塞本次)。
|
|
21
|
+
// 永不抛错、永不写 stdout —— mcp 模式 stdout 是 JSON-RPC 流,绝不能污染。
|
|
22
|
+
export function maybeNotifyUpdate(currentVersion) {
|
|
23
|
+
try {
|
|
24
|
+
let cache = {};
|
|
25
|
+
if (existsSync(CACHE)) {
|
|
26
|
+
try {
|
|
27
|
+
cache = JSON.parse(readFileSync(CACHE, "utf8"));
|
|
28
|
+
} catch {
|
|
29
|
+
cache = {};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (cache.latest && isNewer(cache.latest, currentVersion)) {
|
|
33
|
+
process.stderr.write(
|
|
34
|
+
`\n⚠ manturhub 有新版 ${cache.latest}(当前 ${currentVersion})。` +
|
|
35
|
+
`\n 更新: npm i -g @manturhub/cli@latest\n\n`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
if (Date.now() - (cache.checkedAt || 0) > ONE_DAY) {
|
|
39
|
+
// 短命 CLI 进程里直接 fetch 会随进程退出被中断,改 spawn 一个 detached 子进程
|
|
40
|
+
// 去查 npm 并写缓存;父进程不等它(unref),下次启动读到结果再提示。
|
|
41
|
+
const script = join(dirname(fileURLToPath(import.meta.url)), "update-fetch.js");
|
|
42
|
+
const child = spawn(process.execPath, [script], {
|
|
43
|
+
detached: true,
|
|
44
|
+
stdio: "ignore",
|
|
45
|
+
});
|
|
46
|
+
child.unref();
|
|
47
|
+
}
|
|
48
|
+
} catch {
|
|
49
|
+
/* 检查更新绝不影响主命令 */
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// 后台短命子进程:查 npm latest 写缓存。由 update-check.js detached spawn,
|
|
2
|
+
// 独立于主命令运行,任何失败都静默(更新检查永不打扰用户)。
|
|
3
|
+
import { writeFileSync, readFileSync, mkdirSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
|
|
7
|
+
const DIR = join(homedir(), ".manturhub");
|
|
8
|
+
const CACHE = join(DIR, "update-check.json");
|
|
9
|
+
|
|
10
|
+
function readPrev() {
|
|
11
|
+
try {
|
|
12
|
+
return JSON.parse(readFileSync(CACHE, "utf8"));
|
|
13
|
+
} catch {
|
|
14
|
+
return {};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const res = await fetch("https://registry.npmjs.org/@manturhub/cli/latest", {
|
|
20
|
+
signal: AbortSignal.timeout(8000),
|
|
21
|
+
headers: { Accept: "application/json" },
|
|
22
|
+
});
|
|
23
|
+
mkdirSync(DIR, { recursive: true });
|
|
24
|
+
if (res.ok) {
|
|
25
|
+
const j = await res.json();
|
|
26
|
+
writeFileSync(CACHE, JSON.stringify({ latest: j.version, checkedAt: Date.now() }));
|
|
27
|
+
} else {
|
|
28
|
+
writeFileSync(CACHE, JSON.stringify({ ...readPrev(), checkedAt: Date.now() }));
|
|
29
|
+
}
|
|
30
|
+
} catch {
|
|
31
|
+
// 失败也记下时间戳(保留已知 latest),避免每次启动都重试,一天后再查。
|
|
32
|
+
try {
|
|
33
|
+
mkdirSync(DIR, { recursive: true });
|
|
34
|
+
writeFileSync(CACHE, JSON.stringify({ ...readPrev(), checkedAt: Date.now() }));
|
|
35
|
+
} catch {
|
|
36
|
+
/* ignore */
|
|
37
|
+
}
|
|
38
|
+
}
|