Skip to content

Commit 2fc0e1a

Browse files
committed
feat: add environment variable to use archive name for binary name
Closes #609
1 parent 8c0afc0 commit 2fc0e1a

File tree

8 files changed

+158
-33
lines changed

8 files changed

+158
-33
lines changed

docs/api/config-options.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ Option `USE_HTTP` is used to use `http` over `https`
128128

129129
Default: `false`
130130

131+
### USE_ARCHIVE_NAME_FOR_BINARY_NAME
132+
133+
Option `USE_ARCHIVE_NAME_FOR_BINARY_NAME` is used to use the archive name as binary name
134+
135+
Default: `false`
136+
131137
## How to use them in the package.json
132138

133139
To use the config options in the `package.json`, they need to be camelCased (and without `_`), and need to be in the property `config.mongodbMemoryServer`

packages/mongodb-memory-server-core/src/util/DryMongoBinary.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { assertion, checkBinaryPermissions, isNullOrUndefined, pathExists } from
44
import * as path from 'path';
55
import { arch, homedir, platform } from 'os';
66
import findCacheDir from 'find-cache-dir';
7-
import getOS, { AnyOS, isLinuxOS, OtherOS } from './getos';
7+
import { getOS, AnyOS, isLinuxOS, OtherOS } from './getos';
88
import { NoRegexMatchError, NoSystemBinaryFoundError, ParseArchiveRegexError } from './errors';
9+
import { MongoBinaryDownloadUrl } from './MongoBinaryDownloadUrl';
910

1011
const log = debug('MongoMS:DryMongoBinary');
1112

@@ -14,6 +15,7 @@ export interface BaseDryMongoBinaryOptions {
1415
downloadDir?: string;
1516
os?: AnyOS;
1617
arch?: string;
18+
platform?: string;
1719
systemBinary?: string;
1820
}
1921

@@ -24,6 +26,7 @@ export interface DryMongoBinaryOptions extends BaseDryMongoBinaryOptions {
2426
export interface DryMongoBinaryNameOptions {
2527
version: NonNullable<DryMongoBinaryOptions['version']>;
2628
arch: NonNullable<DryMongoBinaryOptions['arch']>;
29+
platform: NonNullable<DryMongoBinaryOptions['version']>;
2730
os: NonNullable<DryMongoBinaryOptions['os']>;
2831
}
2932

@@ -52,10 +55,6 @@ export class DryMongoBinary {
5255
* Binaries already found, values are: [Version, Path]
5356
*/
5457
static binaryCache: Map<string, string> = new Map();
55-
/**
56-
* Cache the "getOS" call, so that not much has to be re-executed over and over
57-
*/
58-
static cachedGetOs: AnyOS;
5958

6059
/**
6160
* Try to locate an existing binary
@@ -111,15 +110,12 @@ export class DryMongoBinary {
111110
? { version: defaultVersion }
112111
: opts;
113112

114-
if (isNullOrUndefined(this.cachedGetOs)) {
115-
this.cachedGetOs = await getOS();
116-
}
117-
118113
const final: Required<DryMongoBinaryOptions> = {
119114
version: ensuredOpts.version || defaultVersion,
120115
downloadDir:
121116
resolveConfig(ResolveConfigVariables.DOWNLOAD_DIR) || ensuredOpts.downloadDir || '',
122-
os: ensuredOpts.os ?? this.cachedGetOs,
117+
os: ensuredOpts.os ?? (await getOS()),
118+
platform: ensuredOpts.platform || platform(),
123119
arch: ensuredOpts.arch || arch(),
124120
systemBinary:
125121
resolveConfig(ResolveConfigVariables.SYSTEM_BINARY) || ensuredOpts.systemBinary || '',
@@ -208,12 +204,22 @@ export class DryMongoBinary {
208204
* Get the full path with filename
209205
* @returns Absoulte Path with FileName
210206
*/
211-
static getBinaryName(opts: DryMongoBinaryNameOptions): string {
207+
static async getBinaryName(opts: DryMongoBinaryNameOptions): Promise<string> {
212208
log('getBinaryName');
213-
const addExe = platform() === 'win32' ? '.exe' : '';
214-
const dist = isLinuxOS(opts.os) ? opts.os.dist : opts.os.os;
215209

216-
return `mongod-${opts.arch}-${dist}-${opts.version}${addExe}`;
210+
let binaryName: string;
211+
212+
if (envToBool(resolveConfig(ResolveConfigVariables.USE_ARCHIVE_NAME_FOR_BINARY_NAME))) {
213+
const archiveName = await new MongoBinaryDownloadUrl(opts).getArchiveName();
214+
binaryName = path.parse(archiveName).name;
215+
} else {
216+
const addExe = opts.platform === 'win32' ? '.exe' : '';
217+
const dist = isLinuxOS(opts.os) ? opts.os.dist : opts.os.os;
218+
219+
binaryName = `mongod-${opts.arch}-${dist}-${opts.version}${addExe}`;
220+
}
221+
222+
return binaryName;
217223
}
218224

219225
/**
@@ -266,7 +272,7 @@ export class DryMongoBinary {
266272
relative: '',
267273
resolveConfig: '',
268274
};
269-
const binaryName = this.getBinaryName(opts);
275+
const binaryName = await this.getBinaryName(opts);
270276
// Assign "node_modules/.cache" to modulesCache
271277

272278
// if we're in postinstall script, npm will set the cwd too deep

packages/mongodb-memory-server-core/src/util/MongoBinaryDownload.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ export class MongoBinaryDownload {
115115
protected async getPath(): Promise<string> {
116116
const opts = await DryMongoBinary.generateOptions(this.binaryOpts);
117117

118-
return DryMongoBinary.combineBinaryName(this.downloadDir, DryMongoBinary.getBinaryName(opts));
118+
return DryMongoBinary.combineBinaryName(
119+
this.downloadDir,
120+
await DryMongoBinary.getBinaryName(opts)
121+
);
119122
}
120123

121124
/**

packages/mongodb-memory-server-core/src/util/MongoBinaryDownloadUrl.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { AnyOS, LinuxOS } from './getos';
1+
import { getOS, AnyOS, LinuxOS } from './getos';
22
import { resolveConfig, ResolveConfigVariables } from './resolveConfig';
33
import debug from 'debug';
44
import * as semver from 'semver';
55
import { isNullOrUndefined } from './utils';
6-
import { BaseDryMongoBinaryOptions, DryMongoBinary } from './DryMongoBinary';
76
import { URL } from 'url';
87
import {
98
KnownVersionIncompatibilityError,
@@ -14,10 +13,10 @@ import {
1413
const log = debug('MongoMS:MongoBinaryDownloadUrl');
1514

1615
export interface MongoBinaryDownloadUrlOpts {
17-
version: NonNullable<BaseDryMongoBinaryOptions['version']>;
16+
version: string;
1817
platform: string;
19-
arch: NonNullable<BaseDryMongoBinaryOptions['arch']>;
20-
os?: BaseDryMongoBinaryOptions['os'];
18+
arch: string;
19+
os?: AnyOS;
2120
}
2221

2322
/**
@@ -150,7 +149,7 @@ export class MongoBinaryDownloadUrl implements MongoBinaryDownloadUrlOpts {
150149
// the highest version for "i686" seems to be 3.3
151150
if (this.arch !== 'i686') {
152151
if (!this.os) {
153-
this.os = (await DryMongoBinary.generateOptions()).os;
152+
this.os = await getOS();
154153
}
155154

156155
osString = this.getLinuxOSVersionString(this.os as LinuxOS);

packages/mongodb-memory-server-core/src/util/__tests__/MongoBinaryDownload.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ describe('MongoBinaryDownload', () => {
176176
dist: 'ubuntu',
177177
release: '14',
178178
},
179+
platform: 'linux',
179180
});
180181

181182
const du = new MongoBinaryDownload({ downloadDir });

0 commit comments

Comments
 (0)