-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (62 loc) · 1.83 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const fs = require('fs');
const kill = require('tree-kill');
const spawn = require("child_process").spawn;
const EventEmitter = require('events');
class ComfyMongo extends EventEmitter {};
module.exports = function( dataDir = "./data" ) {
try {
fs.mkdirSync( "data", { recursive: true } );
}
catch( err ) {
if( err.code !== 'EEXIST' ) throw err;
}
const mongo = new ComfyMongo();
var mongoPath = "";
switch( process.platform ) {
case "win32": // Windows
mongoPath = __dirname + "\\mongodb\\win\\mongod.exe";
// mongoPath = __dirname + "/node_modules/.bin/mongo-win";
break;
case "darwin": // OSX
mongoPath = __dirname + "/mongodb/mac/mongod";
// mongoPath = __dirname + "/node_modules/.bin/mongo-mac";
break;
case "linux": // Linux
mongoPath = __dirname + "/mongodb/linux/mongod";
// mongoPath = __dirname + "/node_modules/.bin/mongo-linux";
break;
default:
throw new Error( "Unsupported Platform: " + process.platform );
}
let mongoProc = spawn( mongoPath, [ "-dbpath", dataDir ], {
shell: true
});
mongoProc.stdout.on( "data", ( data ) => {
mongo.emit( "output", data.toString( "utf8" ) );
if( data.includes( "waiting for connections on" ) ) {
mongo.emit( "ready" );
}
});
mongoProc.stderr.on( "data", ( data ) => {
mongo.emit( "error", data.toString( "utf8" ) );
});
mongoProc.on( "close", ( code ) => {
mongo.emit( "close", code );
});
mongoProc.on( "exit", ( code ) => {
mongo.emit( "exit", code );
});
mongo.shutdown = () => {
if( mongoProc ) {
kill( mongoProc.pid );
mongoProc = null;
}
};
process.on( "beforeExit", mongo.shutdown );
process.on( "SIGINT", mongo.shutdown );
process.on( "SIGTERM", mongo.shutdown );
process.on( "SIGBREAK", mongo.shutdown );
process.on( "SIGHUP", mongo.shutdown );
process.on( "uncaughtException", mongo.shutdown );
return mongo;
}