@juejin-opensource/jusage 0.1.6 → 0.1.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.
Files changed (38) hide show
  1. package/README.md +1 -0
  2. package/dist/args.d.ts +20 -0
  3. package/dist/args.js +121 -0
  4. package/dist/daemon.d.ts +2 -2
  5. package/dist/daemon.js +8 -7
  6. package/dist/dashboard/assets/click-idle-CXAVCmiH.png +0 -0
  7. package/dist/dashboard/assets/dashboard-BcYn7CaU.js +55 -0
  8. package/dist/dashboard/assets/download-CteP_gYs.js +6 -0
  9. package/dist/dashboard/assets/downloads-dKq4xNjC.js +1 -0
  10. package/dist/dashboard/assets/hawking-idle-QoXt5bST.png +0 -0
  11. package/dist/dashboard/assets/index-BfeJy_UY.js +1 -0
  12. package/dist/dashboard/assets/index-CWwN4M6h.css +1 -0
  13. package/dist/dashboard/assets/{index-C-ulpYDE.js → index-flWmwEjH.js} +31 -31
  14. package/dist/dashboard/assets/model-provider-0f_aGOIG.js +1 -0
  15. package/dist/dashboard/assets/pricing-OObJBgJw.js +1 -0
  16. package/dist/dashboard/assets/rank-CuAJ5i73.js +16 -0
  17. package/dist/dashboard/assets/{settings-Cud6Sr27.js → settings-KbZOVhnT.js} +1 -1
  18. package/dist/dashboard/assets/useLocalStorage-BdF6kE3i.js +1 -0
  19. package/dist/dashboard/assets/yoyo-idle-DDvV3ieO.png +0 -0
  20. package/dist/dashboard/captain-wechat.jpg +0 -0
  21. package/dist/dashboard/index.html +2 -2
  22. package/dist/index.d.ts +1 -10
  23. package/dist/index.js +21 -93
  24. package/dist/service.d.ts +4 -1
  25. package/dist/service.js +19 -8
  26. package/package.json +3 -3
  27. package/dist/dashboard/assets/ChevronDown-hI5eRGzD.js +0 -1
  28. package/dist/dashboard/assets/click-idle-6pQvdt8N.png +0 -0
  29. package/dist/dashboard/assets/dashboard-CAkIFa6U.js +0 -55
  30. package/dist/dashboard/assets/download-ByMnRiaK.js +0 -1
  31. package/dist/dashboard/assets/downloads-C3JkgIXC.js +0 -1
  32. package/dist/dashboard/assets/env-Cw7aSECu.js +0 -1
  33. package/dist/dashboard/assets/hawking-idle-C6M3ZvDm.png +0 -0
  34. package/dist/dashboard/assets/index-CeqvL8Jz.js +0 -1
  35. package/dist/dashboard/assets/index-CmpX7J2H.css +0 -1
  36. package/dist/dashboard/assets/pricing-J0aEt4op.js +0 -1
  37. package/dist/dashboard/assets/rank-DNcLrJpg.js +0 -16
  38. package/dist/dashboard/assets/yoyo-idle-DmXh5tLT.png +0 -0
package/README.md CHANGED
@@ -55,6 +55,7 @@ jusage start # 或直接 jusage
55
55
  ```bash
56
56
  jusage service start
57
57
  jusage start --port 8452
58
+ jusage start --host 0.0.0.0 # 局域网可访问;默认 127.0.0.1
58
59
  jusage sync --source=claude # claude | codex | cursor | all
59
60
  jusage upload --force # 忽略云端同步开关,强制上报
60
61
  jusage upload --reconcile # 全量对账后上报
package/dist/args.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ export declare const DEFAULT_HOST = "127.0.0.1";
2
+ export declare function isWildcardListenHost(host: string): boolean;
3
+ export declare function formatListenUrl(host: string, port: number): string;
4
+ export interface ParsedArgs {
5
+ command: string;
6
+ serviceAction?: string;
7
+ /** Set only when --port is passed. */
8
+ port?: number;
9
+ /** Set only when --host is passed. */
10
+ host?: string;
11
+ source?: string;
12
+ force?: boolean;
13
+ reconcile?: boolean;
14
+ /** Hidden debug: seed statsSince to N days ago when missing. Not shown in --help. */
15
+ days?: number;
16
+ }
17
+ export declare function normalizeListenHost(raw: string): string;
18
+ export declare function parseArgs(argv: string[]): ParsedArgs;
19
+ export declare function resolveDaysAgo(days: number | undefined): number | undefined;
20
+ export declare function printHelp(): void;
package/dist/args.js ADDED
@@ -0,0 +1,121 @@
1
+ import { DEFAULT_PORT } from '@juejin-opensource/jusage-core';
2
+ export const DEFAULT_HOST = '127.0.0.1';
3
+ export function isWildcardListenHost(host) {
4
+ return host === '0.0.0.0' || host === '::';
5
+ }
6
+ export function formatListenUrl(host, port) {
7
+ const display = isWildcardListenHost(host) ? DEFAULT_HOST : host;
8
+ const formatted = display.includes(':') && !display.startsWith('[') ? `[${display}]` : display;
9
+ return `http://${formatted}:${port}`;
10
+ }
11
+ export function normalizeListenHost(raw) {
12
+ const host = raw.trim();
13
+ if (!host) {
14
+ throw new Error('--host 不能为空,例如 0.0.0.0 或 127.0.0.1');
15
+ }
16
+ if (/[\s/?#]/.test(host)) {
17
+ throw new Error(`无效的 --host: ${raw}`);
18
+ }
19
+ return host;
20
+ }
21
+ function isFlagToken(value) {
22
+ return value != null && value.startsWith('-') && value !== '-';
23
+ }
24
+ export function parseArgs(argv) {
25
+ const args = argv.slice(2);
26
+ let command = args[0] ?? 'start';
27
+ let serviceAction;
28
+ let port;
29
+ let host;
30
+ let source;
31
+ let force = false;
32
+ let reconcile = false;
33
+ let days;
34
+ if (command === 'help' ||
35
+ command === '--help' ||
36
+ command === '-h' ||
37
+ args.includes('--help') ||
38
+ args.includes('-h')) {
39
+ return { command: 'help', port, host, source, force, reconcile };
40
+ }
41
+ if (command === 'service') {
42
+ serviceAction = args[1];
43
+ }
44
+ for (let i = 0; i < args.length; i++) {
45
+ if (args[i] === '--port' && args[i + 1]) {
46
+ port = Number(args[i + 1]) || DEFAULT_PORT;
47
+ i += 1;
48
+ }
49
+ else if (args[i] === '--host') {
50
+ if (isFlagToken(args[i + 1]) || args[i + 1] == null) {
51
+ throw new Error('--host 需要地址,例如 0.0.0.0 或 127.0.0.1');
52
+ }
53
+ host = normalizeListenHost(args[i + 1]);
54
+ i += 1;
55
+ }
56
+ else if (args[i]?.startsWith('--host=')) {
57
+ host = normalizeListenHost(args[i].slice('--host='.length));
58
+ }
59
+ else if (args[i] === '--source' && args[i + 1]) {
60
+ source = args[i + 1];
61
+ i += 1;
62
+ }
63
+ else if (args[i]?.startsWith('--source=')) {
64
+ source = args[i].slice('--source='.length);
65
+ }
66
+ else if (args[i] === '--force') {
67
+ force = true;
68
+ }
69
+ else if (args[i] === '--reconcile') {
70
+ reconcile = true;
71
+ }
72
+ else if (args[i] === '--days' && args[i + 1]) {
73
+ days = Number(args[i + 1]);
74
+ i += 1;
75
+ }
76
+ else if (args[i]?.startsWith('--days=')) {
77
+ days = Number(args[i].slice('--days='.length));
78
+ }
79
+ }
80
+ if (command.startsWith('-'))
81
+ command = 'start';
82
+ return { command, serviceAction, port, host, source, force, reconcile, days };
83
+ }
84
+ export function resolveDaysAgo(days) {
85
+ if (days === undefined)
86
+ return undefined;
87
+ if (!Number.isInteger(days) || days <= 0) {
88
+ throw new Error(`--days 须为正整数,收到: ${days}`);
89
+ }
90
+ return days;
91
+ }
92
+ export function printHelp() {
93
+ console.log(`Usage: jusage <command> [options]
94
+
95
+ Commands:
96
+ start 前台启动本地面板与同步(默认;桌面已运行时为观察模式)
97
+ stop 停止当前进程内的服务
98
+ status 查看 CLI / 面板当前启动状态
99
+ sync 手动同步本地用量数据
100
+ upload 上报数据到云端
101
+ service <action> 后台服务与开机自启(macOS / Windows / Linux)
102
+ action: start | stop | status
103
+ help 显示帮助
104
+
105
+ Options:
106
+ --port <number> 面板端口(默认 ${DEFAULT_PORT})
107
+ --host <address> 面板监听地址(默认 ${DEFAULT_HOST};局域网访问用 0.0.0.0)
108
+ --source <name> sync 数据源:claude | codex | cursor | qoder | trae | gemini | opencode | copilot | antigravity | openclaw | hermes | zcode | pi | kimi | roocode | droid | kiro | cline | amp | qwen | codebuddy | workbuddy | grok | mimo | every-code | omp | kilo-cli | kilocode | goose | zed | warp | all
109
+ --force upload 时忽略云端同步开关,强制上报
110
+ --reconcile upload 时做全量对账
111
+ -h, --help 显示帮助
112
+
113
+ Examples:
114
+ jusage
115
+ jusage start --port 8452
116
+ jusage start --host 0.0.0.0
117
+ jusage status
118
+ jusage sync --source=claude
119
+ jusage upload --force
120
+ jusage service start`);
121
+ }
package/dist/daemon.d.ts CHANGED
@@ -3,14 +3,14 @@ export { clearPid, getRunningOwner, getRunningPid, isPidAlive, pidFilePath, read
3
3
  export declare function daemonLogPath(dataDir?: string): string;
4
4
  export declare function ensureDaemonLogDir(dataDir?: string): Promise<string>;
5
5
  /** True when local-api `/health` returns `{ ok: true }`. */
6
- export declare function probeLocalHealth(port: number): Promise<boolean>;
6
+ export declare function probeLocalHealth(port: number, host?: string): Promise<boolean>;
7
7
  export declare function waitForPid(dataDir?: string, timeoutMs?: number): Promise<number | null>;
8
8
  export interface ServiceReady {
9
9
  pid: number | null;
10
10
  health: boolean;
11
11
  }
12
12
  /** Wait until the pid file is live, or `/health` is up as a fallback. */
13
- export declare function waitForServiceReady(dataDir: string, port: number, timeoutMs?: number): Promise<ServiceReady>;
13
+ export declare function waitForServiceReady(dataDir: string, port: number, host?: string, timeoutMs?: number): Promise<ServiceReady>;
14
14
  export declare function readDaemonLogTail(dataDir?: string, maxChars?: number): Promise<string>;
15
15
  export declare function resolveServiceCommand(cliBinPath: string): {
16
16
  nodePath: string;
package/dist/daemon.js CHANGED
@@ -3,6 +3,7 @@ import { realpathSync } from 'node:fs';
3
3
  import { mkdir, readFile } from 'node:fs/promises';
4
4
  import { join } from 'node:path';
5
5
  import { DEFAULT_DATA_DIR, argsMatchRuntimeKind, logsDir, clearPid, getRunningOwner, getRunningPid, isPidAlive, pidFilePath, readPid, readProcessArgs, readRuntimeOwner, stopPid, writePid, writeRuntimeOwner, } from '@juejin-opensource/jusage-core';
6
+ import { DEFAULT_HOST, formatListenUrl } from './args.js';
6
7
  export { clearPid, getRunningOwner, getRunningPid, isPidAlive, pidFilePath, readPid, readRuntimeOwner, stopPid, writePid, writeRuntimeOwner, };
7
8
  export function daemonLogPath(dataDir = DEFAULT_DATA_DIR) {
8
9
  return join(logsDir(dataDir), 'daemon.log');
@@ -21,9 +22,9 @@ async function readLivePid(dataDir) {
21
22
  return null;
22
23
  }
23
24
  /** True when local-api `/health` returns `{ ok: true }`. */
24
- export async function probeLocalHealth(port) {
25
+ export async function probeLocalHealth(port, host = DEFAULT_HOST) {
25
26
  try {
26
- const res = await fetch(`http://127.0.0.1:${port}/health`, {
27
+ const res = await fetch(`${formatListenUrl(host, port)}/health`, {
27
28
  signal: AbortSignal.timeout(400),
28
29
  });
29
30
  if (!res.ok)
@@ -83,21 +84,21 @@ export async function waitForPid(dataDir = DEFAULT_DATA_DIR, timeoutMs = 15_000)
83
84
  return getRunningPid(dataDir);
84
85
  }
85
86
  /** Wait until the pid file is live, or `/health` is up as a fallback. */
86
- export async function waitForServiceReady(dataDir, port, timeoutMs = 15_000) {
87
+ export async function waitForServiceReady(dataDir, port, host = DEFAULT_HOST, timeoutMs = 15_000) {
87
88
  const deadline = Date.now() + timeoutMs;
88
89
  while (Date.now() < deadline) {
89
90
  const pid = await readLivePid(dataDir);
90
91
  if (pid != null)
91
- return { pid, health: await probeLocalHealth(port) };
92
- if (await probeLocalHealth(port)) {
92
+ return { pid, health: await probeLocalHealth(port, host) };
93
+ if (await probeLocalHealth(port, host)) {
93
94
  return { pid: await recoverPidFromHealth(dataDir, port), health: true };
94
95
  }
95
96
  await new Promise((r) => setTimeout(r, 200));
96
97
  }
97
98
  const pid = (await readLivePid(dataDir)) ?? (await getRunningPid(dataDir));
98
99
  if (pid != null)
99
- return { pid, health: await probeLocalHealth(port) };
100
- if (await probeLocalHealth(port)) {
100
+ return { pid, health: await probeLocalHealth(port, host) };
101
+ if (await probeLocalHealth(port, host)) {
101
102
  return { pid: await recoverPidFromHealth(dataDir, port), health: true };
102
103
  }
103
104
  return { pid: null, health: false };