@guanzhu.me/xbot-cli 0.7.2 → 0.7.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/xbot-cli.cjs CHANGED
@@ -2,22 +2,127 @@
2
2
  'use strict';
3
3
 
4
4
  const { spawnSync } = require('node:child_process');
5
- const { join } = require('node:path');
5
+ const { dirname, join } = require('node:path');
6
+ const { tmpdir } = require('node:os');
7
+ const { mkdirSync, readFileSync, writeFileSync } = require('node:fs');
8
+ const pkg = require('../package.json');
6
9
 
7
- if (process.platform !== 'win32' || process.arch !== 'x64') {
8
- process.stderr.write('xbot-cli currently supports Windows x64 with ShadowBot installed.\n');
9
- process.exit(1);
10
+ const UPDATE_TTL_MS = 24 * 60 * 60 * 1000;
11
+ const UPDATE_FAILURE_TTL_MS = 60 * 60 * 1000;
12
+ const UPDATE_TIMEOUT_MS = 1000;
13
+
14
+ // 数字段比较,忽略预发布后缀;返回正数表示 a 比 b 新。
15
+ function compareVersions(a, b) {
16
+ const pick = (v) => String(v).split('-')[0].split('.').map((n) => parseInt(n, 10) || 0);
17
+ const pa = pick(a);
18
+ const pb = pick(b);
19
+ for (let i = 0; i < 3; i += 1) {
20
+ if (pa[i] !== pb[i]) return (pa[i] || 0) - (pb[i] || 0);
21
+ }
22
+ return 0;
23
+ }
24
+
25
+ function notifierEnabled() {
26
+ if (process.env.XBOT_NO_UPDATE_NOTIFIER) return false;
27
+ if (process.env.XBOT_UPDATE_NOTIFIER === '1') return true;
28
+ if (process.env.CI) return false;
29
+ return Boolean(process.stderr.isTTY);
30
+ }
31
+
32
+ function cacheFile() {
33
+ const dir = process.env.XBOT_UPDATE_CACHE_DIR
34
+ || join(process.env.LOCALAPPDATA || tmpdir(), 'xbot-cli');
35
+ return join(dir, 'update-check.json');
36
+ }
37
+
38
+ function readCache() {
39
+ try {
40
+ return JSON.parse(readFileSync(cacheFile(), 'utf8'));
41
+ } catch {
42
+ return {};
43
+ }
44
+ }
45
+
46
+ function writeCache(cache) {
47
+ try {
48
+ mkdirSync(dirname(cacheFile()), { recursive: true });
49
+ writeFileSync(cacheFile(), JSON.stringify(cache));
50
+ } catch {
51
+ // 缓存写失败不影响 CLI 本身。
52
+ }
53
+ }
54
+
55
+ async function fetchLatest() {
56
+ const base = (process.env.XBOT_UPDATE_REGISTRY || 'https://registry.npmjs.org').replace(/\/+$/, '');
57
+ const name = pkg.name.startsWith('@') ? `@${pkg.name.slice(1).replace('/', '%2F')}` : pkg.name;
58
+ const response = await fetch(`${base}/${name}/latest`, {
59
+ signal: AbortSignal.timeout(UPDATE_TIMEOUT_MS),
60
+ });
61
+ if (!response.ok) throw new Error(`registry ${response.status}`);
62
+ const body = await response.json();
63
+ if (!body || typeof body.version !== 'string') throw new Error('registry bad payload');
64
+ return body.version;
10
65
  }
11
66
 
12
- // Preserve stdin, terminal detection, output streams, cwd and argument boundaries.
13
- const executable = join(__dirname, '..', 'native', 'win32-x64', 'xbot-cli.exe');
14
- const result = spawnSync(executable, process.argv.slice(2), {
15
- stdio: 'inherit',
16
- shell: false,
17
- windowsHide: true,
18
- });
19
- if (result.error) {
20
- process.stderr.write(`Unable to start xbot-cli (${result.error.code}). Reinstall the npm package.\n`);
21
- process.exit(1);
67
+ // 命中缓存就直接用;过期则联网刷新,失败退避 1 小时,任何失败都不抛错。
68
+ async function resolveLatest(cache, now) {
69
+ if (cache.expiresAt && cache.expiresAt > now && cache.latest) return cache.latest;
70
+ try {
71
+ cache.latest = await fetchLatest();
72
+ cache.expiresAt = now + UPDATE_TTL_MS;
73
+ } catch {
74
+ if (!cache.latest) cache.expiresAt = now + UPDATE_FAILURE_TTL_MS;
75
+ }
76
+ writeCache(cache);
77
+ return cache.latest;
22
78
  }
23
- process.exit(result.status ?? (result.signal === 'SIGINT' ? 130 : 1));
79
+
80
+ function formatNotice(latest) {
81
+ const text = `新版本 xbot-cli ${latest} 可用(当前 ${pkg.version})。运行 npm i -g ${pkg.name} 更新。`;
82
+ if (process.stderr.isTTY) return `\u001b[33m${text}\u001b[0m\n`;
83
+ return `${text}\n`;
84
+ }
85
+
86
+ // 返回要写到 stderr 的提醒文本;不需要提醒时返回空串。
87
+ async function updateNotice() {
88
+ if (!notifierEnabled()) return '';
89
+ const now = Date.now();
90
+ const cache = readCache();
91
+ const latest = await resolveLatest(cache, now);
92
+ if (!latest || compareVersions(latest, pkg.version) <= 0) return '';
93
+ if (cache.notifiedFor === latest) return '';
94
+ cache.notifiedFor = latest;
95
+ writeCache(cache);
96
+ return formatNotice(latest);
97
+ }
98
+
99
+ async function main() {
100
+ if (process.platform !== 'win32' || process.arch !== 'x64') {
101
+ process.stderr.write('xbot-cli currently supports Windows x64 with ShadowBot installed.\n');
102
+ process.exit(1);
103
+ }
104
+
105
+ // 与原生进程并行发起,最多拖慢退出约 1 秒,失败静默。
106
+ const noticePromise = updateNotice().catch(() => '');
107
+
108
+ // Preserve stdin, terminal detection, output streams, cwd and argument boundaries.
109
+ const executable = join(__dirname, '..', 'native', 'win32-x64', 'xbot-cli.exe');
110
+ const result = spawnSync(executable, process.argv.slice(2), {
111
+ stdio: 'inherit',
112
+ shell: false,
113
+ windowsHide: true,
114
+ });
115
+ if (result.error) {
116
+ process.stderr.write(`Unable to start xbot-cli (${result.error.code}). Reinstall the npm package.\n`);
117
+ process.exit(1);
118
+ }
119
+ const notice = await noticePromise;
120
+ if (notice) process.stderr.write(notice);
121
+ process.exit(result.status ?? (result.signal === 'SIGINT' ? 130 : 1));
122
+ }
123
+
124
+ if (require.main === module) {
125
+ main();
126
+ }
127
+
128
+ module.exports = { compareVersions, notifierEnabled, updateNotice, formatNotice };
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guanzhu.me/xbot-cli",
3
- "version": "0.7.2",
3
+ "version": "0.7.3",
4
4
  "description": "Agent-friendly CLI for ShadowBot RPA applications, flows, tasks and browser automation",
5
5
  "license": "MIT",
6
6
  "author": "wn0x00",