Skip to content

Commit

Permalink
feat: Add IPC to enable commands from different processes
Browse files Browse the repository at this point in the history
Preparation of daemon and dynamic configuration reloading.
  • Loading branch information
schw4rzlicht committed Jun 10, 2020
1 parent 2ee8af3 commit f0bc52c
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 8 deletions.
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"humanize-duration": "^3.23.0",
"libnpm": "^3.0.1",
"lodash": "^4.17.15",
"node-ipc": "^9.1.1",
"semver": "^7.3.2",
"source-map-support": "^0.5.19",
"telnet-client": "^1.4.2",
Expand All @@ -46,6 +47,7 @@
"@types/humanize-duration": "^3.18.0",
"@types/jest": "^25.2.3",
"@types/lodash": "^4.14.152",
"@types/node-ipc": "^9.1.3",
"@types/semver": "^7.2.0",
"@types/source-map-support": "^0.5.1",
"ajv-cli": "~3.1.0",
Expand Down
25 changes: 25 additions & 0 deletions src/lib/CommandSocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {EventEmitter} from "@d-fischer/typed-event-emitter";

import ipc = require("node-ipc");

export class CommandSocket extends EventEmitter {

constructor() {
super();

ipc.config.appspace = "twitch2ma.";
ipc.config.id = "main"
ipc.config.silent = true;

ipc.serve(() => {

ipc.server.on("exit", (data, socket) => {
this.emit(this.onExitCommand);
});
});

ipc.server.start();
}

onExitCommand = this.registerEvent<() => any>();
}
56 changes: 48 additions & 8 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import {Command} from "commander";
import Twitch2Ma from "./Twitch2Ma";
import {Config} from "./Config";
import {CommandSocket} from "./CommandSocket";

import Fs = require("fs");
import YAML = require("yaml");
import _ = require("lodash");
import chalk = require("chalk");
import ipc = require("node-ipc");

const semverGt = require('semver/functions/gt')
const packageInformation = require("../../package.json");

let commandSocket: CommandSocket;

export async function main() {

process.on("SIGINT", () => {
console.log(chalk`\n{bold Thank you for using twitch2ma} ❤️`);
process.exit(0);
});
process.on("SIGINT", exit);

return require("libnpm")
.manifest(`${packageInformation.name}@latest`)
Expand All @@ -25,19 +26,54 @@ export async function main() {
}

function init(): void {
new Command()
let program = new Command()
.name(packageInformation.name)
.description(packageInformation.description)
.version(packageInformation.version, "-v, --version", "output the current version")
.version(packageInformation.version, "-v, --version", "output the current version");

program.command("start", {isDefault: true})
.description("starts twitch2ma bot")
.arguments("[configFile]")
.action(configFile => {
loadConfig(configFile)
.then(config => new Twitch2Ma(config))
.then(attachEventHandlers)
.then(twitch2Ma => twitch2Ma.start())
.then(openCommandSocket)
.catch(exitWithError);
})
.parse(process.argv);
});

program.command("stop")
.description("stops twitch2ma bot")
.action(() => emitSocketEvent("exit"));

program.parse(process.argv);
}

function exit() {
console.log(chalk`\n{bold Thank you for using twitch2ma} ❤️`);
process.exit(0);
}

function emitSocketEvent(event: string) {

ipc.config.appspace = "twitch2ma.";
ipc.config.silent = true;

ipc.connectTo("main", () => {
ipc.of.main.on("connect", () => {
ipc.of.main.emit(event);
ipc.disconnect("main");
});
})
}

function openCommandSocket() {
commandSocket = new CommandSocket();
commandSocket.onExitCommand(() => {
socket("Exit command received!");
exit();
});
}

export function notifyUpdate(manifest: any) {
Expand Down Expand Up @@ -106,6 +142,10 @@ export function exitWithError(err: Error) {
process.exit(1);
}

function socket(message: string) {
console.log(chalk`{inverse ${message}}`);
}

function channelMessage(channel: string, message: string): void {
console.log(chalk`{bgGreen.black ${channel} }: ${message}`);
}
Expand Down

0 comments on commit f0bc52c

Please sign in to comment.