mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-16 19:00:38 +09:00
feat: implement app-side telemetry sender
Add the shared telemetry sender, wire the CLI/server emit points, and cover the config and completion behavior with tests. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
parent
ca5659f734
commit
34044cdfce
29 changed files with 670 additions and 5 deletions
31
packages/shared/src/telemetry/state.ts
Normal file
31
packages/shared/src/telemetry/state.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import { randomUUID, randomBytes } from "node:crypto";
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import type { TelemetryState } from "./types.js";
|
||||
|
||||
export function loadOrCreateState(stateDir: string, version: string): TelemetryState {
|
||||
const filePath = path.join(stateDir, "state.json");
|
||||
|
||||
if (existsSync(filePath)) {
|
||||
try {
|
||||
const raw = readFileSync(filePath, "utf-8");
|
||||
const parsed = JSON.parse(raw) as TelemetryState;
|
||||
if (parsed.installId && parsed.salt) {
|
||||
return parsed;
|
||||
}
|
||||
} catch {
|
||||
// Corrupted state file — recreate
|
||||
}
|
||||
}
|
||||
|
||||
const state: TelemetryState = {
|
||||
installId: randomUUID(),
|
||||
salt: randomBytes(32).toString("hex"),
|
||||
createdAt: new Date().toISOString(),
|
||||
firstSeenVersion: version,
|
||||
};
|
||||
|
||||
mkdirSync(stateDir, { recursive: true });
|
||||
writeFileSync(filePath, JSON.stringify(state, null, 2) + "\n", "utf-8");
|
||||
return state;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue