UNPKG

eas-cli

Version:
190 lines (189 loc) 9.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAnalyticsAsync = exports.getAnalyticsEnabledAsync = exports.setAnalyticsEnabledAsync = exports.MetadataEvent = exports.BuildEvent = exports.SubmissionEvent = exports.CommandEvent = void 0; const tslib_1 = require("tslib"); const rudder_sdk_node_1 = tslib_1.__importDefault(require("@expo/rudder-sdk-node")); const os_1 = tslib_1.__importDefault(require("os")); const url_1 = require("url"); const uuid_1 = require("uuid"); const User_1 = require("../user/User"); const UserSettings_1 = tslib_1.__importDefault(require("../user/UserSettings")); const easCli_1 = require("../utils/easCli"); const PLATFORM_TO_ANALYTICS_PLATFORM = { darwin: 'Mac', win32: 'Windows', linux: 'Linux', }; var CommandEvent; (function (CommandEvent) { CommandEvent["ACTION"] = "action"; })(CommandEvent || (exports.CommandEvent = CommandEvent = {})); var SubmissionEvent; (function (SubmissionEvent) { SubmissionEvent["SUBMIT_COMMAND"] = "submit cli submit command"; SubmissionEvent["SUBMIT_COMMAND_ATTEMPT"] = "submit cli attempt"; SubmissionEvent["SUBMIT_COMMAND_SUCCESS"] = "submit cli success"; SubmissionEvent["SUBMIT_COMMAND_FAIL"] = "submit cli fail"; SubmissionEvent["GATHER_CREDENTIALS_ATTEMPT"] = "submit cli gather credentials attempt"; SubmissionEvent["GATHER_CREDENTIALS_SUCCESS"] = "submit cli gather credentials success"; SubmissionEvent["GATHER_CREDENTIALS_FAIL"] = "submit cli gather credentials fail"; SubmissionEvent["GATHER_ARCHIVE_ATTEMPT"] = "submit cli gather archive attempt"; SubmissionEvent["GATHER_ARCHIVE_SUCCESS"] = "submit cli gather archive success"; SubmissionEvent["GATHER_ARCHIVE_FAIL"] = "submit cli gather archive fail"; SubmissionEvent["SUBMIT_REQUEST_ATTEMPT"] = "submit cli request attempt"; SubmissionEvent["SUBMIT_REQUEST_SUCCESS"] = "submit cli request success"; SubmissionEvent["SUBMIT_REQUEST_FAIL"] = "submit cli request fail"; SubmissionEvent["API_KEY_DOWNLOAD_FAIL"] = "submit cli credentials api key download fail"; SubmissionEvent["API_KEY_DOWNLOAD_RETRY"] = "submit cli credentials api key download fail temporary"; SubmissionEvent["API_KEY_DOWNLOAD_SUCCESS"] = "submit cli credentials api key download succeed"; })(SubmissionEvent || (exports.SubmissionEvent = SubmissionEvent = {})); var BuildEvent; (function (BuildEvent) { BuildEvent["BUILD_COMMAND"] = "build cli build command"; BuildEvent["PROJECT_UPLOAD_ATTEMPT"] = "build cli project upload attempt"; BuildEvent["PROJECT_UPLOAD_SUCCESS"] = "build cli project upload success"; BuildEvent["PROJECT_UPLOAD_FAIL"] = "build cli project upload fail"; BuildEvent["GATHER_CREDENTIALS_ATTEMPT"] = "build cli gather credentials attempt"; BuildEvent["GATHER_CREDENTIALS_SUCCESS"] = "build cli gather credentials success"; BuildEvent["GATHER_CREDENTIALS_FAIL"] = "build cli gather credentials fail"; BuildEvent["CONFIGURE_PROJECT_ATTEMPT"] = "build cli configure project attempt"; BuildEvent["CONFIGURE_PROJECT_SUCCESS"] = "build cli configure project success"; BuildEvent["CONFIGURE_PROJECT_FAIL"] = "build cli configure project fail"; BuildEvent["BUILD_REQUEST_ATTEMPT"] = "build cli build request attempt"; BuildEvent["BUILD_REQUEST_SUCCESS"] = "build cli build request success"; BuildEvent["BUILD_REQUEST_FAIL"] = "build cli build request fail"; BuildEvent["BUILD_STATUS_COMMAND"] = "build cli build status"; BuildEvent["CREDENTIALS_SYNC_COMMAND"] = "build cli credentials sync command"; BuildEvent["CREDENTIALS_SYNC_UPDATE_LOCAL_ATTEMPT"] = "build cli credentials sync update local attempt"; BuildEvent["CREDENTIALS_SYNC_UPDATE_LOCAL_SUCCESS"] = "build cli credentials sync update local success"; BuildEvent["CREDENTIALS_SYNC_UPDATE_LOCAL_FAIL"] = "build cli credentials sync update local fail"; BuildEvent["CREDENTIALS_SYNC_UPDATE_REMOTE_ATTEMPT"] = "build cli credentials sync update remote attempt"; BuildEvent["CREDENTIALS_SYNC_UPDATE_REMOTE_SUCCESS"] = "build cli credentials sync update remote success"; BuildEvent["CREDENTIALS_SYNC_UPDATE_REMOTE_FAIL"] = "build cli credentials sync update remote fail"; BuildEvent["ANDROID_KEYSTORE_CREATE"] = "build cli credentials keystore create"; })(BuildEvent || (exports.BuildEvent = BuildEvent = {})); var MetadataEvent; (function (MetadataEvent) { MetadataEvent["APPLE_METADATA_DOWNLOAD"] = "metadata cli download apple response"; MetadataEvent["APPLE_METADATA_UPLOAD"] = "metadata cli upload apple response"; })(MetadataEvent || (exports.MetadataEvent = MetadataEvent = {})); const USER_SETTINGS_KEY_AMPLITUDE_ENABLED = 'amplitudeEnabled'; const USER_SETTINGS_KEY_AMPLITUDE_DEVICE_ID = 'amplitudeDeviceId'; const USER_SETTINGS_KEY_ANALYTICS_ENABLED = 'analyticsEnabled'; const USER_SETTINGS_KEY_ANALYTICS_DEVICE_ID = 'analyticsDeviceId'; /** * Sets the user's analytics enabled preference. Note that this will only take effect * upon the next run of the CLI. */ async function setAnalyticsEnabledAsync(enabled) { await UserSettings_1.default.setAsync(USER_SETTINGS_KEY_ANALYTICS_ENABLED, enabled); } exports.setAnalyticsEnabledAsync = setAnalyticsEnabledAsync; /** * Returns the user's analytics enabled preference. */ async function getAnalyticsEnabledAsync() { const analyticsEnabled = await UserSettings_1.default.getAsync(USER_SETTINGS_KEY_ANALYTICS_ENABLED, null); return !!analyticsEnabled; } exports.getAnalyticsEnabledAsync = getAnalyticsEnabledAsync; /** * Create an instance of Analytics based on the user's analytics enabled preferences. */ async function createAnalyticsAsync() { // TODO: remove after some time const amplitudeEnabled = await UserSettings_1.default.getAsync(USER_SETTINGS_KEY_AMPLITUDE_ENABLED, null); if (amplitudeEnabled !== null) { await UserSettings_1.default.setAsync(USER_SETTINGS_KEY_ANALYTICS_ENABLED, amplitudeEnabled); await UserSettings_1.default.deleteKeyAsync(USER_SETTINGS_KEY_AMPLITUDE_ENABLED); } const amplitudeDeviceId = await UserSettings_1.default.getAsync(USER_SETTINGS_KEY_AMPLITUDE_DEVICE_ID, null); if (amplitudeDeviceId !== null) { await UserSettings_1.default.setAsync(USER_SETTINGS_KEY_ANALYTICS_DEVICE_ID, amplitudeDeviceId); await UserSettings_1.default.deleteKeyAsync(USER_SETTINGS_KEY_AMPLITUDE_DEVICE_ID); } if (process.env.DISABLE_EAS_ANALYTICS) { await UserSettings_1.default.setAsync(USER_SETTINGS_KEY_ANALYTICS_ENABLED, false); } const analyticsEnabled = !process.env.https_proxy && // disable analytics if running behind proxy (await UserSettings_1.default.getAsync(USER_SETTINGS_KEY_ANALYTICS_ENABLED, true)); if (!analyticsEnabled) { return new NoOpAnalytics(); } const persistedDeviceId = await UserSettings_1.default.getAsync(USER_SETTINGS_KEY_ANALYTICS_DEVICE_ID, null); const deviceId = persistedDeviceId ?? (0, uuid_1.v4)(); if (!persistedDeviceId) { await UserSettings_1.default.setAsync(USER_SETTINGS_KEY_ANALYTICS_DEVICE_ID, deviceId); } return new RudderstackAnalytics(deviceId); } exports.createAnalyticsAsync = createAnalyticsAsync; class NoOpAnalytics { logEvent() { } setActor() { } async flushAsync() { } } const RudderstackAnalyticsConfig = process.env.EXPO_STAGING || process.env.EXPO_LOCAL ? { // staging environment rudderstackWriteKey: '1wpX20Da4ltFGSXbPFYUL00Chb7', rudderstackDataPlaneURL: 'https://cdp.expo.dev', } : { // prod environment rudderstackWriteKey: '1wpXLFxmujq86etH6G6cc90hPcC', rudderstackDataPlaneURL: 'https://cdp.expo.dev', }; class RudderstackAnalytics { persistentDeviceId; rudderstackClient = new rudder_sdk_node_1.default(RudderstackAnalyticsConfig.rudderstackWriteKey, new url_1.URL('/v1/batch', RudderstackAnalyticsConfig.rudderstackDataPlaneURL).toString(), { flushInterval: 300, }); identifiedActor = null; constructor(persistentDeviceId) { this.persistentDeviceId = persistentDeviceId; // identify once with just anonymous ID. Once the actor is fetched, re-indentify with // both so that they can be associated this.rudderstackClient.identify({ anonymousId: persistentDeviceId, }); } setActor(actor) { if (this.identifiedActor) { return; } this.rudderstackClient.identify({ userId: actor.id, anonymousId: this.persistentDeviceId, traits: { username: (0, User_1.getActorDisplayName)(actor), user_id: actor.id, user_type: actor.__typename, }, }); this.identifiedActor = actor; } logEvent(name, properties) { const userId = this.identifiedActor?.id; const deviceId = this.persistentDeviceId; const commonEventProperties = { source_version: easCli_1.easCliVersion, source: 'eas cli' }; const identity = { userId: userId ?? undefined, anonymousId: deviceId ?? (0, uuid_1.v4)() }; this.rudderstackClient.track({ event: name, properties: { ...properties, ...commonEventProperties }, ...identity, context: this.getRudderStackContext(), }); } async flushAsync() { await this.rudderstackClient.flush(); } getRudderStackContext() { const platform = PLATFORM_TO_ANALYTICS_PLATFORM[os_1.default.platform()] || os_1.default.platform(); return { os: { name: platform, version: os_1.default.release() }, device: { type: platform, model: platform }, app: { name: 'eas cli', version: easCli_1.easCliVersion ?? undefined }, }; } }