2020-10-26 08:57:34 +03:00
|
|
|
import getPort from 'getport';
|
|
|
|
import SocketCluster from 'socketcluster';
|
|
|
|
import getOptions, { Options } from './options';
|
2019-01-04 01:30:48 +03:00
|
|
|
|
2019-01-10 20:23:33 +03:00
|
|
|
// var LOG_LEVEL_NONE = 0;
|
2020-10-26 08:57:34 +03:00
|
|
|
const LOG_LEVEL_ERROR = 1;
|
|
|
|
const LOG_LEVEL_WARN = 2;
|
|
|
|
const LOG_LEVEL_INFO = 3;
|
2019-01-04 01:30:48 +03:00
|
|
|
|
2020-10-26 08:57:34 +03:00
|
|
|
export interface ExtendedOptions extends Options {
|
|
|
|
workerController: string;
|
|
|
|
allowClientPublish: boolean;
|
|
|
|
}
|
|
|
|
|
2021-06-18 06:56:36 +03:00
|
|
|
export default function (argv: { [arg: string]: any }): Promise<{
|
2020-10-26 08:57:34 +03:00
|
|
|
portAlreadyUsed?: boolean;
|
2021-08-29 22:09:57 +03:00
|
|
|
on: (status: 'ready', cb: (() => void) | (() => Promise<void>)) => void;
|
2020-10-26 08:57:34 +03:00
|
|
|
}> {
|
|
|
|
const options = Object.assign(getOptions(argv), {
|
|
|
|
workerController: __dirname + '/worker.js',
|
2020-08-08 23:26:39 +03:00
|
|
|
allowClientPublish: false,
|
2019-01-04 01:30:48 +03:00
|
|
|
});
|
2020-10-26 08:57:34 +03:00
|
|
|
const port = options.port;
|
|
|
|
const logLevel =
|
2019-01-10 21:51:14 +03:00
|
|
|
options.logLevel === undefined ? LOG_LEVEL_INFO : options.logLevel;
|
2020-08-08 23:26:39 +03:00
|
|
|
return new Promise(function (resolve) {
|
2019-01-04 01:30:48 +03:00
|
|
|
// Check port already used
|
2020-08-08 23:26:39 +03:00
|
|
|
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) {
|
2020-10-26 08:57:34 +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,
|
2020-10-26 08:57:34 +03:00
|
|
|
on: function (status: string, cb: () => void) {
|
2019-01-10 21:51:14 +03:00
|
|
|
cb();
|
2020-08-08 23:26:39 +03:00
|
|
|
},
|
2019-01-10 21:51:14 +03:00
|
|
|
});
|
2019-01-04 01:30:48 +03:00
|
|
|
} else {
|
|
|
|
if (logLevel >= LOG_LEVEL_INFO) {
|
2019-01-04 01:58:29 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|
2020-10-26 08:57:34 +03:00
|
|
|
}
|