nexus/packages/shared/src/telemetry/state.ts
dotta 34044cdfce 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>
2026-04-02 10:47:29 -05:00

31 lines
968 B
TypeScript

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;
}