@shopify/cli-kit
Version:
A set of utilities, interfaces, and models that are common across all the platform features
71 lines • 3.3 kB
JavaScript
import { getLastSeenAuthMethod } from './session.js';
import { hashString } from '../../public/node/crypto.js';
import { getPackageManager, packageManagerFromUserAgent } from '../../public/node/node-package-manager.js';
import * as metadata from '../../public/node/metadata.js';
import { platformAndArch } from '../../public/node/os.js';
import { ciPlatform, cloudEnvironment, macAddress } from '../../public/node/context/local.js';
import { cwd } from '../../public/node/path.js';
import { currentProcessIsGlobal } from '../../public/node/is-global.js';
import { isWsl } from '../../public/node/system.js';
export async function startAnalytics({ commandContent, args, currentTime = new Date().getTime(), commandClass, }) {
let startCommand = commandContent.command;
if (commandClass && Object.prototype.hasOwnProperty.call(commandClass, 'analyticsNameOverride')) {
startCommand = commandClass.analyticsNameOverride() ?? commandContent.command;
}
let pluginName = commandClass?.plugin?.name;
if (commandClass && 'customPluginName' in commandClass) {
pluginName = commandClass.customPluginName;
}
await metadata.addSensitiveMetadata(() => ({
commandStartOptions: {
startTime: currentTime,
startCommand,
startArgs: args,
},
}));
await metadata.addPublicMetadata(() => ({
cmd_all_launcher: packageManagerFromUserAgent(),
cmd_all_alias_used: commandContent.alias,
cmd_all_topic: commandContent.topic,
cmd_all_plugin: pluginName,
cmd_all_force: flagIncluded('force', commandClass) ? args.includes('--force') : undefined,
}));
}
export async function getEnvironmentData(config) {
const ciplatform = ciPlatform();
const pluginNames = getPluginNames(config);
const shopifyPlugins = pluginNames.filter((plugin) => plugin.startsWith('@shopify/'));
const { platform, arch } = platformAndArch();
return {
uname: `${platform} ${arch}`,
env_ci: ciplatform.isCI,
env_ci_platform: ciplatform.name,
env_plugin_installed_any_custom: pluginNames.length !== shopifyPlugins.length,
env_plugin_installed_shopify: JSON.stringify(shopifyPlugins),
env_shell: config.shell,
env_web_ide: cloudEnvironment().editor ? cloudEnvironment().platform : undefined,
env_device_id: hashString(await macAddress()),
env_cloud: cloudEnvironment().platform,
env_package_manager: await getPackageManager(cwd()),
env_is_global: currentProcessIsGlobal(),
env_auth_method: await getLastSeenAuthMethod(),
env_is_wsl: await isWsl(),
env_build_repository: process.env.SHOPIFY_CLI_BUILD_REPO ?? 'unknown',
};
}
export async function getSensitiveEnvironmentData(config) {
return {
env_plugin_installed_all: JSON.stringify(getPluginNames(config)),
};
}
function getPluginNames(config) {
const pluginNames = [...config.plugins.keys()];
return pluginNames.sort().filter((plugin) => !plugin.startsWith('@oclif/'));
}
function flagIncluded(flag, commandClass) {
if (!commandClass)
return false;
const commandFlags = commandClass.flags ?? {};
return Object.keys(commandFlags).includes(flag);
}
//# sourceMappingURL=analytics.js.map