redux-devtools/packages/redux-devtools-cli/index.js

51 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2019-01-04 01:30:48 +03:00
var getPort = require('getport');
var SocketCluster = require('socketcluster');
var getOptions = require('./src/options');
2019-01-10 20:23:33 +03:00
// var LOG_LEVEL_NONE = 0;
2019-01-04 01:30:48 +03:00
var LOG_LEVEL_ERROR = 1;
var LOG_LEVEL_WARN = 2;
var LOG_LEVEL_INFO = 3;
module.exports = function (argv) {
2019-01-04 01:30:48 +03:00
var options = Object.assign(getOptions(argv), {
workerController: __dirname + '/src/worker.js',
allowClientPublish: false,
2019-01-04 01:30:48 +03:00
});
var port = options.port;
2019-01-10 21:51:14 +03:00
var logLevel =
options.logLevel === undefined ? LOG_LEVEL_INFO : options.logLevel;
return new Promise(function (resolve) {
2019-01-04 01:30:48 +03:00
// Check port already used
getPort(port, function (err, p) {
2019-01-10 20:23:33 +03:00
/* eslint-disable no-console */
2019-01-04 01:30:48 +03:00
if (err) {
if (logLevel >= LOG_LEVEL_ERROR) {
console.error(err);
}
return;
}
if (port !== p) {
if (logLevel >= LOG_LEVEL_WARN) {
2019-01-10 21:51:14 +03:00
console.log(
'[ReduxDevTools] Server port ' + port + ' is already used.'
);
2019-01-04 01:30:48 +03:00
}
2019-01-10 21:51:14 +03:00
resolve({
portAlreadyUsed: true,
on: function (status, cb) {
2019-01-10 21:51:14 +03:00
cb();
},
2019-01-10 21:51:14 +03:00
});
2019-01-04 01:30:48 +03:00
} else {
if (logLevel >= LOG_LEVEL_INFO) {
console.log('[ReduxDevTools] Start server...');
2019-01-04 01:30:48 +03:00
console.log('-'.repeat(80) + '\n');
}
resolve(new SocketCluster(options));
}
2019-01-10 20:23:33 +03:00
/* eslint-enable no-console */
2019-01-04 01:30:48 +03:00
});
});
};