Skip to content

Commit c803c31

Browse files
authored
fix(cli-repl): add best guess about installation method to buildInfo() MONGOSH-2915 (#2551)
1 parent fea739e commit c803c31

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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/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+
> {
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;

packages/e2e-tests/test/e2e.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ describe('e2e', function () {
6868

6969
const data = JSON.parse(shell.output);
7070
expect(Object.keys(data)).to.deep.equal([
71+
'installationMethod',
7172
'version',
7273
'distributionKind',
7374
'buildArch',

0 commit comments

Comments
 (0)