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,
|
|
|
|
|
IPC_CLIENT_APPSAPCE,
|
|
|
|
|
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-25 13:57:47 -04:00
|
|
|
let connected = false;
|
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-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-25 13:57:47 -04:00
|
|
|
if (connected) {
|
|
|
|
|
ipc.of[name].emit(IPC_RESTART_EVENT);
|
|
|
|
|
}
|
2021-07-27 17:48:50 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
|
process.exit(0);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get connected() {
|
|
|
|
|
return connected;
|
2021-07-25 13:57:47 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const ProcessManager = new ProcessManagerClass();
|
|
|
|
|
|
2021-07-23 00:12:02 -04:00
|
|
|
const name = IPC_CLIENT_CONNECT_NAME;
|
|
|
|
|
ipc.config.appspace = IPC_CLIENT_APPSAPCE;
|
|
|
|
|
ipc.config.silent = true;
|
|
|
|
|
|
|
|
|
|
ipc.connectTo(name, () => {
|
2021-07-27 17:48:50 -04:00
|
|
|
ipc.of[name].on('connect', () => {
|
|
|
|
|
connected = true;
|
|
|
|
|
patchLog();
|
|
|
|
|
});
|
|
|
|
|
ipc.of[name].on('disconnect', () => {
|
|
|
|
|
connected = false
|
|
|
|
|
restoreLog();
|
|
|
|
|
});
|
2021-07-25 13:57:47 -04:00
|
|
|
ipc.of[name].on(IPC_REQUEST_RESTART, () => {
|
|
|
|
|
ProcessManager.emit('reload');
|
|
|
|
|
})
|
2021-07-26 00:18:21 -04:00
|
|
|
});
|
|
|
|
|
|
2021-07-28 23:08:45 -04:00
|
|
|
// process.on('SIGKILL', () => ProcessManager.quit());
|
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('exit', () => ProcessManager.quit());
|
|
|
|
|
process.on('beforeExit', () => ProcessManager.quit());
|
2021-07-27 17:48:50 -04:00
|
|
|
|
2021-07-28 01:42:32 -04:00
|
|
|
// dumbass hack hahahah :)
|
|
|
|
|
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-27 17:48:50 -04:00
|
|
|
///
|