|
1 | 1 | import os from 'os'; |
2 | 2 | import { NodeDriverServiceProvider } from '@mongosh/service-provider-node-driver'; |
| 3 | +import { promises as fs } from 'fs'; |
3 | 4 |
|
4 | 5 | export interface BuildInfo { |
5 | 6 | version: string; |
6 | 7 | nodeVersion: string; |
7 | 8 | distributionKind: 'unpackaged' | 'packaged' | 'compiled'; |
| 9 | + installationMethod: 'npx' | 'homebrew' | 'linux-system-wide' | 'other'; |
8 | 10 | runtimeArch: (typeof process)['arch']; |
9 | 11 | runtimePlatform: (typeof process)['platform']; |
10 | 12 | buildArch: (typeof process)['arch']; |
@@ -34,7 +36,36 @@ function getSystemArch(): (typeof process)['arch'] { |
34 | 36 | : process.arch; |
35 | 37 | } |
36 | 38 |
|
37 | | -export function baseBuildInfo(): Omit<BuildInfo, 'deps'> { |
| 39 | +async function getInstallationMethod( |
| 40 | + info: Pick<BuildInfo, 'distributionKind' | 'buildPlatform'> |
| 41 | +): Promise<BuildInfo['installationMethod']> { |
| 42 | + if (info.distributionKind !== 'compiled') { |
| 43 | + if ( |
| 44 | + process.env.npm_lifecycle_event === 'npx' && |
| 45 | + process.env.npm_lifecycle_script?.includes('mongosh') |
| 46 | + ) |
| 47 | + return 'npx'; |
| 48 | + if ( |
| 49 | + __filename.match(/\bhomebrew\b/i) && |
| 50 | + process.execPath.match(/\bhomebrew\b/i) |
| 51 | + ) |
| 52 | + return 'homebrew'; |
| 53 | + } else { |
| 54 | + if ( |
| 55 | + info.buildPlatform === 'linux' && |
| 56 | + process.execPath.startsWith('/usr/bin/') && |
| 57 | + (await fs.stat(process.execPath)).uid === 0 |
| 58 | + ) { |
| 59 | + return 'linux-system-wide'; // e.g. deb or rpm |
| 60 | + } |
| 61 | + } |
| 62 | + return 'other'; |
| 63 | +} |
| 64 | + |
| 65 | +export function baseBuildInfo(): Omit< |
| 66 | + BuildInfo, |
| 67 | + 'deps' | 'installationMethod' |
| 68 | +> { |
38 | 69 | const runtimeData = { |
39 | 70 | nodeVersion: process.version, |
40 | 71 | opensslVersion: process.versions.openssl, |
@@ -86,7 +117,10 @@ export async function buildInfo({ |
86 | 117 | if (!withSegmentApiKey) { |
87 | 118 | delete buildInfo.segmentApiKey; |
88 | 119 | } |
89 | | - return buildInfo; |
| 120 | + return { |
| 121 | + installationMethod: await getInstallationMethod(buildInfo), |
| 122 | + ...buildInfo, |
| 123 | + }; |
90 | 124 | } |
91 | 125 |
|
92 | 126 | let cachedGlibcVersion: string | undefined | null = null; |
|
0 commit comments