UNPKG

@mui/x-telemetry

Version:
52 lines (48 loc) 2.52 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import fs from 'fs'; import path from 'path'; import { randomBytes } from 'crypto'; import { fileURLToPath } from 'url'; import getEnvironmentInfo from "./get-environment-info.mjs"; import { getAnonymousRepoHash, getAnonymousPackageNameHash, getAnonymousRootPathHash } from "./get-project-id.mjs"; import getAnonymousMachineId from "./get-machine-id.mjs"; import { TelemetryStorage } from "./storage.mjs"; // It's a flat build, both CJS and ESM files live in the same directory. // postinstall/index.mjs is at <pkg-root>/postinstall/index.mjs, // so we go up one level to reach the package root. const dirname = typeof __dirname === 'string' ? __dirname : path.dirname(fileURLToPath(import.meta.url)); (async () => { // If Node.js support permissions, we need to check if the current user has // the necessary permissions to write to the file system. if (typeof process.permission !== 'undefined' && !(process.permission.has('fs.read') && process.permission.has('fs.write'))) { return; } const storage = await TelemetryStorage.init({ distDir: process.cwd() }); const [environmentInfo, repoHash, postinstallPackageNameHash, rootPathHash, machineId] = await Promise.all([getEnvironmentInfo(), getAnonymousRepoHash(), getAnonymousPackageNameHash(), getAnonymousRootPathHash(), getAnonymousMachineId()]); // Compute projectId from the resolved signals (no duplicate calls) const projectId = repoHash || postinstallPackageNameHash || rootPathHash; const contextData = { config: { isInitialized: true }, traits: _extends({}, environmentInfo, { machineId, repoHash, postinstallPackageNameHash, rootPathHash, projectId, sessionId: randomBytes(32).toString('hex'), anonymousId: storage.anonymousId }) }; const packageRoot = path.resolve(dirname, '..'); const content = JSON.stringify(contextData, null, 2); // ESM: context.mjs fs.writeFileSync(path.resolve(packageRoot, 'context.mjs'), `export default ${content};`); // CJS: context.js fs.writeFileSync(path.resolve(packageRoot, 'context.js'), [`"use strict";`, `Object.defineProperty(exports, "__esModule", { value: true });`, `exports.default = void 0;`, `var _default = exports.default = ${content};`].join('\n')); })().catch(error => { console.error('[telemetry] Failed to make initialization. Please, report error to MUI X team:\n' + 'https://mui.com/r/x-telemetry-postinstall-troubleshoot\n', error); });