Skip to content

Commit 4258eb7

Browse files
committed
refactor(MongoBinaryDownload): unify path resolving
1 parent 7d3e998 commit 4258eb7

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

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

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,22 @@ export class MongoBinaryDownload {
6868
};
6969
}
7070

71+
/**
72+
* Get the full path with filename
73+
* @return Absoulte Path with FileName
74+
*/
75+
protected getPath(): string {
76+
const addExe = this.platform === 'win32' ? '.exe' : '';
77+
78+
return path.resolve(this.downloadDir, this.version, `mongod${addExe}`);
79+
}
80+
7181
/**
7282
* Get the path of the already downloaded "mongod" file
7383
* otherwise download it and then return the path
7484
*/
7585
async getMongodPath(): Promise<string> {
76-
const binaryName = this.platform === 'win32' ? 'mongod.exe' : 'mongod';
77-
const mongodPath = path.resolve(this.downloadDir, this.version, binaryName);
86+
const mongodPath = this.getPath();
7887

7988
if (await pathExists(mongodPath)) {
8089
return mongodPath;
@@ -222,12 +231,12 @@ export class MongoBinaryDownload {
222231
* @returns extracted directory location
223232
*/
224233
async extract(mongoDBArchive: string): Promise<string> {
225-
const binaryName = this.platform === 'win32' ? 'mongod.exe' : 'mongod';
226-
const extractDir = path.resolve(this.downloadDir, this.version);
227-
log(`extract(): ${extractDir}`);
234+
const mongodbFullPath = this.getPath();
235+
const mongodbDirPath = path.dirname(mongodbFullPath);
236+
log(`extract: ${mongodbFullPath}`);
228237

229-
if (!(await pathExists(extractDir))) {
230-
fspromises.mkdir(extractDir);
238+
if (!(await pathExists(mongodbDirPath))) {
239+
await fspromises.mkdir(mongodbDirPath);
231240
}
232241

233242
let filter: (file: string) => boolean;
@@ -240,10 +249,10 @@ export class MongoBinaryDownload {
240249
filter = (file: string) => /bin\/mongod$/.test(file);
241250
}
242251

243-
if (/(.tar.gz|.tgz)$/.test(path.extname(mongoDBArchive))) {
244-
await this.extractTarGz(mongoDBArchive, extractDir, filter);
245-
} else if (/.zip$/.test(path.extname(mongoDBArchive))) {
246-
await this.extractZip(mongoDBArchive, extractDir, filter);
252+
if (/(.tar.gz|.tgz)$/.test(mongoDBArchive)) {
253+
await this.extractTarGz(mongoDBArchive, mongodbFullPath, filter);
254+
} else if (/.zip$/.test(mongoDBArchive)) {
255+
await this.extractZip(mongoDBArchive, mongodbFullPath, filter);
247256
} else {
248257
throw new Error(
249258
`MongoBinaryDownload: unsupported archive ${mongoDBArchive} (downloaded from ${
@@ -252,33 +261,33 @@ export class MongoBinaryDownload {
252261
);
253262
}
254263

255-
if (!(await pathExists(path.resolve(this.downloadDir, this.version, binaryName)))) {
264+
if (!(await pathExists(mongodbFullPath))) {
256265
throw new Error(
257266
`MongoBinaryDownload: missing mongod binary in ${mongoDBArchive} (downloaded from ${
258267
this._downloadingUrl ?? 'unknown'
259268
}). Broken archive from MongoDB Provider?`
260269
);
261270
}
262271

263-
return extractDir;
272+
return mongodbFullPath;
264273
}
265274

266275
/**
267276
* Extract a .tar.gz archive
268277
* @param mongoDBArchive Archive location
269-
* @param extractDir Directory to extract to
278+
* @param extractPath Directory to extract to
270279
* @param filter Method to determine which files to extract
271280
*/
272281
async extractTarGz(
273282
mongoDBArchive: string,
274-
extractDir: string,
283+
extractPath: string,
275284
filter: (file: string) => boolean
276285
): Promise<void> {
277286
const extract = tar.extract();
278287
extract.on('entry', (header, stream, next) => {
279288
if (filter(header.name)) {
280289
stream.pipe(
281-
createWriteStream(path.resolve(extractDir, path.basename(header.name)), {
290+
createWriteStream(extractPath, {
282291
mode: 0o775,
283292
})
284293
);
@@ -310,12 +319,12 @@ export class MongoBinaryDownload {
310319
/**
311320
* Extract a .zip archive
312321
* @param mongoDBArchive Archive location
313-
* @param extractDir Directory to extract to
322+
* @param extractPath Directory to extract to
314323
* @param filter Method to determine which files to extract
315324
*/
316325
async extractZip(
317326
mongoDBArchive: string,
318-
extractDir: string,
327+
extractPath: string,
319328
filter: (file: string) => boolean
320329
): Promise<void> {
321330
return new Promise((resolve, reject) => {
@@ -340,7 +349,7 @@ export class MongoBinaryDownload {
340349

341350
r.on('end', () => zipfile.readEntry());
342351
r.pipe(
343-
createWriteStream(path.resolve(extractDir, path.basename(entry.fileName)), {
352+
createWriteStream(extractPath, {
344353
mode: 0o775,
345354
})
346355
);

0 commit comments

Comments
 (0)