forked from zalmoxisus/remotedev-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (40 loc) · 1.27 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
var assign = require('object-assign');
var repeat = require('repeat-string');
var getPort = require('getport');
var getOptions = require('./lib/options');
var LOG_LEVEL_NONE = 0;
var LOG_LEVEL_ERROR = 1;
var LOG_LEVEL_WARN = 2;
var LOG_LEVEL_INFO = 3;
module.exports = function(argv) {
var SocketCluster = require('socketcluster').SocketCluster;
var options = assign(getOptions(argv), {
workerController: __dirname + '/lib/worker.js',
allowClientPublish: false
});
var port = options.port;
var logLevel = options.logLevel === undefined ? LOG_LEVEL_INFO : options.logLevel;
return new Promise(function(resolve) {
// Check port already used
getPort(port, function(err, p) {
if (err) {
if (logLevel >= LOG_LEVEL_ERROR) {
console.error(err);
}
return;
}
if (port !== p) {
if (logLevel >= LOG_LEVEL_WARN) {
console.log('[RemoteDev] Server port ' + port + ' is already used.');
}
resolve({ portAlreadyUsed: true, on: function(status, cb) { cb(); } });
} else {
if (logLevel >= LOG_LEVEL_INFO) {
console.log('[RemoteDev] Start server...');
console.log(repeat('-', 80) + '\n');
}
resolve(new SocketCluster(options));
}
});
});
};