nodemss
Version:
Interactive cli tool to list and run start multiple services simultainously
108 lines • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const readline_1 = require("readline");
class CommandHandler {
constructor() {
this.readline = readline_1.createInterface({
input: process.stdin,
output: process.stdout
});
}
listenToCommand(main) {
this.main = main;
console.log(`Waiting for command. Type '?' for help`);
this.readline.on('line', (input) => {
this.commandInterpreter(input);
});
this.readline.on('close', () => {
console.log(`Bye!!! All opened processess may be still running`);
});
this.readline.on('SIGCONT', () => {
console.log(`Waiting for command. Type '?' for help`);
});
this.readline.on('SIGINT', () => {
this.readline.close();
});
}
commandInterpreter(command) {
let subCommand = '';
let args = '';
let firstIndexOfSpace = command.trim().indexOf(' ');
if (firstIndexOfSpace < 0) {
subCommand = command;
}
else {
subCommand = command.split(' ', 1)[0];
args = command.substring(firstIndexOfSpace + 1, command.length);
}
switch (subCommand) {
case '?':
this.showHelp();
break;
case 'exit':
this.readline.close();
break;
case 'bye':
this.readline.close();
break;
case 'listAll':
this.listAll();
break;
case 'kill':
this.kill(args);
break;
case 'killAll':
this.main.killAll();
break;
case 'start':
this.main.start(args);
break;
case 'startAll':
this.main.startAll();
break;
case 'find':
this.find(args);
break;
default:
this.showHelp();
break;
}
}
showHelp() {
console.log(`Showing options:`);
console.log(`?: \tShows help screen.`);
console.log(`find [xxxx]: \tFinds the process matching the pattern xxxx`);
console.log(`kill xxxx: \tKills the process associated with PID xxxx`);
console.log(`killAll: \tKills all process started and their status`);
console.log(`listAll: \tLists all process started and their status`);
console.log(`start service name: \tStarts the service identified by the name. If the service is started already, it will ignore the command`);
console.log(`startAll: \tStarts all the services. If the service is started already, it will ignore the command`);
console.log(`exit || bye: \tExits the programme. !!All opened processess may be still running!!`);
console.log();
}
listAll() {
console.log(`PID \tNAME \tSTATUS`);
console.log(`---------------------------------------------------------------------------`);
this.main.getRunningProcessess().forEach((state) => {
console.log(`${state.processId}\t${state.name}\t${state.status}`);
});
}
kill(command) {
console.log(command);
this.main.kill(Number(command));
}
find(name) {
console.log(`NAME`);
console.log(`-----`);
this.main.getProcessFromConfigFileByLikeName(name).then((value) => {
value.forEach((process) => {
console.log(process.name);
});
console.log();
}).catch((error) => {
console.log(error);
});
}
}
exports.CommandHandler = CommandHandler;
//# sourceMappingURL=command-handler.js.map