1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.eventCreateExpoModule = exports.logEventAsync = exports.getTelemetryClient = void 0;
7const getUserState_1 = require("@expo/config/build/getUserState");
8const json_file_1 = __importDefault(require("@expo/json-file"));
9const rudder_sdk_node_1 = __importDefault(require("@expo/rudder-sdk-node"));
10const crypto_1 = __importDefault(require("crypto"));
11const getenv_1 = require("getenv");
12const os_1 = __importDefault(require("os"));
13const packageJson = require('../package.json');
14/** If telemetry is disabled by the user */
15const EXPO_NO_TELEMETRY = (0, getenv_1.boolish)('EXPO_NO_TELEMETRY', false);
16/** If the tool is running in a sanboxed environment, either staging or local envs */
17const EXPO_SANDBOX = (0, getenv_1.boolish)('EXPO_STAGING', false) || (0, getenv_1.boolish)('EXPO_LOCAL', false);
18/** The telemetry client instance to use */
19let client = null;
20/** The anonymous identity ID */
21let telemetryId = null;
22function getTelemetryClient() {
23    if (!client) {
24        client = new rudder_sdk_node_1.default(EXPO_SANDBOX ? '24TKICqYKilXM480mA7ktgVDdea' : '24TKR7CQAaGgIrLTgu3Fp4OdOkI', // expo unified,
25        'https://cdp.expo.dev/v1/batch', {
26            flushInterval: 300,
27        });
28        // Empty the telemetry queue on exit
29        process.on('SIGINT', () => client?.flush?.());
30        process.on('SIGTERM', () => client?.flush?.());
31    }
32    return client;
33}
34exports.getTelemetryClient = getTelemetryClient;
35/** Get the randomly generated anonymous ID from the persistent storage, see @expo/cli */
36async function getTelemetryIdAsync() {
37    const settings = new json_file_1.default((0, getUserState_1.getUserStatePath)(), {
38        ensureDir: true,
39        jsonParseErrorDefault: {},
40        cantReadFileDefault: {},
41    });
42    let id = await settings.getAsync('uuid', null);
43    if (!id) {
44        id = crypto_1.default.randomUUID();
45        await settings.setAsync('uuid', id);
46    }
47    return id;
48}
49function getTelemetryContext() {
50    const PLATFORM_NAMES = {
51        darwin: 'Mac',
52        win32: 'Windows',
53        linux: 'Linux',
54    };
55    return {
56        os: { name: PLATFORM_NAMES[os_1.default.platform()] ?? os_1.default.platform(), version: os_1.default.release() },
57        app: { name: 'create-expo-module', version: packageJson.version ?? undefined },
58    };
59}
60async function logEventAsync(event) {
61    if (EXPO_NO_TELEMETRY) {
62        return;
63    }
64    if (!telemetryId) {
65        telemetryId = await getTelemetryIdAsync();
66        getTelemetryClient().identify({ anonymousId: telemetryId });
67    }
68    const commonProperties = {
69        source: 'create-expo-module',
70        source_version: packageJson.version ?? undefined,
71    };
72    getTelemetryClient().track({
73        ...event,
74        properties: { ...event.properties, ...commonProperties },
75        anonymousId: telemetryId,
76        context: getTelemetryContext(),
77    });
78}
79exports.logEventAsync = logEventAsync;
80function eventCreateExpoModule(packageManager, options) {
81    return {
82        event: 'create expo module',
83        properties: {
84            nodeVersion: process.version,
85            packageManager,
86            withTemplate: !!options.source,
87            withReadme: options.withReadme,
88            withChangelog: options.withChangelog,
89            withExample: options.example,
90            local: !!options.local,
91        },
92    };
93}
94exports.eventCreateExpoModule = eventCreateExpoModule;
95//# sourceMappingURL=telemetry.js.map