forked from docker-archive-public/docker.kitematic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
134 lines (127 loc) · 4.23 KB
/
index.js
File metadata and controls
134 lines (127 loc) · 4.23 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var child_process = require('child_process');
var net = require('net');
var os = require('os');
var fs = require('fs');
var path = require('path');
var freeport = function (callback) {
var server = net.createServer();
var port = 0;
server.on('listening', function() {
port = server.address().port;
server.close();
});
server.on('close', function() {
callback(null, port);
});
server.listen(0, '127.0.0.1');
};
var start = function (callback) {
if (process.env.NODE_ENV === 'development') {
callback('http://localhost:3000');
} else {
process.stdout.write('Starting production server\n');
if (os.platform() === 'darwin') {
var kitePath = path.join(process.env.HOME, 'Library/Application Support/Kitematic/');
var dataPath = path.join(kitePath, 'data');
console.log(dataPath);
var bundlePath = path.join(kitePath, 'bundle');
if (!fs.existsSync(kitePath)) {
fs.mkdirSync(kitePath);
}
if (!fs.existsSync(dataPath)) {
fs.mkdirSync(dataPath);
}
if (!fs.existsSync(bundlePath)) {
fs.mkdirSync(bundlePath);
}
}
// One for meteor, one for mongo
freeport(function (err, webPort) {
freeport(function(err, mongoPort) {
console.log('MongoDB: ' + mongoPort);
console.log('webPort: ' + webPort);
child_process.exec('kill $(ps aux -e | grep PURPOSE=KITEMATIC | awk \'{print $2}\')', function (error, stdout, stderr) {
console.log(error);
console.log(stdout);
console.log(stderr);
var mongoChild = child_process.spawn(path.join(process.cwd(), 'resources', 'mongod'), ['--bind_ip', '127.0.0.1', '--dbpath', dataPath, '--port', mongoPort, '--unixSocketPrefix', dataPath], {
env: {
PURPOSE: 'KITEMATIC'
}
});
var started = false;
mongoChild.stdout.setEncoding('utf8');
mongoChild.stdout.on('data', function (data) {
if (data.indexOf('waiting for connections on port ' + mongoPort)) {
if (!started) {
started = true;
} else {
return;
}
console.log('Starting node child...');
var rootURL = 'http://localhost:' + webPort;
var user_env = process.env;
user_env.ROOT_URL = rootURL;
user_env.PORT = webPort;
user_env.BIND_IP = '127.0.0.1';
user_env.DB_PATH = dataPath;
user_env.MONGO_URL = 'mongodb://localhost:' + mongoPort + '/meteor';
console.log(path.join(process.cwd(), 'resources', 'node'));
var nodeChild = child_process.spawn(path.join(process.cwd(), 'resources', 'node'), ['./bundle/main.js'], {
env: user_env
});
var opened = false;
nodeChild.stdout.setEncoding('utf8');
nodeChild.stdout.on('data', function (data) {
console.log(data);
if (data.indexOf('Kitematic started.') !== -1) {
if (!opened) {
opened = true;
} else {
return;
}
callback(rootURL, nodeChild, mongoChild);
}
});
}
});
});
});
});
}
};
start(function (url, nodeChild, mongoChild) {
var cleanUpChildren = function () {
console.log('Cleaning up children.')
mongoChild.kill();
nodeChild.kill();
};
if (nodeChild && mongoChild) {
process.on('exit', cleanUpChildren);
process.on('uncaughtException', cleanUpChildren);
process.on('SIGINT', cleanUpChildren);
process.on('SIGTERM', cleanUpChildren);
}
var gui = require('nw.gui');
var mainWindow = gui.Window.get();
gui.App.on('reopen', function () {
mainWindow.show();
});
setTimeout(function () {
mainWindow.window.location = url;
mainWindow.on('loaded', function () {
mainWindow.show();
});
}, 400);
mainWindow.on('close', function (type) {
this.hide();
if (type === 'quit') {
console.log('here');
if (nodeChild && mongoChild) {
cleanUpChildren();
}
this.close(true);
}
console.log('Window Closed.');
});
});