Skip to content

Commit 0d60f42

Browse files
committed
fix(cli-repl): add best guess about installation method to buildInfo()
1 parent 130fd18 commit 0d60f42

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

packages/build/src/build-info.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Config } from './config';
22
import { promises as fs } from 'fs';
3-
import os from 'os';
3+
import type { BuildInfo } from '../../cli-repl/src/build-info';
44

55
/**
66
* Write data into a build config that is included in the executable but
@@ -18,12 +18,22 @@ export async function writeBuildInfo(
1818
throw new Error('Segment key is required');
1919
}
2020

21-
const info = {
21+
const info: Omit<
22+
BuildInfo,
23+
| 'deps'
24+
| 'installationMethod'
25+
| 'runtimeArch'
26+
| 'runtimePlatform'
27+
| 'nodeVersion'
28+
| 'opensslVersion'
29+
| 'sharedOpenssl'
30+
| 'runtimeGlibcVersion'
31+
> = {
2232
segmentApiKey: config.segmentKey,
2333
version: config.version,
2434
distributionKind,
25-
buildArch: os.arch(),
26-
buildPlatform: os.platform(),
35+
buildArch: process.arch,
36+
buildPlatform: process.platform,
2737
buildTarget: config.executableOsId ?? 'unknown',
2838
buildTime: new Date().toISOString(),
2939
gitVersion: config.revision ?? null,

packages/cli-repl/src/build-info.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os from 'os';
22
import { NodeDriverServiceProvider } from '@mongosh/service-provider-node-driver';
3+
import { promises as fs } from 'fs';
34

45
export interface BuildInfo {
56
version: string;
67
nodeVersion: string;
78
distributionKind: 'unpackaged' | 'packaged' | 'compiled';
9+
installationMethod: 'npx' | 'homebrew' | 'linux-system-wide' | 'other';
810
runtimeArch: (typeof process)['arch'];
911
runtimePlatform: (typeof process)['platform'];
1012
buildArch: (typeof process)['arch'];
@@ -34,7 +36,36 @@ function getSystemArch(): (typeof process)['arch'] {
3436
: process.arch;
3537
}
3638

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/)
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+
> {
3869
const runtimeData = {
3970
nodeVersion: process.version,
4071
opensslVersion: process.versions.openssl,
@@ -86,7 +117,10 @@ export async function buildInfo({
86117
if (!withSegmentApiKey) {
87118
delete buildInfo.segmentApiKey;
88119
}
89-
return buildInfo;
120+
return {
121+
installationMethod: await getInstallationMethod(buildInfo),
122+
...buildInfo,
123+
};
90124
}
91125

92126
let cachedGlibcVersion: string | undefined | null = null;

0 commit comments

Comments
 (0)