shine-code-submit 1.0.5 → 1.0.7

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.
@@ -118,10 +118,18 @@ export interface CommitsResponse {
118
118
  // ---- GET /api/report:数据上报页用的跨项目聚合 ----
119
119
 
120
120
  /** 报告里单个会话的 token 明细。tokenTotal 读不到 transcript 为 null。 */
121
+ /** 代码变更行数(分开统计,三者不重复:added 纯增 / deleted 纯删 / modified 一删一加配对)。 */
122
+ export interface LinesStat {
123
+ added: number;
124
+ deleted: number;
125
+ modified: number;
126
+ }
127
+
121
128
  export interface ReportSession {
122
129
  sessionId: string;
123
130
  lastActive: number;
124
131
  tokenTotal: TokenUsage | null;
132
+ linesTotal: LinesStat | null;
125
133
  }
126
134
 
127
135
  /** 报告里单个项目(=cwd)的聚合行。 */
@@ -133,6 +141,7 @@ export interface ReportProject {
133
141
  sessionCount: number;
134
142
  sessions: ReportSession[]; // 每会话 token 明细
135
143
  totalTokens: TokenUsage; // 该项目 token 合计
144
+ totalLines: LinesStat; // 该项目代码变更行数合计
136
145
  gitError?: string;
137
146
  }
138
147
 
@@ -141,6 +150,7 @@ export interface ReportTotals {
141
150
  projects: number;
142
151
  sessions: number;
143
152
  tokens: TokenUsage;
153
+ lines: LinesStat;
144
154
  }
145
155
 
146
156
  /** GET /api/report 响应。 */
@@ -0,0 +1,73 @@
1
+ // 自动更新:查 npm registry 最新版本,有新版 spawn detached `npx shine-code-submit@latest install` 升级。
2
+ // daemon 启动时 + 定时调 autoUpdateIfNeeded。全程静默(try/catch),绝不影响 daemon。
3
+ //
4
+ // 升级链路:npx 拉 latest 包 → install CLI 部署新版到 cache + 注册 → startDaemonWithBun(1.0.5)
5
+ // 检测旧 daemon 版本不匹配 → stopDaemon 停旧 → 启新 daemon。daemon 退出不影响 npx install(detached)。
6
+ import { spawn } from "node:child_process";
7
+ import { SERVICE_NAME, SERVICE_VERSION } from "./config";
8
+ import { readSettings, writeSettings } from "../daemon/settings";
9
+
10
+ const REGISTRY_LATEST = `https://registry.npmjs.org/${SERVICE_NAME}/latest`;
11
+ const NPM_REGISTRY = "https://registry.npmjs.org/";
12
+
13
+ /** semver 大于:a > b 才 true(相等 false)。只升级不降级,避免本地比 npm 新(如发版前 build)时误降级。 */
14
+ function versionGt(a: string, b: string): boolean {
15
+ const pa = a.split(".").map((x) => parseInt(x, 10) || 0);
16
+ const pb = b.split(".").map((x) => parseInt(x, 10) || 0);
17
+ for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
18
+ const x = pa[i] ?? 0;
19
+ const y = pb[i] ?? 0;
20
+ if (x > y) return true;
21
+ if (x < y) return false;
22
+ }
23
+ return false;
24
+ }
25
+
26
+ export interface UpdateCheck {
27
+ latest?: string;
28
+ current: string;
29
+ hasUpdate: boolean;
30
+ }
31
+
32
+ /** 查 npm registry 最新版本。失败返回 hasUpdate:false(不抛)。 */
33
+ export async function checkForUpdate(): Promise<UpdateCheck> {
34
+ try {
35
+ const res = await fetch(REGISTRY_LATEST, { signal: AbortSignal.timeout(5000) });
36
+ if (!res.ok) return { current: SERVICE_VERSION, hasUpdate: false };
37
+ const data = (await res.json()) as { version?: string };
38
+ const latest = data.version;
39
+ if (!latest) return { current: SERVICE_VERSION, hasUpdate: false };
40
+ return { latest, current: SERVICE_VERSION, hasUpdate: versionGt(latest, SERVICE_VERSION) };
41
+ } catch {
42
+ return { current: SERVICE_VERSION, hasUpdate: false };
43
+ }
44
+ }
45
+
46
+ /**
47
+ * 自动更新:读 settings(autoUpdate 开关),有新版 spawn detached npx install。
48
+ * 返回 {updated, latest}。全程 try/catch 静默——绝不影响 daemon。
49
+ * - autoUpdate===false → 跳过。
50
+ * - 有新版 → spawn `npx --yes --registry=官方 shine-code-submit@latest install`(detached,不阻塞)。
51
+ * - 无论是否升级,都缓存 latestVersion 到 settings(dashboard 显示用)。
52
+ */
53
+ export async function autoUpdateIfNeeded(force = false): Promise<{ updated: boolean; latest?: string }> {
54
+ try {
55
+ const s = readSettings();
56
+ if (!force && s.autoUpdate === false) return { updated: false };
57
+ const check = await checkForUpdate();
58
+ // 缓存 latestVersion(dashboard 显示当前/最新版本)
59
+ if (check.latest && check.latest !== s.latestVersion) {
60
+ writeSettings({ ...s, latestVersion: check.latest });
61
+ }
62
+ if (!check.hasUpdate) return { updated: false, latest: check.latest };
63
+ // 有新版:spawn detached npx install(官方 registry 确保 latest,npmmirror 有同步延迟)
64
+ spawn(
65
+ "npx",
66
+ ["--yes", `--registry=${NPM_REGISTRY}`, `${SERVICE_NAME}@latest`, "install"],
67
+ { detached: true, stdio: "ignore", windowsHide: true, shell: true },
68
+ ).unref();
69
+ return { updated: true, latest: check.latest };
70
+ } catch {
71
+ return { updated: false };
72
+ }
73
+ }