@mui/x-telemetry
Version:
MUI X Telemetry.
73 lines (69 loc) • 2.71 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getAnonymousPackageNameHash = getAnonymousPackageNameHash;
exports.getAnonymousRepoHash = getAnonymousRepoHash;
exports.getAnonymousRootPathHash = getAnonymousRootPathHash;
exports.getPackageName = getPackageName;
var _child_process = require("child_process");
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _util = _interopRequireDefault(require("util"));
var _hash = require("./hash");
const asyncExec = _util.default.promisify(_child_process.exec);
async function execCLI(command) {
try {
const response = await asyncExec(command, {
timeout: 1000,
windowsHide: true
});
return String(response.stdout).trim();
} catch (_) {
return null;
}
}
function getPackageName() {
const cwd = process.cwd();
const segments = cwd.split(_path.default.sep);
for (let i = segments.length; i > 0; i -= 1) {
const dir = segments.slice(0, i).join(_path.default.sep) || _path.default.sep;
try {
const content = _fs.default.readFileSync(_path.default.join(dir, 'package.json'), 'utf-8');
const pkg = JSON.parse(content);
if (pkg.name && typeof pkg.name === 'string') {
return pkg.name;
}
} catch (_) {
// No package.json at this level, continue walking up
}
}
return null;
}
// Q: Why does MUI send multiple project identifiers?
// A:
// MUI's telemetry always anonymizes these values. We send three separate
// signals (repoHash, postinstallPackageNameHash, rootPathHash) plus a computed projectId
// so we can handle monorepos (same repo, different apps) and micro-frontends
// (different repos, same app) correctly on the backend.
// Raw repo identifier: git remote URL (hashed later into repoHash)
async function getRawRepoId() {
return (await execCLI(`git config --local --get remote.upstream.url`)) || (await execCLI(`git config --local --get remote.origin.url`)) || process.env.REPOSITORY_URL || null;
}
// Raw root path: git root or cwd (hashed later into rootPathHash, unique per developer)
async function getRawRootPathId() {
return (await execCLI(`git rev-parse --show-toplevel`)) || process.cwd();
}
async function getAnonymousRepoHash() {
const raw = await getRawRepoId();
return raw ? (0, _hash.sha256)(raw) : null;
}
async function getAnonymousPackageNameHash() {
const raw = getPackageName();
return raw ? (0, _hash.sha256)(raw) : null;
}
async function getAnonymousRootPathHash() {
const raw = await getRawRootPathId();
return (0, _hash.sha256)(raw);
}