redux-devtools/packages/redux-devtools-cli/src/index.ts

57 lines
1.6 KiB
TypeScript
Raw Normal View History

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;
const LOG_LEVEL_ERROR = 1;
const LOG_LEVEL_WARN = 2;
const LOG_LEVEL_INFO = 3;
2019-01-04 01:30:48 +03:00
export interface ExtendedOptions extends Options {
workerController: string;
allowClientPublish: boolean;
}
export default function (argv: { [arg: string]: any }): Promise<{
portAlreadyUsed?: boolean;
on: (status: 'ready', cb: (() => void) | (() => Promise<void>)) => void;
}> {
const options = Object.assign(getOptions(argv), {
workerController: __dirname + '/worker.js',
allowClientPublish: false,
2019-01-04 01:30:48 +03:00
});
const port = options.port;
const logLevel =
2019-01-10 21:51:14 +03:00
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) {
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: string, cb: () => void) {
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
});
});
}