tokenforbes-cli 0.1.4 → 0.1.5

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.
Files changed (3) hide show
  1. package/bin.js +4 -2
  2. package/daemon.js +146 -24
  3. package/package.json +1 -1
package/bin.js CHANGED
@@ -7,7 +7,7 @@ import path from 'node:path';
7
7
  import { exec } from 'node:child_process';
8
8
  import { readUsage } from './ccusage.js';
9
9
  import { detectDevice } from './device.js';
10
- import { daemon } from './daemon.js';
10
+ 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';
@@ -69,7 +69,7 @@ async function link(preDays) {
69
69
  c.key = p.key; c.server = server; saveConfig(c);
70
70
  console.log('\n✓ 已批准,密钥已存到 ' + CONFIG);
71
71
  await uploadDays(days, { server, key: p.key, device });
72
- console.log('\n想让它每半小时自动后台上报? npm i -g tokenforbes-cli 后跑 tokenforbes daemon install');
72
+ ensureDaemonInstalled();
73
73
  return;
74
74
  }
75
75
  if (p.status === 'expired') break;
@@ -97,6 +97,7 @@ async function register(nickname) {
97
97
  console.log(`注册成功,昵称「${j.nickname}」。密钥已存到 ${CONFIG}`);
98
98
  console.log('正在读取本地用量(ccusage)…');
99
99
  await uploadDays(readUsage(), { server, key: j.key, device }); // 注册完顺手上报,别让用户再跑一条
100
+ ensureDaemonInstalled();
100
101
  }
101
102
 
102
103
  async function report({ allowRelink = true } = {}) {
@@ -122,6 +123,7 @@ async function report({ allowRelink = true } = {}) {
122
123
  // 密钥有效:抓用量并上传。
123
124
  console.log('正在读取本地用量(ccusage)…');
124
125
  await uploadDays(readUsage(), { server, key: c.key, device });
126
+ ensureDaemonInstalled();
125
127
  }
126
128
 
127
129
  // 把抓好的用量传上去。抽出来给 report / link / register 共用,避免各写一遍、避免重复抓。
package/daemon.js CHANGED
@@ -1,38 +1,72 @@
1
- // 后台自动同步:在 macOS 上装一个 launchd 定时任务,每 30 分钟自动跑一次 report。
2
- // ponytail: 只做 macOS(launchd)—— 负责人在 Mac,对标的竞品也是这套。
3
- // Linux(systemd/cron)、Windows(计划任务)等真有人要再加,先不抽象。
1
+ // 后台自动同步:macOS launchd、Windows 用计划任务,每 30 分钟自动跑一次 report。
4
2
  import fs from 'node:fs';
5
3
  import os from 'node:os';
6
4
  import path from 'node:path';
7
5
  import { execFileSync } from 'node:child_process';
8
- import { fileURLToPath } from 'node:url';
9
6
 
10
7
  const LABEL = 'ai.tokenboard.report';
11
8
  const PLIST = path.join(os.homedir(), 'Library', 'LaunchAgents', LABEL + '.plist');
12
9
  const LOG = path.join(os.homedir(), '.tokenboard.log');
13
10
  const CONFIG = path.join(os.homedir(), '.tokenboard.json');
14
- const BIN = fileURLToPath(new URL('./bin.js', import.meta.url)); // 这个 CLI 的绝对路径
11
+ const VBS = path.join(os.homedir(), '.tokenboard.vbs'); // Windows 隐藏窗口启动器
15
12
  const INTERVAL = 30 * 60; // 秒
13
+ // launchd 不读用户 shell 配置,PATH 得自己给全。nvm/fnm 装的 node 不在这些标准目录,
14
+ // 所以安装时把「当前 node 的真实目录」排最前(realpath 解掉 fnm 每个终端的临时软链)。
15
+ const FALLBACK_PATH = '/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin';
16
+ // 上线前旧采集脚本(opentoken)的 launchd 任务名,新版安装/卸载时顺手关掉,免得双份任务并存。
17
+ export const LEGACY_LABELS = ['com.opentoken.daemon'];
16
18
 
17
- function ensureMac() {
18
- if (process.platform !== 'darwin') {
19
- console.error('后台自动同步目前只支持 macOS。其它系统可自己挂个定时任务跑 tokenforbes report');
19
+ export function launchdPath() {
20
+ let nodeBin = '';
21
+ try { nodeBin = path.dirname(fs.realpathSync(process.execPath)); } catch {}
22
+ if (!nodeBin || FALLBACK_PATH.split(':').includes(nodeBin)) return FALLBACK_PATH;
23
+ return nodeBin + ':' + FALLBACK_PATH;
24
+ }
25
+
26
+ export function reportCommand() {
27
+ return ['npx', '-y', 'tokenforbes-cli@latest', 'report'];
28
+ }
29
+
30
+ // Windows 上直接让计划任务跑 cmd 会每半小时闪一个黑窗,所以包一层 VBS:
31
+ // wscript 是无窗口宿主,由它再以隐藏方式(第二参 0)拉起 cmd。VBS 字符串里的双引号写成两个。
32
+ export function buildVbs() {
33
+ const cmd = 'cmd /d /c ' + reportCommand().join(' ') + ' >> ""' + LOG + '"" 2>&1';
34
+ return 'CreateObject("WScript.Shell").Run "' + cmd + '", 0, False\r\n';
35
+ }
36
+
37
+ export function windowsTaskArgs() {
38
+ return ['/create', '/tn', LABEL, '/sc', 'minute', '/mo', '30', '/tr', 'wscript.exe //B //Nologo "' + VBS + '"', '/f'];
39
+ }
40
+
41
+ function supported() {
42
+ return process.platform === 'darwin' || process.platform === 'win32';
43
+ }
44
+
45
+ function ensureSupported() {
46
+ if (!supported()) {
47
+ console.error('后台自动同步目前支持 macOS / Windows。其它系统可自己挂定时任务跑 npx -y tokenforbes-cli@latest report');
20
48
  process.exit(1);
21
49
  }
22
50
  }
23
51
 
24
- // launchd 任务说明书(plist)。用绝对路径,日志重定向到文件,失败不静默。
52
+ // launchd 任务说明书(plist)。日志重定向到文件,失败不静默。
53
+ // ponytail: 命令必须用 /bin/sh -c 包一层 —— launchd 找程序不认 EnvironmentVariables 里的 PATH,
54
+ // 裸写 npx 会以退出码 78(找不到程序)静默失败,真机实测过。PATH 是给 sh 找 npx 用的。
25
55
  export function buildPlist() {
26
56
  return `<?xml version="1.0" encoding="UTF-8"?>
27
57
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
28
58
  <plist version="1.0">
29
59
  <dict>
30
60
  <key>Label</key><string>${LABEL}</string>
61
+ <key>EnvironmentVariables</key>
62
+ <dict>
63
+ <key>PATH</key><string>${launchdPath()}</string>
64
+ </dict>
31
65
  <key>ProgramArguments</key>
32
66
  <array>
33
- <string>${process.execPath}</string>
34
- <string>${BIN}</string>
35
- <string>report</string>
67
+ <string>/bin/sh</string>
68
+ <string>-c</string>
69
+ <string>${reportCommand().join(' ')}</string>
36
70
  </array>
37
71
  <key>StartInterval</key><integer>${INTERVAL}</integer>
38
72
  <key>RunAtLoad</key><true/>
@@ -43,46 +77,134 @@ export function buildPlist() {
43
77
  `;
44
78
  }
45
79
 
46
- function install() {
47
- ensureMac();
80
+ function requireKey() {
48
81
  // 没登录就别装,否则只会每半小时报一次「未登录」错。
49
82
  let key;
50
83
  try { key = JSON.parse(fs.readFileSync(CONFIG, 'utf8')).key; } catch {}
51
- if (!key) { console.error('还没登录,先跑 npx tokenforbes-cli 登录后再装。'); process.exit(1); }
84
+ return Boolean(key);
85
+ }
52
86
 
53
- // ponytail: plist 里写死了 BIN 的绝对路径。装 daemon 请用全局安装(npm i -g),
54
- // 别用 npx —— npx 缓存目录会被清,清掉后 launchd 每半小时报一次「文件不存在」。
87
+ function cleanupLegacyMac() {
88
+ for (const label of LEGACY_LABELS) {
89
+ const p = path.join(os.homedir(), 'Library', 'LaunchAgents', label + '.plist');
90
+ if (!fs.existsSync(p)) continue;
91
+ try { execFileSync('launchctl', ['unload', p], { stdio: 'ignore' }); } catch {}
92
+ try { fs.rmSync(p, { force: true }); } catch {}
93
+ }
94
+ }
95
+
96
+ function installMac() {
97
+ cleanupLegacyMac();
55
98
  fs.mkdirSync(path.dirname(PLIST), { recursive: true });
56
99
  fs.writeFileSync(PLIST, buildPlist());
57
100
  // ponytail: 用 load/unload(老接口,现 macOS 仍可用);若哪天失效,换 bootstrap/bootout gui/$UID。
58
101
  // 先 unload 再 load = 幂等,重复装不报错。
59
102
  try { execFileSync('launchctl', ['unload', PLIST], { stdio: 'ignore' }); } catch {}
60
103
  execFileSync('launchctl', ['load', PLIST]);
104
+ }
105
+
106
+ function installWindows() {
107
+ fs.writeFileSync(VBS, buildVbs());
108
+ execFileSync('schtasks', windowsTaskArgs(), { stdio: 'ignore' });
109
+ }
110
+
111
+ function install() {
112
+ ensureSupported();
113
+ if (!requireKey()) {
114
+ console.error('还没登录,先跑 npx tokenforbes-cli 登录后再装。');
115
+ process.exit(1);
116
+ }
117
+ if (process.platform === 'darwin') installMac();
118
+ else installWindows();
61
119
  console.log('✓ 已开启后台自动同步,每 30 分钟自动上报一次。');
62
- console.log(' 任务文件: ' + PLIST);
63
- console.log(' 日志: ' + LOG);
120
+ if (process.platform === 'darwin') {
121
+ console.log(' 任务文件: ' + PLIST);
122
+ console.log(' 日志: ' + LOG);
123
+ }
64
124
  console.log(' 查看状态: tokenforbes daemon status');
65
125
  console.log(' 关掉它: tokenforbes daemon uninstall');
66
126
  }
67
127
 
68
- function status() {
69
- ensureMac();
70
- if (!fs.existsSync(PLIST)) return console.log('未安装。装它: tokenforbes daemon install');
128
+ function statusMac() {
129
+ if (!fs.existsSync(PLIST)) return false;
71
130
  let loaded = false;
72
131
  try { loaded = execFileSync('launchctl', ['list'], { encoding: 'utf8' }).includes(LABEL); } catch {}
73
132
  console.log(loaded ? '✓ 已安装并在运行,每 30 分钟自动上报。' : '已安装但未加载(重新 install 一下)。');
74
133
  console.log(' 任务文件: ' + PLIST);
75
134
  console.log(' 日志: ' + LOG + '(看最近几次上报有没有出错)');
135
+ return true;
76
136
  }
77
137
 
78
- function uninstall() {
79
- ensureMac();
138
+ function statusWindows() {
139
+ try {
140
+ execFileSync('schtasks', ['/query', '/tn', LABEL], { stdio: 'ignore' });
141
+ console.log('✓ 已安装,Windows 会每 30 分钟自动上报。');
142
+ return true;
143
+ } catch {
144
+ return false;
145
+ }
146
+ }
147
+
148
+ function needsInstall() {
149
+ if (process.platform === 'darwin') {
150
+ if (!fs.existsSync(PLIST)) return true;
151
+ // 内容不含新版命令 = 旧格式;不含当前 PATH = 用户换过 node 安装位置,都重装自愈。
152
+ try {
153
+ const content = fs.readFileSync(PLIST, 'utf8');
154
+ return !content.includes('tokenforbes-cli@latest') || !content.includes(launchdPath());
155
+ } catch { return true; }
156
+ }
157
+ if (process.platform === 'win32') {
158
+ try { execFileSync('schtasks', ['/query', '/tn', LABEL], { stdio: 'ignore' }); return false; } catch { return true; }
159
+ }
160
+ return true;
161
+ }
162
+
163
+ function status() {
164
+ ensureSupported();
165
+ const ok = process.platform === 'darwin' ? statusMac() : statusWindows();
166
+ if (!ok) console.log('未安装。装它: tokenforbes daemon install');
167
+ }
168
+
169
+ function uninstallMac() {
170
+ cleanupLegacyMac();
80
171
  if (!fs.existsSync(PLIST)) return console.log('本来就没装。');
81
172
  try { execFileSync('launchctl', ['unload', PLIST], { stdio: 'ignore' }); } catch {}
82
173
  fs.rmSync(PLIST, { force: true });
83
174
  console.log('✓ 已关闭后台自动同步,删除了任务文件。');
84
175
  }
85
176
 
177
+ function uninstallWindows() {
178
+ try {
179
+ execFileSync('schtasks', ['/delete', '/tn', LABEL, '/f'], { stdio: 'ignore' });
180
+ console.log('✓ 已关闭后台自动同步。');
181
+ } catch {
182
+ console.log('本来就没装。');
183
+ }
184
+ fs.rmSync(VBS, { force: true });
185
+ }
186
+
187
+ function uninstall() {
188
+ ensureSupported();
189
+ if (process.platform === 'darwin') return uninstallMac();
190
+ return uninstallWindows();
191
+ }
192
+
193
+ export function ensureDaemonInstalled() {
194
+ if (!supported()) return;
195
+ // 老脚本清理独立于「要不要装」:就算新任务已装好,发现旧任务也顺手关掉。
196
+ if (process.platform === 'darwin') { try { cleanupLegacyMac(); } catch {} }
197
+ if (!needsInstall()) return;
198
+ try {
199
+ if (!requireKey()) return;
200
+ if (process.platform === 'darwin') installMac();
201
+ else installWindows();
202
+ console.log('后台自动同步已开启:之后每 30 分钟自动上报一次。');
203
+ } catch (e) {
204
+ console.log('后台自动同步没自动开启,可稍后手动跑: tokenforbes daemon install');
205
+ }
206
+ }
207
+
86
208
  export function daemon(sub) {
87
209
  if (sub === 'install') return install();
88
210
  if (sub === 'status') return status();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tokenforbes-cli",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "上报本地 AI 编程工具(Claude Code / Codex 等)的 token 用量到 Token 福布斯排行榜(ceorank.cn)。只读用量数字和设备型号,绝不碰你的代码内容。",
5
5
  "type": "module",
6
6
  "bin": {