2021-07-27 17:48:50 -04:00
|
|
|
import chalk from 'chalk';
|
2021-07-25 13:57:47 -04:00
|
|
|
import EventEmitter from 'events';
|
2021-07-23 00:12:02 -04:00
|
|
|
import ipc from 'node-ipc';
|
|
|
|
|
import {
|
|
|
|
|
IPC_CLIENT_CONNECT_NAME,
|
2021-07-29 13:28:30 -04:00
|
|
|
IPC_CLIENT_APPSPACE,
|
2021-07-23 00:12:02 -04:00
|
|
|
IPC_QUIT_EVENT,
|
2021-07-25 13:57:47 -04:00
|
|
|
IPC_RESTART_EVENT,
|
|
|
|
|
IPC_REQUEST_RESTART
|
2021-07-23 00:12:02 -04:00
|
|
|
} from './Constants.js';
|
|
|
|
|
|
2021-07-29 13:28:30 -04:00
|
|
|
ipc.config.appspace = IPC_CLIENT_APPSPACE;
|
|
|
|
|
ipc.config.silent = true;
|
|
|
|
|
|
2021-07-27 17:48:50 -04:00
|
|
|
const oldConsoleLog = console.log;
|
|
|
|
|
|
|
|
|
|
const patchLog = () => console.log = console.log.bind(console, chalk.cyan('[CLIENT]'));
|
|
|
|
|
const restoreLog = () => console.log = oldConsoleLog;
|
|
|
|
|
|
|
|
|
|
// const log = (...args: any[]) => console.log(chalk.cyan('[CLIENT]'), ...args);
|
2021-07-25 13:57:47 -04:00
|
|
|
|
|
|
|
|
class ProcessManagerClass extends EventEmitter {
|
2021-07-28 01:42:32 -04:00
|
|
|
|
|
|
|
|
processLock = Promise.resolve();
|
|
|
|
|
// TODO replace this with an async sortof
|
|
|
|
|
// event emitter, to wait for all clean up
|
|
|
|
|
|
2021-07-29 13:28:30 -04:00
|
|
|
connected = false;
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
ipc.connectTo(IPC_CLIENT_CONNECT_NAME, () => {
|
|
|
|
|
ipc.of[IPC_CLIENT_CONNECT_NAME].on('connect', this.onConnect.bind(this));
|
|
|
|
|
ipc.of[IPC_CLIENT_CONNECT_NAME].on('disconnect', this.onDisconnect.bind(this));
|
|
|
|
|
ipc.of[IPC_CLIENT_CONNECT_NAME].on(IPC_REQUEST_RESTART, this.onReloadRequested.bind(this));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onReloadRequested() {
|
|
|
|
|
this.emit('reload');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onConnect() {
|
|
|
|
|
this.connected = true;
|
|
|
|
|
this.emit('change');
|
|
|
|
|
patchLog();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onDisconnect() {
|
|
|
|
|
this.connected = false;
|
|
|
|
|
this.emit('change');
|
|
|
|
|
restoreLog();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-25 13:57:47 -04:00
|
|
|
quit() {
|
2021-07-27 17:48:50 -04:00
|
|
|
this.emit('shutdown');
|
2021-07-28 01:42:32 -04:00
|
|
|
this.processLock.then(() => {
|
|
|
|
|
process.exit(0);
|
|
|
|
|
})
|
2021-07-25 13:57:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
restart() {
|
2021-07-27 17:48:50 -04:00
|
|
|
this.emit('shutdown');
|
2021-07-29 13:28:30 -04:00
|
|
|
if (this.connected) {
|
|
|
|
|
ipc.of[IPC_CLIENT_CONNECT_NAME].emit(IPC_RESTART_EVENT);
|
2021-07-25 13:57:47 -04:00
|
|
|
}
|
2021-07-27 17:48:50 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
|
process.exit(0);
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-07-25 13:57:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const ProcessManager = new ProcessManagerClass();
|
|
|
|
|
|
2021-07-27 17:48:50 -04:00
|
|
|
process.on('SIGTERM', () => ProcessManager.quit());
|
|
|
|
|
process.on('SIGINT', () => ProcessManager.quit());
|
2021-07-28 01:42:32 -04:00
|
|
|
process.on('beforeExit', () => ProcessManager.quit());
|
2021-07-27 17:48:50 -04:00
|
|
|
|
2021-07-30 22:11:13 -04:00
|
|
|
// HACK dumbass hack hahahah :)
|
2021-07-28 01:42:32 -04:00
|
|
|
if (process.platform === "win32") {
|
|
|
|
|
var rl = require("readline").createInterface({
|
|
|
|
|
input: process.stdin,
|
|
|
|
|
output: process.stdout
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
rl.on("SIGINT", function () {
|
|
|
|
|
// @ts-ignore dont know why, dont fuckin care
|
|
|
|
|
process.emit("SIGINT");
|
|
|
|
|
});
|
2021-07-29 13:28:30 -04:00
|
|
|
}
|