tokenforbes-cli 0.1.3 → 0.1.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/bin.js +47 -29
- package/package.json +1 -1
package/bin.js
CHANGED
|
@@ -36,35 +36,46 @@ if (cmd === 'login') {
|
|
|
36
36
|
console.log('命令: link | register <昵称> | login <密钥> | report | daemon <install|status|uninstall>');
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
//
|
|
40
|
-
|
|
39
|
+
// 设备授权登录:先弹浏览器让你批准 → 趁这工夫后台抓用量 → 批准一到立刻上传。
|
|
40
|
+
// preDays: 若上层已抓好用量(比如 report 探到密钥失效),直接传进来,不再重抓。
|
|
41
|
+
async function link(preDays) {
|
|
41
42
|
const c = loadConfig();
|
|
42
43
|
const server = c.server || DEFAULT_SERVER;
|
|
43
44
|
const device = detectDevice();
|
|
44
45
|
const s = await post(server + '/api/device/start', { device });
|
|
45
46
|
const url = `${server}/link.html?code=${s.user_code}`;
|
|
46
|
-
|
|
47
|
-
console.log('
|
|
48
|
-
console.log('
|
|
47
|
+
// 第一步就把浏览器弹出去,别让用户对着黑终端干等。
|
|
48
|
+
console.log('\n① 浏览器这就打开,填个昵称、点「注册并批准」就行。没弹出来就手动复制这条链接:');
|
|
49
|
+
console.log(' → ' + url);
|
|
50
|
+
console.log(' 验证码: ' + s.user_code + '(跟网页上那串核对一致再批准)');
|
|
49
51
|
openBrowser(url);
|
|
50
52
|
|
|
51
|
-
|
|
53
|
+
// 趁你在浏览器填昵称、点批准的工夫,后台把本地用量先抓好,批准一到就能立刻上传。
|
|
54
|
+
let days = preDays;
|
|
55
|
+
if (!days) {
|
|
56
|
+
process.stdout.write('\n② 正在后台读取本地用量(ccusage)…');
|
|
57
|
+
days = readUsage();
|
|
58
|
+
console.log(days.length ? ` 已抓好 ${days.length} 天,就等你在浏览器批准。` : ' 没读到用量。');
|
|
59
|
+
} else {
|
|
60
|
+
console.log('\n② 本地用量已经抓好,就等你在浏览器批准。');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
process.stdout.write(' 等你点确认…');
|
|
52
64
|
const until = Date.now() + s.expires_in * 1000;
|
|
53
65
|
while (Date.now() < until) {
|
|
54
66
|
await sleep(s.interval * 1000);
|
|
55
67
|
const p = await post(server + '/api/device/poll', { device_code: s.device_code });
|
|
56
68
|
if (p.status === 'approved') {
|
|
57
69
|
c.key = p.key; c.server = server; saveConfig(c);
|
|
58
|
-
console.log('\n✓
|
|
59
|
-
|
|
60
|
-
await report({ allowRelink: false }); // 登录即上报,一条命令闭环;刚拿到新密钥,别再触发二次登录
|
|
70
|
+
console.log('\n✓ 已批准,密钥已存到 ' + CONFIG);
|
|
71
|
+
await uploadDays(days, { server, key: p.key, device });
|
|
61
72
|
console.log('\n想让它每半小时自动后台上报? npm i -g tokenforbes-cli 后跑 tokenforbes daemon install');
|
|
62
73
|
return;
|
|
63
74
|
}
|
|
64
75
|
if (p.status === 'expired') break;
|
|
65
76
|
process.stdout.write('.');
|
|
66
77
|
}
|
|
67
|
-
die('\n登录超时或验证码已过期,重新跑一次 npx tokenforbes-cli
|
|
78
|
+
die('\n登录超时或验证码已过期,重新跑一次 npx -y tokenforbes-cli');
|
|
68
79
|
}
|
|
69
80
|
|
|
70
81
|
function sleep(ms) { return new Promise((r) => setTimeout(r, ms)); }
|
|
@@ -84,37 +95,44 @@ async function register(nickname) {
|
|
|
84
95
|
const j = await post(server + '/api/register', { nickname, device });
|
|
85
96
|
c.key = j.key; c.server = server; saveConfig(c);
|
|
86
97
|
console.log(`注册成功,昵称「${j.nickname}」。密钥已存到 ${CONFIG}`);
|
|
87
|
-
console.log('
|
|
98
|
+
console.log('正在读取本地用量(ccusage)…');
|
|
99
|
+
await uploadDays(readUsage(), { server, key: j.key, device }); // 注册完顺手上报,别让用户再跑一条
|
|
88
100
|
}
|
|
89
101
|
|
|
90
102
|
async function report({ allowRelink = true } = {}) {
|
|
91
103
|
const c = loadConfig();
|
|
92
|
-
// 没密钥:直接带去登录(
|
|
104
|
+
// 没密钥:直接带去登录(link 会先弹浏览器,再后台抓用量),不让用户对着报错发愣。
|
|
93
105
|
if (!c.key) {
|
|
94
106
|
if (allowRelink) return link();
|
|
95
|
-
die('还没登录。先跑 npx tokenforbes-cli (浏览器一键登录),或 register / login');
|
|
107
|
+
die('还没登录。先跑 npx -y tokenforbes-cli (浏览器一键登录),或 register / login');
|
|
96
108
|
}
|
|
97
109
|
const server = c.server || DEFAULT_SERVER;
|
|
98
|
-
console.log('正在读取本地用量(ccusage)…');
|
|
99
|
-
const days = readUsage();
|
|
100
|
-
if (!days.length) return console.log('没读到用量数据(这台电脑还没用过 Claude Code / Codex?)。');
|
|
101
110
|
const device = detectDevice();
|
|
111
|
+
// 先用一个很快的空请求验密钥,别等抓完几十天用量才发现要重登——那样浏览器弹得太迟。
|
|
112
|
+
const probe = await postRaw(server + '/api/report', { key: c.key, device, days: [] });
|
|
113
|
+
if (!probe.ok) {
|
|
114
|
+
const authFailed = probe.status === 401 || /密钥/.test(probe.json?.error || '');
|
|
115
|
+
if (authFailed && allowRelink) {
|
|
116
|
+
console.log('本地密钥已失效,这就打开浏览器带你重新登录…');
|
|
117
|
+
delete c.key; saveConfig(c);
|
|
118
|
+
return link(); // 密钥无效,还没抓用量;交给 link 先弹浏览器再后台抓
|
|
119
|
+
}
|
|
120
|
+
die('上报失败:' + (probe.json?.error || probe.netError || probe.status));
|
|
121
|
+
}
|
|
122
|
+
// 密钥有效:抓用量并上传。
|
|
123
|
+
console.log('正在读取本地用量(ccusage)…');
|
|
124
|
+
await uploadDays(readUsage(), { server, key: c.key, device });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 把抓好的用量传上去。抽出来给 report / link / register 共用,避免各写一遍、避免重复抓。
|
|
128
|
+
async function uploadDays(days, { server, key, device }) {
|
|
129
|
+
if (!days || !days.length) return console.log('没读到用量数据(这台电脑还没用过 Claude Code / Codex?)。');
|
|
102
130
|
const total = days.reduce((s, d) => s + d.input + d.output + (d.cacheCreation || 0) + (d.cacheRead || 0), 0);
|
|
103
131
|
console.log(`设备:${device}`);
|
|
104
132
|
console.log(`共 ${days.length} 天,合计约 ${Math.round(total / 1e4).toLocaleString()} 万 token,正在上传…`);
|
|
105
|
-
const res = await postRaw(server + '/api/report', { key
|
|
106
|
-
if (res.ok) {
|
|
107
|
-
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
// 密钥失效(最常见:服务器重置过用户库,旧密钥查无此人):清掉旧密钥,自动带去重新登录再上报。
|
|
111
|
-
const authFailed = res.status === 401 || /密钥/.test(res.json?.error || '');
|
|
112
|
-
if (authFailed && allowRelink) {
|
|
113
|
-
console.log('\n本地密钥已失效(服务器可能重置过数据),带你重新登录一次…\n');
|
|
114
|
-
delete c.key; saveConfig(c);
|
|
115
|
-
return link();
|
|
116
|
-
}
|
|
117
|
-
die('上报失败:' + (res.json?.error || res.netError || res.status));
|
|
133
|
+
const res = await postRaw(server + '/api/report', { key, device, days });
|
|
134
|
+
if (res.ok) console.log(`上报成功 ✓ 已记录 ${res.json.days} 天。去 ${server} 看你的排名。`);
|
|
135
|
+
else die('上报失败:' + (res.json?.error || res.netError || res.status));
|
|
118
136
|
}
|
|
119
137
|
|
|
120
138
|
// 会抛错就 die 的版本:给登录/注册这类「失败就该停」的调用用。
|