UNPKG

balena-cli

Version:

The official balena Command Line Interface

87 lines 3.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.trackCommand = trackCommand; const packageJSON = require("../package.json"); const lazy_1 = require("./utils/lazy"); async function trackCommand(commandSignature) { var _a; try { let Sentry; if (!process.env.BALENARC_NO_SENTRY) { Sentry = await Promise.resolve().then(() => require('@sentry/node')); Sentry.configureScope((scope) => { scope.setExtra('command', commandSignature); }); } const { getCachedUsername } = await Promise.resolve().then(() => require('./utils/bootstrap')); let username; try { username = (_a = (await getCachedUsername())) === null || _a === void 0 ? void 0 : _a.username; } catch (_b) { } if (!process.env.BALENARC_NO_SENTRY) { Sentry.configureScope((scope) => { scope.setUser({ id: username, username, }); }); } if (!process.env.BALENA_CLI_TEST_TYPE && !process.env.BALENARC_NO_ANALYTICS) { const settings = await Promise.resolve().then(() => require('balena-settings-client')); const balenaUrl = settings.get('balenaUrl'); await sendEvent(balenaUrl, `[CLI] ${commandSignature}`, username); } } catch (_c) { } } const TIMEOUT = 4000; async function sendEvent(balenaUrl, event, username) { var _a; const { default: got } = await Promise.resolve().then(() => require('got')); const trackData = { api_key: 'balena-main', events: [ { event_type: event, user_id: username, version_name: packageJSON.version, event_properties: { balenaUrl, arch: process.arch, platform: process.platform, node: process.version, }, }, ], }; const url = `https://data.${balenaUrl}/amplitude/2/httpapi`; try { await got.post(url, { json: trackData, retry: 0, timeout: { request: TIMEOUT, response: 0, }, }); } catch (e) { if (process.env.DEBUG) { console.error(`[debug] Event tracking error: ${e.message || e}`); } if (e instanceof got.TimeoutError && TIMEOUT < ((_a = e.timings.phases.total) !== null && _a !== void 0 ? _a : 0)) { console.error((0, lazy_1.stripIndent) ` Timeout submitting analytics event to balenaCloud/openBalena. If you are using the balena CLI in an air-gapped environment with a filtered internet connection, set the BALENARC_OFFLINE_MODE=1 environment variable when using CLI commands that do not strictly require access to balenaCloud. `); } } } //# sourceMappingURL=events.js.map