@tsa-group/claude-usage 0.4.7 → 0.4.8

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/README.md CHANGED
@@ -362,6 +362,9 @@ v0.4.7 起支援自我更新,但**由伺服器決定**,不是去問 npm:
362
362
  (實測:2026-08-29 四台 Win11 因此全數斷線 10.7–18.1 小時,其中一台累計只跑過 7 次。)
363
363
  - **重疊的 tick 尚未互斥**:VBS 以非阻塞方式啟動 node,工作排程器認為任務瞬間完成,
364
364
  所以 `MultipleInstances` 擋不住重疊的 node 行程。極少數情況下會競寫 `health.json`。
365
+ - **npm 11 起不跑 postinstall**:安裝時會印 `npm warn allow-scripts`,我們的安裝提示
366
+ 因此不會顯示。同樣的檢查已搬進 `claude-usage install` 與 `doctor`,所以裝完務必
367
+ 跑一次 `install`。
365
368
  - **PowerShell 執行原則**:預設 `Restricted` 會同時擋掉 `npm.ps1`(安裝時)與
366
369
  `claude-usage.ps1`(執行時)。處理方式見上面 **Windows → 第 0 步**。
367
370
  - **Linux 背景任務未實作**(systemd --user timer)。其餘指令可用。
package/dist/install.js CHANGED
@@ -141,7 +141,32 @@ function winHardenTask() {
141
141
  "-AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable)");
142
142
  return false;
143
143
  }
144
+ /**
145
+ * 安裝時檢查 PowerShell 執行原則。
146
+ *
147
+ * ★ 為什麼這段從 postinstall.mjs 搬到這裡:npm 11 起預設**不執行**未核准的
148
+ * postinstall 腳本(安裝時只印 `npm warn allow-scripts`),所以那支檔案在新版 npm
149
+ * 上根本不會跑 —— 它原本的唯一價值(在使用者撞牆前先講)已經失效。
150
+ * `install` 是 `npm i -g` 之後緊接著要跑的指令,把提示放這裡才真的看得到。
151
+ *
152
+ * ★ 為什麼仍然要提示而不是自動修:改動使用者機器的安全設定,該由他自己決定。
153
+ */
154
+ function warnExecutionPolicy() {
155
+ const r = spawnSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", "Get-ExecutionPolicy"], { encoding: "utf8", timeout: 10_000, windowsHide: true });
156
+ const policy = r.error || r.status !== 0 ? null : (r.stdout ?? "").trim();
157
+ // 只在**實際會擋**的機器上印。無條件每次都印,就是在訓練人忽略它。
158
+ if (policy !== "Restricted" && policy !== "AllSigned")
159
+ return;
160
+ console.log(`\n! PowerShell 執行原則是 ${policy} —— 直接打 claude-usage 會被擋\n` +
161
+ " (錯誤含 UnauthorizedAccess / PSSecurityException / ExecutionPolicy)\n" +
162
+ " 擇一處理:\n" +
163
+ " 1) Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned\n" +
164
+ " 設定一次、永久生效、免系統管理員。\n" +
165
+ " 2) 不改原則,改叫 claude-usage.cmd(功能完全一樣)\n" +
166
+ " 公司用 GPO 鎖了機器層級原則時 (1) 會被覆寫 —— 直接用 (2)。\n");
167
+ }
144
168
  function winInstall() {
169
+ warnExecutionPolicy(); // 先講,因為接下來每一步都可能因為它而失敗
145
170
  const vbs = `Set s = CreateObject("Wscript.Shell")\r\n` +
146
171
  `s.Run """${NODE}"" ""${CLI}"" daemon-tick", 0, False\r\n`;
147
172
  writeFileSync(VBS_PATH(), vbs, "utf8");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsa-group/claude-usage",
3
- "version": "0.4.7",
3
+ "version": "0.4.8",
4
4
  "description": "Per-user Claude usage collector — measures Claude Code token detail and account-level rate-limit utilization locally, reports to your own ingest server.",
5
5
  "type": "module",
6
6
  "bin": {
package/postinstall.mjs CHANGED
@@ -1,6 +1,11 @@
1
1
  /**
2
2
  * 安裝後提示:Windows 的 PowerShell 執行原則會擋掉 npm 產生的 .ps1 啟動器。
3
3
  *
4
+ * ⚠️ npm 11 起預設**不執行**未核准的 postinstall(只印 `npm warn allow-scripts`),
5
+ * 所以這支檔案在新版 npm 上不會跑。同樣的檢查已於 v0.4.8 搬進 `install`
6
+ * 與 `doctor`(見 src/install.ts warnExecutionPolicy)。這裡保留是因為在
7
+ * 舊版 npm 上它仍然是唯一能「撞牆之前先說話」的時機。
8
+ *
4
9
  * ★ 為什麼一定要在這裡講:這個失敗發生時**我們的程式碼根本沒機會執行** ——
5
10
  * PowerShell 在 Node 啟動前就拒絕載入 `claude-usage.ps1` 了。所以工具永遠無法在
6
11
  * 出錯當下自我診斷,提示只能前置,而 postinstall 是唯一「撞牆之前還能說話」的