@aiusage/cli 1.1.0 → 1.2.0
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 +19 -2
- package/dist/cli.js +256 -9
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
`@aiusage/cli` is the AIUsage command-line tool for:
|
|
4
4
|
|
|
5
|
-
-
|
|
5
|
+
- discovering and managing projects across AI tools
|
|
6
|
+
- scanning local Claude Code, Codex, Copilot CLI, and Gemini CLI token usage
|
|
6
7
|
- printing local usage summaries for the last 7 days, 30 days, 90 days, or all history
|
|
7
8
|
- scheduling automatic sync to an AIUsage Worker
|
|
8
9
|
- diagnosing configuration and connectivity issues
|
|
@@ -27,6 +28,22 @@ aiusage --help
|
|
|
27
28
|
|
|
28
29
|
## Commands
|
|
29
30
|
|
|
31
|
+
### project
|
|
32
|
+
|
|
33
|
+
Discover and manage projects on this machine.
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
aiusage project # list all discovered projects (default)
|
|
37
|
+
aiusage project list # same as above
|
|
38
|
+
aiusage project alias myapp "我的应用" # set alias for a project
|
|
39
|
+
aiusage project alias # list all configured aliases
|
|
40
|
+
aiusage project alias --remove myapp # remove alias
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Scans data directories for Claude Code, Codex, Copilot CLI, and Gemini CLI, listing discovered projects with their aliases and sources.
|
|
44
|
+
|
|
45
|
+
Project aliases are applied locally before upload. If two devices set the same alias for their respective project directories, the server merges them into one project.
|
|
46
|
+
|
|
30
47
|
### report
|
|
31
48
|
|
|
32
49
|
Local usage report. No cloud upload required.
|
|
@@ -129,7 +146,7 @@ aiusage config set lang zh # default language: en o
|
|
|
129
146
|
aiusage config set emoji false # disable emoji in report title
|
|
130
147
|
aiusage config set device.alias "MacBook Pro 工作机"
|
|
131
148
|
aiusage config set privacy.projectVisibility masked
|
|
132
|
-
aiusage config set project.alias
|
|
149
|
+
aiusage config set project.alias MyApp "我的应用" # prefer: aiusage project alias
|
|
133
150
|
```
|
|
134
151
|
|
|
135
152
|
CLI flags (`--lang`, `--no-emoji`) override config values for a single run.
|
package/dist/cli.js
CHANGED
|
@@ -262,7 +262,7 @@ async function processCodexFile(filePath, targetDateSet, projectAliases, grouped
|
|
|
262
262
|
let rl = createInterface2({
|
|
263
263
|
input: fh.createReadStream({ encoding: "utf-8" }),
|
|
264
264
|
crlfDelay: 1 / 0
|
|
265
|
-
}), currentModel = "unknown", currentProject = "unknown";
|
|
265
|
+
}), currentModel = "unknown", currentProject = "unknown", previousTotal = {};
|
|
266
266
|
for await (let line of rl) {
|
|
267
267
|
if (!line || Buffer.byteLength(line) > MAX_LINE_BYTES2) continue;
|
|
268
268
|
let record;
|
|
@@ -278,7 +278,7 @@ async function processCodexFile(filePath, targetDateSet, projectAliases, grouped
|
|
|
278
278
|
}
|
|
279
279
|
if (record.type !== "event_msg" || record.payload?.type !== "token_count") continue;
|
|
280
280
|
let info = record.payload?.info;
|
|
281
|
-
if (!info?.
|
|
281
|
+
if (!info?.total_token_usage) continue;
|
|
282
282
|
let ts = parseTimestamp2(record.timestamp);
|
|
283
283
|
if (!ts) continue;
|
|
284
284
|
let usageDate = toDateKey2(ts);
|
|
@@ -286,17 +286,24 @@ async function processCodexFile(filePath, targetDateSet, projectAliases, grouped
|
|
|
286
286
|
let total = info.total_token_usage, signature = `${total.input_tokens ?? 0}|${total.cached_input_tokens ?? 0}|${total.output_tokens ?? 0}|${total.reasoning_output_tokens ?? 0}|${total.total_tokens ?? 0}`;
|
|
287
287
|
if (globalSeenSigs.has(signature)) continue;
|
|
288
288
|
globalSeenSigs.add(signature);
|
|
289
|
-
let last = info.last_token_usage
|
|
289
|
+
let last = info.last_token_usage ?? {
|
|
290
|
+
input_tokens: Math.max(0, (total.input_tokens ?? 0) - (previousTotal.input_tokens ?? 0)),
|
|
291
|
+
cached_input_tokens: Math.max(0, (total.cached_input_tokens ?? 0) - (previousTotal.cached_input_tokens ?? 0)),
|
|
292
|
+
output_tokens: Math.max(0, (total.output_tokens ?? 0) - (previousTotal.output_tokens ?? 0)),
|
|
293
|
+
reasoning_output_tokens: Math.max(0, (total.reasoning_output_tokens ?? 0) - (previousTotal.reasoning_output_tokens ?? 0))
|
|
294
|
+
};
|
|
295
|
+
previousTotal = total;
|
|
296
|
+
let nonCachedInput = Math.max(0, (last.input_tokens ?? 0) - (last.cached_input_tokens ?? 0)), grouped = groupedByDate.get(usageDate);
|
|
290
297
|
if (!grouped) continue;
|
|
291
298
|
let key = `${currentModel}|${currentProject}`, existing = grouped.get(key);
|
|
292
|
-
existing ? (existing.eventCount += 1, existing.inputTokens +=
|
|
299
|
+
existing ? (existing.eventCount += 1, existing.inputTokens += nonCachedInput, existing.cachedInputTokens += last.cached_input_tokens ?? 0, existing.outputTokens += last.output_tokens ?? 0, existing.reasoningOutputTokens += last.reasoning_output_tokens ?? 0) : grouped.set(key, {
|
|
293
300
|
provider: "openai",
|
|
294
301
|
product: "codex",
|
|
295
302
|
channel: "cli",
|
|
296
303
|
model: currentModel,
|
|
297
304
|
project: currentProject,
|
|
298
305
|
eventCount: 1,
|
|
299
|
-
inputTokens:
|
|
306
|
+
inputTokens: nonCachedInput,
|
|
300
307
|
cachedInputTokens: last.cached_input_tokens ?? 0,
|
|
301
308
|
cacheWriteTokens: 0,
|
|
302
309
|
outputTokens: last.output_tokens ?? 0,
|
|
@@ -327,7 +334,7 @@ async function walkDir(dir, result) {
|
|
|
327
334
|
}
|
|
328
335
|
for (let entry of entries) {
|
|
329
336
|
let fullPath = join3(dir, entry.name);
|
|
330
|
-
entry.isDirectory() ? await walkDir(fullPath, result) : entry.name.
|
|
337
|
+
entry.isDirectory() ? await walkDir(fullPath, result) : entry.name.endsWith(".jsonl") && result.push(fullPath);
|
|
331
338
|
}
|
|
332
339
|
}
|
|
333
340
|
function parseTimestamp2(value) {
|
|
@@ -2246,6 +2253,192 @@ async function runDoctor(lang = "zh") {
|
|
|
2246
2253
|
}) : checks.push({ group: g4, name: s.schedule, status: "warn", message: s.scheduleOff }), checks;
|
|
2247
2254
|
}
|
|
2248
2255
|
|
|
2256
|
+
// src/project.ts
|
|
2257
|
+
import { open as open3, readdir as readdir7, readFile as readFile12 } from "node:fs/promises";
|
|
2258
|
+
import { join as join16 } from "node:path";
|
|
2259
|
+
import { homedir as homedir16 } from "node:os";
|
|
2260
|
+
async function discoverProjects(projectAliases) {
|
|
2261
|
+
let projectMap = /* @__PURE__ */ new Map();
|
|
2262
|
+
function add(name, source) {
|
|
2263
|
+
!name || name === "unknown" || (projectMap.has(name) || projectMap.set(name, /* @__PURE__ */ new Set()), projectMap.get(name).add(source));
|
|
2264
|
+
}
|
|
2265
|
+
await Promise.all([
|
|
2266
|
+
// Claude: ~/.claude/projects/{encoded-path}/
|
|
2267
|
+
// 目录名是编码路径(- 替换 /),但项目名本身可能含 -,无法可靠还原。
|
|
2268
|
+
// 读取每个项目目录下第一个 jsonl 首行的 cwd 字段来获取真实项目名。
|
|
2269
|
+
discoverClaudeProjects().then((names) => names.forEach((n) => add(n, "claude"))),
|
|
2270
|
+
// Codex: ~/.codex/sessions/YYYY/MM/DD/*.jsonl
|
|
2271
|
+
// 每个 session 首行 session_meta 含 cwd 字段
|
|
2272
|
+
discoverCodexProjects().then((names) => names.forEach((n) => add(n, "codex"))),
|
|
2273
|
+
// Copilot CLI: ~/.copilot/session-state/{sessionId}/events.jsonl
|
|
2274
|
+
// 首行 session.start 含 data.context.gitRoot / cwd
|
|
2275
|
+
discoverCopilotProjects().then((names) => names.forEach((n) => add(n, "copilot"))),
|
|
2276
|
+
// Gemini CLI: ~/.gemini/projects.json 含 { projects: { path: name } }
|
|
2277
|
+
discoverGeminiProjects().then((names) => names.forEach((n) => add(n, "gemini")))
|
|
2278
|
+
]);
|
|
2279
|
+
let results = [];
|
|
2280
|
+
for (let [name, sources] of projectMap) {
|
|
2281
|
+
let alias = projectAliases?.[name];
|
|
2282
|
+
results.push({
|
|
2283
|
+
name,
|
|
2284
|
+
alias,
|
|
2285
|
+
sources: [...sources].sort()
|
|
2286
|
+
});
|
|
2287
|
+
}
|
|
2288
|
+
return results.sort((a, b) => a.name.localeCompare(b.name));
|
|
2289
|
+
}
|
|
2290
|
+
async function discoverClaudeProjects() {
|
|
2291
|
+
let names = [];
|
|
2292
|
+
for (let baseDir of getClaudeDirs()) {
|
|
2293
|
+
let entries;
|
|
2294
|
+
try {
|
|
2295
|
+
entries = await readdir7(baseDir, { withFileTypes: !0 });
|
|
2296
|
+
} catch {
|
|
2297
|
+
continue;
|
|
2298
|
+
}
|
|
2299
|
+
for (let entry of entries) {
|
|
2300
|
+
if (!entry.isDirectory() || entry.name.startsWith(".")) continue;
|
|
2301
|
+
let projectDir = join16(baseDir, entry.name), name = await extractCwdFromFirstJsonl(projectDir);
|
|
2302
|
+
name && name !== "unknown" && names.push(name);
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
return names;
|
|
2306
|
+
}
|
|
2307
|
+
async function discoverCodexProjects() {
|
|
2308
|
+
let sessionsDir = join16(homedir16(), ".codex", "sessions"), files = await walkJsonlFiles(sessionsDir), projects = /* @__PURE__ */ new Set();
|
|
2309
|
+
for (let filePath of files) {
|
|
2310
|
+
let name = await extractCwdFromSessionMeta(filePath);
|
|
2311
|
+
name && name !== "unknown" && projects.add(name);
|
|
2312
|
+
}
|
|
2313
|
+
return [...projects];
|
|
2314
|
+
}
|
|
2315
|
+
async function extractCwdFromSessionMeta(filePath) {
|
|
2316
|
+
let fh;
|
|
2317
|
+
try {
|
|
2318
|
+
fh = await open3(filePath, "r");
|
|
2319
|
+
let buf = Buffer.alloc(4096), { bytesRead } = await fh.read(buf, 0, buf.length, 0);
|
|
2320
|
+
if (bytesRead === 0) return;
|
|
2321
|
+
let line = buf.subarray(0, bytesRead).toString("utf-8").split(`
|
|
2322
|
+
`)[0], cwd = JSON.parse(line)?.payload?.cwd;
|
|
2323
|
+
if (!cwd) return;
|
|
2324
|
+
let parts = cwd.split("/").filter(Boolean);
|
|
2325
|
+
return parts[parts.length - 1] || void 0;
|
|
2326
|
+
} catch {
|
|
2327
|
+
return;
|
|
2328
|
+
} finally {
|
|
2329
|
+
await fh?.close();
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
async function walkJsonlFiles(dir) {
|
|
2333
|
+
let result = [];
|
|
2334
|
+
async function walk2(d) {
|
|
2335
|
+
let entries;
|
|
2336
|
+
try {
|
|
2337
|
+
entries = await readdir7(d, { withFileTypes: !0 });
|
|
2338
|
+
} catch {
|
|
2339
|
+
return;
|
|
2340
|
+
}
|
|
2341
|
+
for (let entry of entries) {
|
|
2342
|
+
let full = join16(d, entry.name);
|
|
2343
|
+
entry.isDirectory() ? await walk2(full) : entry.name.endsWith(".jsonl") && result.push(full);
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
return await walk2(dir), result;
|
|
2347
|
+
}
|
|
2348
|
+
async function extractCwdFromFirstJsonl(dir) {
|
|
2349
|
+
let jsonlPath = await findFirstJsonl(dir);
|
|
2350
|
+
if (!jsonlPath) return;
|
|
2351
|
+
let fh;
|
|
2352
|
+
try {
|
|
2353
|
+
fh = await open3(jsonlPath, "r");
|
|
2354
|
+
let buf = Buffer.alloc(32 * 1024), { bytesRead } = await fh.read(buf, 0, buf.length, 0);
|
|
2355
|
+
if (bytesRead === 0) return;
|
|
2356
|
+
let lines = buf.subarray(0, bytesRead).toString("utf-8").split(`
|
|
2357
|
+
`);
|
|
2358
|
+
for (let line of lines)
|
|
2359
|
+
if (line.trim())
|
|
2360
|
+
try {
|
|
2361
|
+
let record = JSON.parse(line);
|
|
2362
|
+
if (record?.cwd) {
|
|
2363
|
+
let parts = record.cwd.split("/").filter(Boolean), name = parts[parts.length - 1];
|
|
2364
|
+
if (name) return name;
|
|
2365
|
+
}
|
|
2366
|
+
} catch {
|
|
2367
|
+
continue;
|
|
2368
|
+
}
|
|
2369
|
+
return;
|
|
2370
|
+
} catch {
|
|
2371
|
+
return;
|
|
2372
|
+
} finally {
|
|
2373
|
+
await fh?.close();
|
|
2374
|
+
}
|
|
2375
|
+
}
|
|
2376
|
+
async function findFirstJsonl(dir) {
|
|
2377
|
+
let entries;
|
|
2378
|
+
try {
|
|
2379
|
+
entries = await readdir7(dir, { withFileTypes: !0 });
|
|
2380
|
+
} catch {
|
|
2381
|
+
return;
|
|
2382
|
+
}
|
|
2383
|
+
let topJsonl = entries.find((e) => e.isFile() && e.name.endsWith(".jsonl"));
|
|
2384
|
+
if (topJsonl) return join16(dir, topJsonl.name);
|
|
2385
|
+
for (let entry of entries)
|
|
2386
|
+
if (!(!entry.isDirectory() || entry.name.startsWith(".")))
|
|
2387
|
+
try {
|
|
2388
|
+
let sub = (await readdir7(join16(dir, entry.name))).find((f) => f.endsWith(".jsonl"));
|
|
2389
|
+
if (sub) return join16(dir, entry.name, sub);
|
|
2390
|
+
} catch {
|
|
2391
|
+
continue;
|
|
2392
|
+
}
|
|
2393
|
+
}
|
|
2394
|
+
async function discoverCopilotProjects() {
|
|
2395
|
+
let dir = join16(homedir16(), ".copilot", "session-state"), files = await walkJsonlFiles(dir), projects = /* @__PURE__ */ new Set();
|
|
2396
|
+
for (let filePath of files) {
|
|
2397
|
+
let fh;
|
|
2398
|
+
try {
|
|
2399
|
+
fh = await open3(filePath, "r");
|
|
2400
|
+
let buf = Buffer.alloc(4096), { bytesRead } = await fh.read(buf, 0, buf.length, 0);
|
|
2401
|
+
if (bytesRead === 0) continue;
|
|
2402
|
+
let line = buf.subarray(0, bytesRead).toString("utf-8").split(`
|
|
2403
|
+
`)[0], record = JSON.parse(line), raw = record?.data?.context?.gitRoot ?? record?.data?.context?.cwd;
|
|
2404
|
+
if (raw) {
|
|
2405
|
+
let parts = raw.split("/").filter(Boolean), name = parts[parts.length - 1];
|
|
2406
|
+
name && name !== "unknown" && projects.add(name);
|
|
2407
|
+
}
|
|
2408
|
+
} catch {
|
|
2409
|
+
continue;
|
|
2410
|
+
} finally {
|
|
2411
|
+
await fh?.close();
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
return [...projects];
|
|
2415
|
+
}
|
|
2416
|
+
async function discoverGeminiProjects() {
|
|
2417
|
+
let filePath = join16(homedir16(), ".gemini", "projects.json");
|
|
2418
|
+
try {
|
|
2419
|
+
let raw = await readFile12(filePath, "utf-8"), projects = JSON.parse(raw)?.projects;
|
|
2420
|
+
if (!projects || typeof projects != "object") return [];
|
|
2421
|
+
let names = [];
|
|
2422
|
+
for (let [path, _name] of Object.entries(projects)) {
|
|
2423
|
+
let parts = path.split("/").filter(Boolean), name = parts[parts.length - 1];
|
|
2424
|
+
name && name !== "unknown" && names.push(name);
|
|
2425
|
+
}
|
|
2426
|
+
return names;
|
|
2427
|
+
} catch {
|
|
2428
|
+
return [];
|
|
2429
|
+
}
|
|
2430
|
+
}
|
|
2431
|
+
function getClaudeDirs() {
|
|
2432
|
+
let envVar = process.env.CLAUDE_CONFIG_DIR?.trim();
|
|
2433
|
+
if (envVar)
|
|
2434
|
+
return envVar.split(",").map((p) => p.trim()).filter(Boolean).map((p) => join16(p, "projects"));
|
|
2435
|
+
let home = homedir16();
|
|
2436
|
+
return [
|
|
2437
|
+
join16(home, ".config", "claude", "projects"),
|
|
2438
|
+
join16(home, ".claude", "projects")
|
|
2439
|
+
];
|
|
2440
|
+
}
|
|
2441
|
+
|
|
2249
2442
|
// src/cli.ts
|
|
2250
2443
|
var argv = process.argv.slice(2), command = argv[0];
|
|
2251
2444
|
try {
|
|
@@ -2285,9 +2478,15 @@ try {
|
|
|
2285
2478
|
} else if (command === "doctor") {
|
|
2286
2479
|
let parsed = parseArgs(argv.slice(1));
|
|
2287
2480
|
await runDoctorCommand(parsed.flags);
|
|
2288
|
-
} else command === "config" && argv[1] === "set"
|
|
2481
|
+
} else if (command === "config" && argv[1] === "set")
|
|
2482
|
+
await runConfigSet(argv.slice(2));
|
|
2483
|
+
else if (command === "project") {
|
|
2484
|
+
let sub = argv[1];
|
|
2485
|
+
sub === "list" || sub === void 0 ? await runProjectList() : sub === "alias" ? await runProjectAlias(argv.slice(2)) : (console.error(`\u672A\u77E5\u5B50\u547D\u4EE4: project ${sub}`), console.log("\u53EF\u7528: aiusage project list, aiusage project alias <\u9879\u76EE\u540D> <\u522B\u540D>"), process.exitCode = 1);
|
|
2486
|
+
} else command === "setup" ? (console.log(`To deploy the server, clone the repo and run the setup wizard:
|
|
2289
2487
|
`), console.log(" git clone https://github.com/ennann/aiusage.git"), console.log(" cd aiusage && pnpm install"), console.log(` pnpm setup
|
|
2290
|
-
`), console.log("See: https://github.com/ennann/aiusage#deploy-your-own-server")) : printHelp()
|
|
2488
|
+
`), console.log("See: https://github.com/ennann/aiusage#deploy-your-own-server")) : command === "--help" || command === "-h" || command === "help" ? printHelp() : (command && console.error(`\u672A\u77E5\u547D\u4EE4: "${command}"
|
|
2489
|
+
`), printUsageHint());
|
|
2291
2490
|
} catch (error) {
|
|
2292
2491
|
console.error(error instanceof Error ? error.message : String(error)), process.exitCode = 1;
|
|
2293
2492
|
}
|
|
@@ -2465,10 +2664,58 @@ async function runConfigSet(args) {
|
|
|
2465
2664
|
let config = await readConfig(), next = setConfigValue(config, keyPath, values);
|
|
2466
2665
|
await writeConfig(next), console.log(JSON.stringify({ configPath: getConfigPath(), updated: keyPath }, null, 2));
|
|
2467
2666
|
}
|
|
2667
|
+
async function runProjectList() {
|
|
2668
|
+
let config = await readConfig(), projects = await discoverProjects(config.projectAliases);
|
|
2669
|
+
if (projects.length === 0) {
|
|
2670
|
+
console.log("\u672A\u53D1\u73B0\u4EFB\u4F55\u9879\u76EE\u3002");
|
|
2671
|
+
return;
|
|
2672
|
+
}
|
|
2673
|
+
let nameWidth = Math.max(6, ...projects.map((p) => p.name.length)), aliasWidth = Math.max(4, ...projects.map((p) => (p.alias ?? "-").length));
|
|
2674
|
+
console.log(
|
|
2675
|
+
"\u9879\u76EE".padEnd(nameWidth + 2) + "\u522B\u540D".padEnd(aliasWidth + 2) + "\u6765\u6E90"
|
|
2676
|
+
), console.log("-".repeat(nameWidth + aliasWidth + 20));
|
|
2677
|
+
for (let p of projects)
|
|
2678
|
+
console.log(
|
|
2679
|
+
p.name.padEnd(nameWidth + 2) + (p.alias ?? "-").padEnd(aliasWidth + 2) + p.sources.join(", ")
|
|
2680
|
+
);
|
|
2681
|
+
console.log(`
|
|
2682
|
+
\u5171 ${projects.length} \u4E2A\u9879\u76EE`);
|
|
2683
|
+
}
|
|
2684
|
+
async function runProjectAlias(args) {
|
|
2685
|
+
if (args.length === 0) {
|
|
2686
|
+
let aliases = (await readConfig()).projectAliases ?? {}, entries = Object.entries(aliases);
|
|
2687
|
+
if (entries.length === 0) {
|
|
2688
|
+
console.log("\u5C1A\u672A\u8BBE\u7F6E\u4EFB\u4F55\u9879\u76EE\u522B\u540D\u3002"), console.log("\u7528\u6CD5: aiusage project alias <\u9879\u76EE\u540D> <\u522B\u540D>");
|
|
2689
|
+
return;
|
|
2690
|
+
}
|
|
2691
|
+
for (let [from, to] of entries)
|
|
2692
|
+
console.log(` ${from} \u2192 ${to}`);
|
|
2693
|
+
return;
|
|
2694
|
+
}
|
|
2695
|
+
if (args[0] === "--remove") {
|
|
2696
|
+
let name2 = args.slice(1).join(" ").trim();
|
|
2697
|
+
if (!name2) throw new Error("\u8BF7\u6307\u5B9A\u8981\u79FB\u9664\u522B\u540D\u7684\u9879\u76EE\u540D");
|
|
2698
|
+
let config2 = await readConfig(), aliases = { ...config2.projectAliases ?? {} };
|
|
2699
|
+
if (!(name2 in aliases))
|
|
2700
|
+
throw new Error(`\u9879\u76EE "${name2}" \u672A\u8BBE\u7F6E\u522B\u540D`);
|
|
2701
|
+
delete aliases[name2], config2.projectAliases = Object.keys(aliases).length > 0 ? aliases : void 0, await writeConfig(config2), console.log(`\u5DF2\u79FB\u9664 "${name2}" \u7684\u522B\u540D\u3002`);
|
|
2702
|
+
return;
|
|
2703
|
+
}
|
|
2704
|
+
if (args.length < 2)
|
|
2705
|
+
throw new Error("\u7528\u6CD5: aiusage project alias <\u9879\u76EE\u540D> <\u522B\u540D>");
|
|
2706
|
+
let name = args[0], alias = args.slice(1).join(" ").trim();
|
|
2707
|
+
if (!alias) throw new Error("\u522B\u540D\u4E0D\u80FD\u4E3A\u7A7A");
|
|
2708
|
+
let config = await readConfig();
|
|
2709
|
+
config.projectAliases = { ...config.projectAliases ?? {}, [name]: alias }, await writeConfig(config), console.log(`\u5DF2\u8BBE\u7F6E: ${name} \u2192 ${alias}`);
|
|
2710
|
+
}
|
|
2468
2711
|
function printHelp() {
|
|
2469
2712
|
let initialized = existsSync2(getConfigPath());
|
|
2470
2713
|
console.log(`aiusage v${getVersion()}
|
|
2471
|
-
`), console.log("Usage: aiusage <command>"), console.log(""), console.log("Commands:"), console.log(" aiusage init [--device-id ID] [--device-name NAME] [--lookback N]"), console.log(" aiusage enroll --server URL --site-id ID --enroll-token TOKEN [--target NAME]"), console.log(" aiusage sync [--target NAME] [--date YYYY-MM-DD] [--from YYYY-MM-DD [--to YYYY-MM-DD]] [--lookback N] [--today]"), console.log(" aiusage health [--target NAME] [--server URL]"), console.log(" aiusage scan [--date YYYY-MM-DD] [--json]"), console.log(" aiusage report [--range 7d|1m|3m|all] [--detail] [--lang en|zh] [--no-emoji] [--json]"), console.log(" aiusage schedule [on|off|status] [--every 5m]"), console.log(" aiusage doctor"), console.log(" aiusage config set <key> <value...>"), console.log(""), console.log(`\u914D\u7F6E\u6587\u4EF6: ${getConfigPath()}${initialized ? "" : " (\u5C1A\u672A\u521D\u59CB\u5316)"}`);
|
|
2714
|
+
`), console.log("Usage: aiusage <command>"), console.log(""), console.log("Commands:"), console.log(" aiusage init [--device-id ID] [--device-name NAME] [--lookback N]"), console.log(" aiusage enroll --server URL --site-id ID --enroll-token TOKEN [--target NAME]"), console.log(" aiusage sync [--target NAME] [--date YYYY-MM-DD] [--from YYYY-MM-DD [--to YYYY-MM-DD]] [--lookback N] [--today]"), console.log(" aiusage health [--target NAME] [--server URL]"), console.log(" aiusage scan [--date YYYY-MM-DD] [--json]"), console.log(" aiusage report [--range 7d|1m|3m|all] [--detail] [--lang en|zh] [--no-emoji] [--json]"), console.log(" aiusage schedule [on|off|status] [--every 5m]"), console.log(" aiusage doctor"), console.log(" aiusage project [list]"), console.log(" aiusage project alias [<\u9879\u76EE\u540D> <\u522B\u540D>] [--remove <\u9879\u76EE\u540D>]"), console.log(" aiusage config set <key> <value...>"), console.log(""), console.log(`\u914D\u7F6E\u6587\u4EF6: ${getConfigPath()}${initialized ? "" : " (\u5C1A\u672A\u521D\u59CB\u5316)"}`);
|
|
2715
|
+
}
|
|
2716
|
+
function printUsageHint() {
|
|
2717
|
+
console.log(`aiusage v${getVersion()}
|
|
2718
|
+
`), console.log("\u5E38\u7528\u547D\u4EE4:"), console.log(" scan [--date YYYY-MM-DD] \u626B\u63CF\u67D0\u65E5\u7528\u91CF\u660E\u7EC6"), console.log(" report [--range 7d|1m|3m|all] \u672C\u5730\u7528\u91CF\u62A5\u544A"), console.log(" project [list] \u5217\u51FA\u672C\u673A\u6240\u6709\u9879\u76EE"), console.log(" project alias <\u540D\u79F0> <\u522B\u540D> \u8BBE\u7F6E\u9879\u76EE\u522B\u540D"), console.log(" sync \u4E0A\u4F20\u7528\u91CF\u5230\u670D\u52A1\u7AEF"), console.log(" schedule [on|off|status] \u5B9A\u65F6\u540C\u6B65"), console.log(" doctor \u8BCA\u65AD\u68C0\u67E5"), console.log(" config set <key> <value> \u4FEE\u6539\u914D\u7F6E"), console.log(""), console.log(`\u914D\u7F6E\u6587\u4EF6: ${getConfigPath()}`);
|
|
2472
2719
|
}
|
|
2473
2720
|
function parseArgs(args) {
|
|
2474
2721
|
let flags = {}, positionals = [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiusage/cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Track and sync AI tool token usage across devices.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"private": false,
|
|
@@ -40,12 +40,13 @@
|
|
|
40
40
|
"build": "rm -rf dist && node build.mjs",
|
|
41
41
|
"dev": "tsc --watch",
|
|
42
42
|
"lint": "tsc --noEmit",
|
|
43
|
-
"test": "
|
|
43
|
+
"test": "vitest run"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@aiusage/shared": "workspace:*",
|
|
47
47
|
"@types/node": "^22",
|
|
48
48
|
"esbuild": "^0.27.4",
|
|
49
|
-
"typescript": "^5"
|
|
49
|
+
"typescript": "^5",
|
|
50
|
+
"vitest": "^4.1.2"
|
|
50
51
|
}
|
|
51
52
|
}
|