Skip to content

Commit c67ee8f

Browse files
committed
feat: remove cross-spawn
1 parent f728b42 commit c67ee8f

File tree

4 files changed

+27
-39
lines changed

4 files changed

+27
-39
lines changed

packages/mongodb-memory-server-core/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
},
3232
"homepage": "https://github.com/nodkz/mongodb-memory-server",
3333
"devDependencies": {
34-
"@types/cross-spawn": "^6.0.2",
3534
"@types/debug": "^4.1.5",
3635
"@types/dedent": "^0.7.0",
3736
"@types/find-cache-dir": "^3.2.0",
@@ -49,7 +48,6 @@
4948
"dependencies": {
5049
"@types/tmp": "^0.2.0",
5150
"camelcase": "^6.1.0",
52-
"cross-spawn": "^7.0.3",
5351
"debug": "^4.2.0",
5452
"find-cache-dir": "^3.3.1",
5553
"find-package-json": "^1.2.0",

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import path from 'path';
44
import LockFile from 'lockfile';
55
import mkdirp from 'mkdirp';
66
import findCacheDir from 'find-cache-dir';
7-
import { execSync } from 'child_process';
87
import MongoBinaryDownload from './MongoBinaryDownload';
98
import resolveConfig, { envToBool, ResolveConfigVariables } from './resolveConfig';
109
import debug from 'debug';
1110
import { assertion, isNullOrUndefined, pathExists } from './utils';
11+
import { spawnSync } from 'child_process';
1212

1313
const log = debug('MongoMS:MongoBinary');
1414

@@ -157,11 +157,8 @@ export class MongoBinary {
157157
binaryPath = await this.getSystemPath(options.systemBinary);
158158

159159
if (binaryPath) {
160-
if (binaryPath.indexOf(' ') >= 0) {
161-
binaryPath = `"${binaryPath}"`;
162-
}
163-
164-
const binaryVersion = execSync(`${binaryPath} --version`)
160+
log(`Spawning binaryPath "${binaryPath}" to get version`);
161+
const binaryVersion = spawnSync(binaryPath, ['--version'])
165162
.toString()
166163
.split('\n')[0]
167164
.split(' ')[2];

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { ChildProcess, SpawnOptions } from 'child_process';
2-
import { default as spawnChild } from 'cross-spawn';
3-
import path from 'path';
1+
import { ChildProcess, fork, spawn, SpawnOptions } from 'child_process';
2+
import path, { resolve } from 'path';
43
import MongoBinary from './MongoBinary';
54
import { MongoBinaryOpts } from './MongoBinary';
65
import debug from 'debug';
@@ -310,7 +309,7 @@ export class MongoInstance extends EventEmitter {
310309
* @fires MongoInstance#instanceLaunched
311310
*/
312311
_launchMongod(mongoBin: string): ChildProcess {
313-
const childProcess = spawnChild(mongoBin, this.prepareCommandArgs(), {
312+
const childProcess = spawn(resolve(mongoBin), this.prepareCommandArgs(), {
314313
...this.spawnOpts,
315314
stdio: 'pipe', // ensure that stdio is always an pipe, regardless of user input
316315
});
@@ -337,29 +336,30 @@ export class MongoInstance extends EventEmitter {
337336
_launchKiller(parentPid: number, childPid: number): ChildProcess {
338337
this.debug(`Called MongoInstance._launchKiller(parent: ${parentPid}, child: ${childPid}):`);
339338
// spawn process which kills itself and mongo process if current process is dead
340-
const killer = spawnChild(
341-
process.env['NODE'] ?? process.argv[0], // try Environment variable "NODE" before using argv[0]
342-
[
343-
path.resolve(__dirname, '../../scripts/mongo_killer.js'),
344-
parentPid.toString(),
345-
childPid.toString(),
346-
],
347-
{ stdio: 'pipe' }
339+
const killer = fork(
340+
path.resolve(__dirname, '../../scripts/mongo_killer.js'),
341+
[parentPid.toString(), childPid.toString()],
342+
{
343+
detached: true,
344+
stdio: 'ignore', // stdio cannot be done with an detached process cross-systems and without killing the fork on parent termination
345+
}
348346
);
349347

350-
killer.stdout?.on('data', (data) => {
351-
this.debug(`[MongoKiller]: ${data}`);
352-
});
348+
// killer.stdout?.on('data', (data) => {
349+
// this.debug(`[MongoKiller]: ${data}`);
350+
// });
353351

354-
killer.stderr?.on('data', (data) => {
355-
this.debug(`[MongoKiller]: ${data}`);
356-
});
352+
// killer.stderr?.on('data', (data) => {
353+
// this.debug(`[MongoKiller]: ${data}`);
354+
// });
357355

358-
['exit', 'message', 'disconnect', 'error'].forEach((type) => {
359-
killer.on(type, (...args) => {
360-
this.debug(`[MongoKiller]: ${type} - ${JSON.stringify(args)}`);
361-
});
362-
});
356+
// ['exit', 'message', 'disconnect', 'error'].forEach((type) => {
357+
// killer.on(type, (...args) => {
358+
// this.debug(`[MongoKiller]: ${type} - ${JSON.stringify(args)}`);
359+
// });
360+
// });
361+
362+
killer.unref(); // dont force an exit on the fork when parent is exiting
363363

364364
this.emit(MongoInstanceEvents.killerLaunched);
365365

yarn.lock

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,13 +1782,6 @@
17821782
dependencies:
17831783
"@types/node" "*"
17841784

1785-
"@types/cross-spawn@^6.0.2":
1786-
version "6.0.2"
1787-
resolved "https://registry.yarnpkg.com/@types/cross-spawn/-/cross-spawn-6.0.2.tgz#168309de311cd30a2b8ae720de6475c2fbf33ac7"
1788-
integrity sha512-KuwNhp3eza+Rhu8IFI5HUXRP0LIhqH5cAjubUvGXXthh4YYBuP2ntwEX+Cz8GJoZUHlKo247wPWOfA9LYEq4cw==
1789-
dependencies:
1790-
"@types/node" "*"
1791-
17921785
"@types/debug@^4.1.5":
17931786
version "4.1.5"
17941787
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
@@ -3347,7 +3340,7 @@ cross-spawn@^6.0.0:
33473340
shebang-command "^1.2.0"
33483341
which "^1.2.9"
33493342

3350-
cross-spawn@^7.0.0, cross-spawn@^7.0.1, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
3343+
cross-spawn@^7.0.0, cross-spawn@^7.0.1, cross-spawn@^7.0.2:
33513344
version "7.0.3"
33523345
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
33533346
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==

0 commit comments

Comments
 (0)