tokmon 0.9.0 → 0.9.1
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/dist/cli.js +34 -14
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -258,29 +258,49 @@ async function fetchTable() {
|
|
|
258
258
|
|
|
259
259
|
// src/billing.ts
|
|
260
260
|
import { execFile as execFileCb } from "child_process";
|
|
261
|
+
import { readFile as readFile2 } from "fs/promises";
|
|
262
|
+
import { join as join3 } from "path";
|
|
263
|
+
import { homedir as homedir3 } from "os";
|
|
261
264
|
import { promisify } from "util";
|
|
262
265
|
var execFile = promisify(execFileCb);
|
|
266
|
+
function credentialsFilePath() {
|
|
267
|
+
const base = process.env.CLAUDE_CONFIG_DIR ?? join3(homedir3(), ".claude");
|
|
268
|
+
return join3(base, ".credentials.json");
|
|
269
|
+
}
|
|
270
|
+
async function readCredentialsFile() {
|
|
271
|
+
try {
|
|
272
|
+
const raw = await readFile2(credentialsFilePath(), "utf-8");
|
|
273
|
+
const creds = JSON.parse(raw);
|
|
274
|
+
return creds?.claudeAiOauth?.accessToken ?? creds?.accessToken ?? null;
|
|
275
|
+
} catch {
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
async function readMacKeychain() {
|
|
280
|
+
try {
|
|
281
|
+
const { stdout } = await execFile("security", [
|
|
282
|
+
"find-generic-password",
|
|
283
|
+
"-s",
|
|
284
|
+
"Claude Code-credentials",
|
|
285
|
+
"-w"
|
|
286
|
+
], { timeout: 5e3 });
|
|
287
|
+
const creds = JSON.parse(stdout.trim());
|
|
288
|
+
return creds?.claudeAiOauth?.accessToken ?? creds?.accessToken ?? null;
|
|
289
|
+
} catch {
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
263
293
|
async function getAccessToken() {
|
|
264
294
|
if (process.platform === "darwin") {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
"find-generic-password",
|
|
268
|
-
"-s",
|
|
269
|
-
"Claude Code-credentials",
|
|
270
|
-
"-w"
|
|
271
|
-
], { timeout: 5e3 });
|
|
272
|
-
const creds = JSON.parse(stdout.trim());
|
|
273
|
-
return creds?.claudeAiOauth?.accessToken ?? null;
|
|
274
|
-
} catch {
|
|
275
|
-
return null;
|
|
276
|
-
}
|
|
295
|
+
const token = await readMacKeychain();
|
|
296
|
+
if (token) return token;
|
|
277
297
|
}
|
|
278
|
-
return
|
|
298
|
+
return readCredentialsFile();
|
|
279
299
|
}
|
|
280
300
|
var EMPTY = { session: null, weekly: null, sonnet: null, extraUsage: null, error: null };
|
|
281
301
|
async function fetchBilling() {
|
|
282
302
|
const token = await getAccessToken();
|
|
283
|
-
if (!token) return { ...EMPTY, error: "No OAuth token
|
|
303
|
+
if (!token) return { ...EMPTY, error: "No OAuth token \u2014 run claude and log in" };
|
|
284
304
|
try {
|
|
285
305
|
const res = await fetch("https://api.anthropic.com/api/oauth/usage", {
|
|
286
306
|
headers: {
|