tokenforbes-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/bin.js +144 -43
- package/ccusage.js +12 -4
- package/daemon.js +22 -16
- package/package.json +1 -1
package/bin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
//
|
|
3
|
-
// 命令:register <昵称> | login <密钥> | report(默认)
|
|
2
|
+
// CEO Rank(tokenforbes)上报命令行。只上报用量数字和设备型号,不读、不传你的任何项目内容。
|
|
3
|
+
// 命令:link | register <昵称> | login <密钥> | report(默认) | daemon <install|status|uninstall>
|
|
4
4
|
import fs from 'node:fs';
|
|
5
5
|
import os from 'node:os';
|
|
6
6
|
import path from 'node:path';
|
|
@@ -11,29 +11,88 @@ import { daemon, ensureDaemonInstalled } from './daemon.js';
|
|
|
11
11
|
|
|
12
12
|
const CONFIG = path.join(os.homedir(), '.tokenboard.json');
|
|
13
13
|
const DEFAULT_SERVER = process.env.TB_SERVER || 'https://ceorank.cn';
|
|
14
|
+
const CMD_PLAIN = 'npx -y tokenforbes-cli@latest'; // 万能命令:没登录带你登录,登录过直接上报
|
|
15
|
+
const CMD_LINK = CMD_PLAIN + ' link'; // 中断后接着来,统一推荐这条
|
|
14
16
|
|
|
15
17
|
const loadConfig = () => { try { return JSON.parse(fs.readFileSync(CONFIG, 'utf8')); } catch { return {}; } };
|
|
16
18
|
const saveConfig = (c) => fs.writeFileSync(CONFIG, JSON.stringify(c, null, 2));
|
|
17
19
|
|
|
20
|
+
// ---- Ctrl+C 善后 ----
|
|
21
|
+
// 任何阶段按 Ctrl+C:说清进行到哪一步、怎么继续,然后干净退出(130),绝不甩堆栈。
|
|
22
|
+
// stage 跟着流程走:start(刚起步) → usage(统计用量) → approve(等浏览器批准) → upload(上传) → done。
|
|
23
|
+
let stage = 'start';
|
|
24
|
+
const STAGE_BYE = {
|
|
25
|
+
start: '刚起步,还什么都没动,一切照旧。',
|
|
26
|
+
usage: '刚才正在统计本地用量,还没统计完;没有上传任何数据,也没改动任何东西。',
|
|
27
|
+
approve: '刚才正在等你去浏览器点批准,还没上传任何数据。',
|
|
28
|
+
upload: '刚才正在上传,可能只传了一部分——不要紧,重跑一次会自动补齐,不会重复计数。',
|
|
29
|
+
done: '该办的都已经办完了,用量传上去了。',
|
|
30
|
+
};
|
|
31
|
+
function bye() {
|
|
32
|
+
console.log('\n\n好,先停在这儿。' + (STAGE_BYE[stage] || ''));
|
|
33
|
+
console.log('想继续的时候,随时跑这一条,接着帮你办完:\n');
|
|
34
|
+
console.log(' ' + CMD_LINK + '\n');
|
|
35
|
+
process.exit(130);
|
|
36
|
+
}
|
|
37
|
+
process.on('SIGINT', bye);
|
|
38
|
+
|
|
18
39
|
const [cmd, arg] = process.argv.slice(2);
|
|
19
40
|
|
|
20
|
-
|
|
21
|
-
if (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
41
|
+
try {
|
|
42
|
+
if (cmd === 'login') {
|
|
43
|
+
login(arg);
|
|
44
|
+
} else if (cmd === 'link') {
|
|
45
|
+
await link();
|
|
46
|
+
} else if (cmd === 'daemon') {
|
|
47
|
+
daemon(arg);
|
|
48
|
+
} else if (cmd === 'register') {
|
|
49
|
+
await register(arg);
|
|
50
|
+
} else if (cmd === 'report') {
|
|
51
|
+
await report();
|
|
52
|
+
} else if (!cmd) {
|
|
53
|
+
// 一条命令走完:report 内部会兜底——没密钥或密钥失效(服务器重置过)都自动带去登录再上报。
|
|
54
|
+
await report();
|
|
55
|
+
} else {
|
|
56
|
+
help();
|
|
57
|
+
}
|
|
58
|
+
} catch (e) {
|
|
59
|
+
// 统计用量时按 Ctrl+C,子进程先被打断,错误会带 interrupted 标记辗转到这里——按告别处理,不算出错。
|
|
60
|
+
if (e?.interrupted) bye();
|
|
61
|
+
if (e?.friendly) die(e.message); // 自家抛的错,文案已经写好了「出了什么事 + 下一步」
|
|
62
|
+
die('✗ 出了点意外:' + (e?.message || e) + '\n 重新跑一次通常就能好: ' + CMD_PLAIN + '\n 要是反复不行,去 https://ceorank.cn 找我们说一声,我们来修。');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function help() {
|
|
66
|
+
console.log(`
|
|
67
|
+
CEO Rank 用量上报工具(tokenforbes-cli)
|
|
68
|
+
只上报 token 用量数字和设备型号,绝不读取、不上传你的代码和对话内容。
|
|
69
|
+
|
|
70
|
+
最省事的用法(一条命令全搞定):
|
|
71
|
+
|
|
72
|
+
${CMD_PLAIN}
|
|
73
|
+
|
|
74
|
+
没登录会自动打开浏览器带你登录;登录过就直接上报。
|
|
75
|
+
|
|
76
|
+
分步命令(想单独控制时用):
|
|
77
|
+
link 浏览器一键登录 + 上报(推荐)
|
|
78
|
+
report 上报一次本地用量
|
|
79
|
+
register <昵称> 不走浏览器,直接用昵称注册
|
|
80
|
+
login <密钥> 已有密钥,保存到本机
|
|
81
|
+
daemon install 开启后台自动同步(每 30 分钟自动上报)
|
|
82
|
+
daemon status 看后台同步是否在跑
|
|
83
|
+
daemon uninstall 关闭后台自动同步
|
|
84
|
+
|
|
85
|
+
上报成功后,去 https://ceorank.cn 看排名;登录后打开「同步数据」页,能看到每台设备的上报明细。
|
|
86
|
+
`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function login(key) {
|
|
90
|
+
if (!key) {
|
|
91
|
+
die('差一个密钥,没法登录。这样用:\n ' + CMD_PLAIN + ' login <你的密钥>\n\n 手头没有密钥?直接跑 ' + CMD_PLAIN + ' 走浏览器登录,更省事。');
|
|
92
|
+
}
|
|
93
|
+
const c = loadConfig(); c.key = key; c.server = c.server || DEFAULT_SERVER; saveConfig(c);
|
|
94
|
+
console.log('✓ 密钥已保存(' + CONFIG + '),这台电脑之后免登录。');
|
|
95
|
+
console.log('接下来跑 ' + CMD_PLAIN + ' 就能把用量传上去了。');
|
|
37
96
|
}
|
|
38
97
|
|
|
39
98
|
// 设备授权登录:先弹浏览器让你批准 → 趁这工夫后台抓用量 → 批准一到立刻上传。
|
|
@@ -45,29 +104,40 @@ async function link(preDays) {
|
|
|
45
104
|
const s = await post(server + '/api/device/start', { device });
|
|
46
105
|
const url = `${server}/link.html?code=${s.user_code}`;
|
|
47
106
|
// 第一步就把浏览器弹出去,别让用户对着黑终端干等。
|
|
48
|
-
console.log('\n①
|
|
107
|
+
console.log('\n① 去浏览器批准登录');
|
|
108
|
+
console.log(' 浏览器马上会自己弹出来,填个昵称、点「注册并批准」就行。');
|
|
109
|
+
console.log(' 没弹出来?手动打开这条链接:');
|
|
49
110
|
console.log(' → ' + url);
|
|
50
|
-
console.log('
|
|
111
|
+
console.log(' 验证码:' + s.user_code + '(和网页上显示的对上号,再点批准)');
|
|
51
112
|
openBrowser(url);
|
|
52
113
|
|
|
53
114
|
// 趁你在浏览器填昵称、点批准的工夫,后台把本地用量先抓好,批准一到就能立刻上传。
|
|
54
115
|
let days = preDays;
|
|
55
116
|
if (!days) {
|
|
56
|
-
|
|
117
|
+
stage = 'usage';
|
|
118
|
+
console.log('\n② 趁这工夫,统计这台电脑的 AI 用量');
|
|
119
|
+
console.log(' 正在读取 Claude Code / Codex 的 token 用量,第一次跑要先下载统计工具,可能要几十秒…');
|
|
57
120
|
days = readUsage();
|
|
58
|
-
|
|
121
|
+
if (days.length) {
|
|
122
|
+
console.log(` ✓ 统计好了,共 ${days.length} 天的用量,你在浏览器一批准就立刻上传。`);
|
|
123
|
+
} else {
|
|
124
|
+
console.log(' 这台电脑暂时没读到用量——可能还没用 Claude Code / Codex 干过活,不碍事。');
|
|
125
|
+
console.log(' 先把账号批准了;之后有了用量,重跑一次这条命令就会自动传上来。');
|
|
126
|
+
}
|
|
59
127
|
} else {
|
|
60
|
-
console.log('\n②
|
|
128
|
+
console.log('\n② 本地用量刚才已经统计好,你一批准就立刻上传。');
|
|
61
129
|
}
|
|
62
130
|
|
|
63
|
-
|
|
131
|
+
stage = 'approve';
|
|
132
|
+
console.log('\n③ 就差你在浏览器里点批准了');
|
|
133
|
+
process.stdout.write(' 等你确认中,点完这边马上继续(不想等就按 Ctrl+C,随时能回来)');
|
|
64
134
|
const until = Date.now() + s.expires_in * 1000;
|
|
65
135
|
while (Date.now() < until) {
|
|
66
136
|
await sleep(s.interval * 1000);
|
|
67
137
|
const p = await post(server + '/api/device/poll', { device_code: s.device_code });
|
|
68
138
|
if (p.status === 'approved') {
|
|
69
139
|
c.key = p.key; c.server = server; saveConfig(c);
|
|
70
|
-
console.log('\n✓
|
|
140
|
+
console.log('\n ✓ 批准成功!密钥已存好(' + CONFIG + '),这台电脑之后免登录。');
|
|
71
141
|
await uploadDays(days, { server, key: p.key, device });
|
|
72
142
|
ensureDaemonInstalled();
|
|
73
143
|
return;
|
|
@@ -75,7 +145,7 @@ async function link(preDays) {
|
|
|
75
145
|
if (p.status === 'expired') break;
|
|
76
146
|
process.stdout.write('.');
|
|
77
147
|
}
|
|
78
|
-
die('
|
|
148
|
+
die('✗ 这次的验证码过期了——等得有点久,安全起见每个码只有几分钟有效。\n 没关系,重跑一遍就有新码,一分钟搞定:\n ' + CMD_LINK);
|
|
79
149
|
}
|
|
80
150
|
|
|
81
151
|
function sleep(ms) { return new Promise((r) => setTimeout(r, ms)); }
|
|
@@ -88,14 +158,17 @@ function openBrowser(url) {
|
|
|
88
158
|
}
|
|
89
159
|
|
|
90
160
|
async function register(nickname) {
|
|
91
|
-
if (!nickname)
|
|
161
|
+
if (!nickname) {
|
|
162
|
+
die('差一个昵称,没法注册。这样用:\n ' + CMD_PLAIN + ' register <你的昵称>\n\n 更省事的办法:直接跑 ' + CMD_PLAIN + ' ,在浏览器里填昵称就行。');
|
|
163
|
+
}
|
|
92
164
|
const c = loadConfig();
|
|
93
165
|
const server = c.server || DEFAULT_SERVER;
|
|
94
166
|
const device = detectDevice();
|
|
95
167
|
const j = await post(server + '/api/register', { nickname, device });
|
|
96
168
|
c.key = j.key; c.server = server; saveConfig(c);
|
|
97
|
-
console.log(
|
|
98
|
-
|
|
169
|
+
console.log(`✓ 注册成功,欢迎入榜,「${j.nickname}」!密钥已存好(${CONFIG}),这台电脑之后免登录。`);
|
|
170
|
+
stage = 'usage';
|
|
171
|
+
console.log('接着统计这台电脑的 token 用量,统计完马上传上去,第一次要下载统计工具,可能要几十秒…');
|
|
99
172
|
await uploadDays(readUsage(), { server, key: j.key, device }); // 注册完顺手上报,别让用户再跑一条
|
|
100
173
|
ensureDaemonInstalled();
|
|
101
174
|
}
|
|
@@ -104,8 +177,11 @@ async function report({ allowRelink = true } = {}) {
|
|
|
104
177
|
const c = loadConfig();
|
|
105
178
|
// 没密钥:直接带去登录(link 会先弹浏览器,再后台抓用量),不让用户对着报错发愣。
|
|
106
179
|
if (!c.key) {
|
|
107
|
-
if (allowRelink)
|
|
108
|
-
|
|
180
|
+
if (allowRelink) {
|
|
181
|
+
console.log('这台电脑还没登录过,先带你走一遍浏览器登录,一分钟搞定。');
|
|
182
|
+
return link();
|
|
183
|
+
}
|
|
184
|
+
die('这台电脑还没登录,没法上报。先跑 ' + CMD_PLAIN + ' 用浏览器一键登录(也可以 register <昵称> 或 login <密钥>)。');
|
|
109
185
|
}
|
|
110
186
|
const server = c.server || DEFAULT_SERVER;
|
|
111
187
|
const device = detectDevice();
|
|
@@ -114,33 +190,58 @@ async function report({ allowRelink = true } = {}) {
|
|
|
114
190
|
if (!probe.ok) {
|
|
115
191
|
const authFailed = probe.status === 401 || /密钥/.test(probe.json?.error || '');
|
|
116
192
|
if (authFailed && allowRelink) {
|
|
117
|
-
console.log('
|
|
193
|
+
console.log('本机存的密钥不管用了(可能在网站上重置过密钥)。');
|
|
194
|
+
console.log('不用慌,你的历史数据都在。这就打开浏览器带你重新登录…');
|
|
118
195
|
delete c.key; saveConfig(c);
|
|
119
196
|
return link(); // 密钥无效,还没抓用量;交给 link 先弹浏览器再后台抓
|
|
120
197
|
}
|
|
121
|
-
die(
|
|
198
|
+
die(sendFailText(probe));
|
|
122
199
|
}
|
|
123
200
|
// 密钥有效:抓用量并上传。
|
|
124
|
-
|
|
201
|
+
stage = 'usage';
|
|
202
|
+
console.log('正在统计这台电脑的 token 用量(Claude Code / Codex),可能要几十秒…');
|
|
125
203
|
await uploadDays(readUsage(), { server, key: c.key, device });
|
|
126
204
|
ensureDaemonInstalled();
|
|
127
205
|
}
|
|
128
206
|
|
|
129
207
|
// 把抓好的用量传上去。抽出来给 report / link / register 共用,避免各写一遍、避免重复抓。
|
|
130
208
|
async function uploadDays(days, { server, key, device }) {
|
|
131
|
-
if (!days || !days.length)
|
|
209
|
+
if (!days || !days.length) {
|
|
210
|
+
console.log('\n这台电脑上没读到 token 用量,这次就先不上传了。');
|
|
211
|
+
console.log('多半是这两种情况,都不用担心:');
|
|
212
|
+
console.log(' · 这台电脑还没用 Claude Code / Codex 干过活 —— 用起来之后重跑一次就有了');
|
|
213
|
+
console.log(' · 你平时在另一台电脑上干活 —— 去那台电脑跑同样这条命令');
|
|
214
|
+
console.log('账号本身没任何问题。之后随时跑 ' + CMD_PLAIN + ' 都能补传。');
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
stage = 'upload';
|
|
132
218
|
const total = days.reduce((s, d) => s + d.input + d.output + (d.cacheCreation || 0) + (d.cacheRead || 0), 0);
|
|
133
|
-
console.log(
|
|
134
|
-
console.log(`共 ${days.length}
|
|
219
|
+
console.log(`\n设备:${device}`);
|
|
220
|
+
console.log(`共 ${days.length} 天、约 ${Math.round(total / 1e4).toLocaleString()} 万 token,正在上传(只传数字,不传内容)…`);
|
|
135
221
|
const res = await postRaw(server + '/api/report', { key, device, days });
|
|
136
|
-
if (res.ok)
|
|
137
|
-
|
|
222
|
+
if (!res.ok) die(sendFailText(res));
|
|
223
|
+
stage = 'done';
|
|
224
|
+
console.log(`✓ 上报成功!服务器已记下 ${res.json.days} 天的用量。\n`);
|
|
225
|
+
console.log('去看看成果:');
|
|
226
|
+
console.log(` · 打开 ${server} ,在排行榜上找你的名次`);
|
|
227
|
+
console.log(' · 登录后进「同步数据」页,能看到这台设备每天的上报明细');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 统一的「没传上去」文案:说清原因 + 数据没丢 + 下一步。
|
|
231
|
+
function sendFailText(res) {
|
|
232
|
+
const why = res.netError || res.json?.error || ('服务器回了个 ' + res.status + ',没说原因');
|
|
233
|
+
return '✗ 这次没传上去:' + why + '\n' +
|
|
234
|
+
' 你的用量都存在本机,一条都不会丢。\n' +
|
|
235
|
+
' 等网络稳定了重跑一次就行: ' + CMD_PLAIN;
|
|
138
236
|
}
|
|
139
237
|
|
|
140
238
|
// 会抛错就 die 的版本:给登录/注册这类「失败就该停」的调用用。
|
|
141
239
|
async function post(url, body) {
|
|
142
240
|
const res = await postRaw(url, body);
|
|
143
|
-
if (!res.ok)
|
|
241
|
+
if (!res.ok) {
|
|
242
|
+
die('✗ 没连上服务器,这一步没办成:' + (res.netError || res.json?.error || ('服务器回了个 ' + res.status)) + '\n' +
|
|
243
|
+
' 你这边什么都没丢。检查下网络,稍后重跑: ' + CMD_PLAIN);
|
|
244
|
+
}
|
|
144
245
|
return res.json;
|
|
145
246
|
}
|
|
146
247
|
|
|
@@ -150,10 +251,10 @@ async function postRaw(url, body) {
|
|
|
150
251
|
try {
|
|
151
252
|
res = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
|
|
152
253
|
} catch (e) {
|
|
153
|
-
return { ok: false, netError:
|
|
254
|
+
return { ok: false, netError: `连不上服务器(${e.message}),可能是断网、开了代理,或服务器暂时没响应` };
|
|
154
255
|
}
|
|
155
256
|
const json = await res.json().catch(() => ({}));
|
|
156
257
|
return { ok: res.ok, status: res.status, json };
|
|
157
258
|
}
|
|
158
259
|
|
|
159
|
-
function die(msg) { console.error(msg); process.exit(1); }
|
|
260
|
+
function die(msg) { console.error('\n' + msg); process.exit(1); }
|
package/ccusage.js
CHANGED
|
@@ -4,6 +4,8 @@ import { execSync } from 'node:child_process';
|
|
|
4
4
|
|
|
5
5
|
const num = (x) => Math.max(0, Math.round(Number(x) || 0));
|
|
6
6
|
const money = (x) => Math.max(0, Number(x) || 0); // 成本是美元小数,别取整
|
|
7
|
+
// friendly 标记 = 文案已经按「出了什么事 + 下一步」写好,上层直接展示,别再包一层「出了点意外」。
|
|
8
|
+
const friendly = (msg) => { const e = new Error(msg); e.friendly = true; return e; };
|
|
7
9
|
|
|
8
10
|
export function readUsage() {
|
|
9
11
|
let out;
|
|
@@ -11,17 +13,23 @@ export function readUsage() {
|
|
|
11
13
|
out = execSync('npx ccusage@latest daily --breakdown --json', {
|
|
12
14
|
encoding: 'utf8', timeout: 120000, maxBuffer: 64 * 1024 * 1024,
|
|
13
15
|
});
|
|
14
|
-
} catch {
|
|
15
|
-
|
|
16
|
+
} catch (e) {
|
|
17
|
+
// 用户按 Ctrl+C 时,这个子进程会先被打断——这不是故障,标记出来让上层好聚好散。
|
|
18
|
+
if (e && (e.signal === 'SIGINT' || e.signal === 'SIGTERM' || e.status === 130)) {
|
|
19
|
+
const stop = new Error('interrupted');
|
|
20
|
+
stop.interrupted = true;
|
|
21
|
+
throw stop;
|
|
22
|
+
}
|
|
23
|
+
throw friendly('✗ 统计工具(ccusage)没跑起来,这次没读到用量。\n 多半是网络不稳——它第一次要联网下载。检查下网络,重跑一次就行;\n 要是反复失败,单独跑一下 npx ccusage@latest daily 看看它报什么。');
|
|
16
24
|
}
|
|
17
25
|
let data;
|
|
18
26
|
try {
|
|
19
27
|
data = JSON.parse(out);
|
|
20
28
|
} catch {
|
|
21
|
-
throw
|
|
29
|
+
throw friendly('✗ 统计工具(ccusage)的输出这版看不懂了,可能它刚改版。\n 过几个小时再跑一次试试;一直不行就是我们这边要修,去 https://ceorank.cn 说一声。');
|
|
22
30
|
}
|
|
23
31
|
if (!data || !Array.isArray(data.daily)) {
|
|
24
|
-
throw
|
|
32
|
+
throw friendly('✗ 统计工具(ccusage)的输出里没找到每日数据,格式可能变了。\n 过几个小时再跑一次试试;一直不行就是我们这边要修,去 https://ceorank.cn 说一声。');
|
|
25
33
|
}
|
|
26
34
|
return data.daily
|
|
27
35
|
.map((d) => {
|
package/daemon.js
CHANGED
|
@@ -44,7 +44,7 @@ function supported() {
|
|
|
44
44
|
|
|
45
45
|
function ensureSupported() {
|
|
46
46
|
if (!supported()) {
|
|
47
|
-
console.error('
|
|
47
|
+
console.error('后台自动同步目前只支持 macOS / Windows,你这个系统暂时开不了。\n不过可以自己挂个定时任务,每半小时跑一次: npx -y tokenforbes-cli@latest report');
|
|
48
48
|
process.exit(1);
|
|
49
49
|
}
|
|
50
50
|
}
|
|
@@ -111,34 +111,36 @@ function installWindows() {
|
|
|
111
111
|
function install() {
|
|
112
112
|
ensureSupported();
|
|
113
113
|
if (!requireKey()) {
|
|
114
|
-
console.error('
|
|
114
|
+
console.error('这台电脑还没登录,先不开后台同步——开了也只会每半小时报一次「未登录」。\n先跑 npx -y tokenforbes-cli@latest 登录,登录成功会自动帮你开启。');
|
|
115
115
|
process.exit(1);
|
|
116
116
|
}
|
|
117
117
|
if (process.platform === 'darwin') installMac();
|
|
118
118
|
else installWindows();
|
|
119
|
-
console.log('✓
|
|
119
|
+
console.log('✓ 后台自动同步已开启:每 30 分钟自动上报一次,之后不用你管。');
|
|
120
120
|
if (process.platform === 'darwin') {
|
|
121
121
|
console.log(' 任务文件: ' + PLIST);
|
|
122
122
|
console.log(' 日志: ' + LOG);
|
|
123
123
|
}
|
|
124
|
-
console.log('
|
|
125
|
-
console.log('
|
|
124
|
+
console.log(' 看状态: npx -y tokenforbes-cli@latest daemon status');
|
|
125
|
+
console.log(' 想关掉: npx -y tokenforbes-cli@latest daemon uninstall');
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
function statusMac() {
|
|
129
129
|
if (!fs.existsSync(PLIST)) return false;
|
|
130
130
|
let loaded = false;
|
|
131
131
|
try { loaded = execFileSync('launchctl', ['list'], { encoding: 'utf8' }).includes(LABEL); } catch {}
|
|
132
|
-
console.log(loaded
|
|
132
|
+
console.log(loaded
|
|
133
|
+
? '✓ 后台自动同步在运行,每 30 分钟自动上报一次,不用你管。'
|
|
134
|
+
: '装过,但目前没在运行。重跑一次 npx -y tokenforbes-cli@latest daemon install 就能拉起来。');
|
|
133
135
|
console.log(' 任务文件: ' + PLIST);
|
|
134
|
-
console.log(' 日志: ' + LOG + '
|
|
136
|
+
console.log(' 日志: ' + LOG + '(想看最近几次上报有没有出错就翻它)');
|
|
135
137
|
return true;
|
|
136
138
|
}
|
|
137
139
|
|
|
138
140
|
function statusWindows() {
|
|
139
141
|
try {
|
|
140
142
|
execFileSync('schtasks', ['/query', '/tn', LABEL], { stdio: 'ignore' });
|
|
141
|
-
console.log('✓
|
|
143
|
+
console.log('✓ 后台自动同步在运行,Windows 每 30 分钟自动上报一次,不用你管。');
|
|
142
144
|
return true;
|
|
143
145
|
} catch {
|
|
144
146
|
return false;
|
|
@@ -163,23 +165,23 @@ function needsInstall() {
|
|
|
163
165
|
function status() {
|
|
164
166
|
ensureSupported();
|
|
165
167
|
const ok = process.platform === 'darwin' ? statusMac() : statusWindows();
|
|
166
|
-
if (!ok) console.log('
|
|
168
|
+
if (!ok) console.log('后台自动同步还没开启。想开就跑: npx -y tokenforbes-cli@latest daemon install');
|
|
167
169
|
}
|
|
168
170
|
|
|
169
171
|
function uninstallMac() {
|
|
170
172
|
cleanupLegacyMac();
|
|
171
|
-
if (!fs.existsSync(PLIST)) return console.log('
|
|
173
|
+
if (!fs.existsSync(PLIST)) return console.log('后台自动同步本来就没开,不用关。');
|
|
172
174
|
try { execFileSync('launchctl', ['unload', PLIST], { stdio: 'ignore' }); } catch {}
|
|
173
175
|
fs.rmSync(PLIST, { force: true });
|
|
174
|
-
console.log('✓
|
|
176
|
+
console.log('✓ 已关闭后台自动同步,任务文件也删干净了。想再开: npx -y tokenforbes-cli@latest daemon install');
|
|
175
177
|
}
|
|
176
178
|
|
|
177
179
|
function uninstallWindows() {
|
|
178
180
|
try {
|
|
179
181
|
execFileSync('schtasks', ['/delete', '/tn', LABEL, '/f'], { stdio: 'ignore' });
|
|
180
|
-
console.log('✓
|
|
182
|
+
console.log('✓ 已关闭后台自动同步。想再开: npx -y tokenforbes-cli@latest daemon install');
|
|
181
183
|
} catch {
|
|
182
|
-
console.log('
|
|
184
|
+
console.log('后台自动同步本来就没开,不用关。');
|
|
183
185
|
}
|
|
184
186
|
fs.rmSync(VBS, { force: true });
|
|
185
187
|
}
|
|
@@ -199,9 +201,10 @@ export function ensureDaemonInstalled() {
|
|
|
199
201
|
if (!requireKey()) return;
|
|
200
202
|
if (process.platform === 'darwin') installMac();
|
|
201
203
|
else installWindows();
|
|
202
|
-
console.log('
|
|
204
|
+
console.log('顺手帮你开好了后台自动同步:之后每 30 分钟自动上报一次,不用再手动跑命令。');
|
|
205
|
+
console.log('(想关掉随时跑: npx -y tokenforbes-cli@latest daemon uninstall )');
|
|
203
206
|
} catch (e) {
|
|
204
|
-
console.log('
|
|
207
|
+
console.log('后台自动同步这次没开成(不影响刚才的上报)。想开的话稍后手动跑: npx -y tokenforbes-cli@latest daemon install');
|
|
205
208
|
}
|
|
206
209
|
}
|
|
207
210
|
|
|
@@ -209,5 +212,8 @@ export function daemon(sub) {
|
|
|
209
212
|
if (sub === 'install') return install();
|
|
210
213
|
if (sub === 'status') return status();
|
|
211
214
|
if (sub === 'uninstall' || sub === 'remove') return uninstall();
|
|
212
|
-
console.log('
|
|
215
|
+
console.log('后台自动同步的开关,这样用:');
|
|
216
|
+
console.log(' npx -y tokenforbes-cli@latest daemon install 开启(每 30 分钟自动上报)');
|
|
217
|
+
console.log(' npx -y tokenforbes-cli@latest daemon status 看状态');
|
|
218
|
+
console.log(' npx -y tokenforbes-cli@latest daemon uninstall 关闭');
|
|
213
219
|
}
|