|
| 1 | +import { Command } from "commander"; |
| 2 | +import dns from "dns"; |
| 3 | +import fs from "fs"; |
| 4 | +import { EventMessage, PostHog } from "posthog-node"; |
| 5 | +import { v4 as uuid } from "uuid"; |
| 6 | + |
| 7 | +import { getFullCommandPath } from "./commands/docs"; |
| 8 | +import { |
| 9 | + POSTHOG_API_KEY, |
| 10 | + POSTHOG_ENDPOINT, |
| 11 | + ZETACHAIN_CONFIG_FILE, |
| 12 | + ZETACHAIN_DIR, |
| 13 | +} from "./constants"; |
| 14 | + |
| 15 | +type Config = { |
| 16 | + uuid?: string; |
| 17 | +}; |
| 18 | + |
| 19 | +const getOrCreateUserUUID = (): string => { |
| 20 | + try { |
| 21 | + fs.mkdirSync(ZETACHAIN_DIR, { recursive: true }); |
| 22 | + } catch { |
| 23 | + // Silently continue - analytics will work without persistent config |
| 24 | + } |
| 25 | + |
| 26 | + let data: Config = {}; |
| 27 | + let needsWrite = false; |
| 28 | + |
| 29 | + if (fs.existsSync(ZETACHAIN_CONFIG_FILE)) { |
| 30 | + try { |
| 31 | + const raw = fs.readFileSync(ZETACHAIN_CONFIG_FILE, "utf8"); |
| 32 | + if (raw.trim()) data = JSON.parse(raw) as Config; |
| 33 | + } catch (err) { |
| 34 | + // Silently recreate config if corrupted |
| 35 | + data = {}; |
| 36 | + needsWrite = true; |
| 37 | + } |
| 38 | + } else { |
| 39 | + needsWrite = true; |
| 40 | + } |
| 41 | + |
| 42 | + if (!data || typeof data !== "object") { |
| 43 | + data = {}; |
| 44 | + needsWrite = true; |
| 45 | + } |
| 46 | + |
| 47 | + if (!data.uuid || typeof data.uuid !== "string" || data.uuid.length === 0) { |
| 48 | + data.uuid = uuid(); |
| 49 | + needsWrite = true; |
| 50 | + } |
| 51 | + |
| 52 | + if (needsWrite) { |
| 53 | + try { |
| 54 | + fs.writeFileSync( |
| 55 | + ZETACHAIN_CONFIG_FILE, |
| 56 | + JSON.stringify(data, null, 2), |
| 57 | + "utf8", |
| 58 | + ); |
| 59 | + } catch { |
| 60 | + // Silently continue - will generate new UUID next time |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return data.uuid as string; |
| 65 | +}; |
| 66 | + |
| 67 | +const canResolveHost = async ( |
| 68 | + hostname: string, |
| 69 | + timeout = 300, |
| 70 | +): Promise<boolean> => { |
| 71 | + try { |
| 72 | + const resolve = dns.promises.lookup(hostname); |
| 73 | + const timeoutPromise = new Promise<never>((_, reject) => |
| 74 | + setTimeout(() => reject(new Error("dns-timeout")), timeout), |
| 75 | + ); |
| 76 | + await Promise.race([resolve, timeoutPromise]); |
| 77 | + return true; |
| 78 | + } catch (_err) { |
| 79 | + return false; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +export const setupAnalytics = (program: Command) => { |
| 84 | + program.hook("preAction", async (_thisCommand, actionCommand) => { |
| 85 | + const opts = program.opts(); |
| 86 | + if (opts && opts.analytics === false) return; |
| 87 | + |
| 88 | + // Skip analytics if the PostHog host cannot be resolved (offline). |
| 89 | + const host = new URL(POSTHOG_ENDPOINT).hostname; |
| 90 | + const canResolve = await canResolveHost(host); |
| 91 | + if (!canResolve) return; |
| 92 | + |
| 93 | + let analytics: PostHog | null = null; |
| 94 | + try { |
| 95 | + analytics = new PostHog(POSTHOG_API_KEY, { |
| 96 | + host: POSTHOG_ENDPOINT, |
| 97 | + }); |
| 98 | + analytics.on("error", () => {}); |
| 99 | + |
| 100 | + const event: EventMessage = { |
| 101 | + distinctId: getOrCreateUserUUID(), |
| 102 | + event: "ZetaChain CLI command executed", |
| 103 | + properties: { |
| 104 | + command: getFullCommandPath(actionCommand), |
| 105 | + }, |
| 106 | + }; |
| 107 | + analytics.capture(event); |
| 108 | + |
| 109 | + await analytics.shutdown(); |
| 110 | + } catch { |
| 111 | + // Skip analytics errors (e.g. offline / network failures), to prevent CLI disruption |
| 112 | + } |
| 113 | + }); |
| 114 | +}; |
0 commit comments