@omlet/cli
Version:
Omlet (https://omlet.dev) is a component analytics tool that uses a CLI to scan your codebase to detect components and their usage. Get real usage insights from customizable charts to measure adoption across all projects and identify opportunities to impr
133 lines (132 loc) • 4.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.trackCommandFailedError = exports.trackLoginFailedError = exports.trackInitInCIError = exports.trackProjectSetupValidationError = exports.trackConfigLoadingError = exports.trackAnalyzeError = exports.trackInitSecondScanError = exports.trackInitSecondScan = exports.trackInitAfterAnalyzeError = exports.trackAnalysisLimitReachedError = exports.trackAnalyzeBeforeInitError = exports.trackHookScript = exports.trackLogin = exports.setDefaultProps = exports.Command = void 0;
const crypto_1 = require("crypto");
const node_machine_id_1 = require("node-machine-id");
const apiClient_1 = require("./apiClient");
const ciUtils_1 = require("./ciUtils");
const config_1 = require("./config");
const logger_1 = require("./logger");
const npmUtils_1 = require("./npmUtils");
function getDeviceId() {
try {
return (0, node_machine_id_1.machineIdSync)();
}
catch (e) {
logger_1.logger.debug(`Failed to get device ID: ${e}`);
return `cli-${Date.now()}-${(0, crypto_1.randomBytes)(32).toString("hex")}`;
}
}
function initClient() {
if (config_1.TRACKER_DEBUG_MODE) {
return {
track(eventName, properties) {
if (!config_1.TRACKER_ENABLED) {
return;
}
logger_1.logger.debug(`[ANALYTICS]: ${eventName} ${JSON.stringify(properties, null, 2)}`);
},
};
}
return {
track(eventName, properties) {
if (!config_1.TRACKER_ENABLED) {
return;
}
// We should fire and forget tracking requests without waiting for a response
// It's likely that logger becomes unavailable before the request is finished
(0, apiClient_1.trackEvent)(eventName, properties).catch(() => {
// Silence errors
});
},
};
}
const client = initClient();
var Command;
(function (Command) {
Command["init"] = "init";
Command["analyze"] = "analyze";
Command["login"] = "login";
Command["none"] = "none";
})(Command = exports.Command || (exports.Command = {}));
function trackEvent(name, props = {}) {
client.track(name, {
...defaultProps,
...props,
});
}
const defaultProps = {
deviceId: getDeviceId(),
cliVersion: (0, npmUtils_1.getCliVersion)(),
distinct_id: getDeviceId(),
command: Command.none,
ci_vendor: ciUtils_1.ciVendor,
};
function setDefaultProps(props) {
Object.assign(defaultProps, props);
}
exports.setDefaultProps = setDefaultProps;
function trackLogin() {
trackEvent("CLI: Logged in");
}
exports.trackLogin = trackLogin;
function trackHookScript() {
trackEvent("CLI: Provided hook script");
}
exports.trackHookScript = trackHookScript;
function trackAnalyzeBeforeInitError() {
trackEvent("CLI: Ran analyze command before init");
}
exports.trackAnalyzeBeforeInitError = trackAnalyzeBeforeInitError;
function trackAnalysisLimitReachedError() {
trackEvent("CLI: Reached analysis limit");
}
exports.trackAnalysisLimitReachedError = trackAnalysisLimitReachedError;
function trackInitAfterAnalyzeError() {
trackEvent("CLI: Ran init command after analyze");
}
exports.trackInitAfterAnalyzeError = trackInitAfterAnalyzeError;
function trackInitSecondScan() {
trackEvent("CLI: Scanned secondary repository in init command");
}
exports.trackInitSecondScan = trackInitSecondScan;
function trackInitSecondScanError(errorMessage) {
trackEvent("CLI: Secondary scan failed in init command", {
error: errorMessage,
});
}
exports.trackInitSecondScanError = trackInitSecondScanError;
function trackAnalyzeError(errorMessage, props) {
trackEvent("CLI: Scan failed", {
error: errorMessage,
...props,
});
}
exports.trackAnalyzeError = trackAnalyzeError;
function trackConfigLoadingError(errorMessage) {
trackEvent("CLI: Failed to load config", {
error: errorMessage,
});
}
exports.trackConfigLoadingError = trackConfigLoadingError;
function trackProjectSetupValidationError(errorMessage, props) {
trackEvent("CLI: Project setup validation failed", {
error: errorMessage,
...props,
});
}
exports.trackProjectSetupValidationError = trackProjectSetupValidationError;
function trackInitInCIError() {
trackEvent("CLI: Ran init command in CI");
}
exports.trackInitInCIError = trackInitInCIError;
function trackLoginFailedError() {
trackEvent("CLI: Login failed while obtaining a token");
}
exports.trackLoginFailedError = trackLoginFailedError;
function trackCommandFailedError(error) {
trackEvent("CLI: Command failed unexpectedly", {
error,
});
}
exports.trackCommandFailedError = trackCommandFailedError;