nodemss
Version:
Interactive cli tool to list and run start multiple services simultainously
172 lines • 6.78 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
const process_status_1 = require("../models/process-status");
class Main {
constructor(config) {
this.runningProcessList = new Array();
this.config = config;
}
run() {
this.config.processess.forEach((process) => {
this.runProcess(process);
});
}
runProcess(process) {
return __awaiter(this, void 0, void 0, function* () {
const options = {
shell: true,
detached: true,
timeout: 20000,
stdio: ['ignore', 'ignore', 'ignore']
};
const command = child_process_1.spawn(process.command || this.config.defaultCommand, process.arguments || this.config.defaultArguments || [], process.options || this.config.defaultOptions || options);
const processState = {
processId: command.pid,
name: process.name,
status: process_status_1.ProcessStatus.RUNNING,
process: command
};
yield this.loadRunningProcessByName(process.name).then((state) => {
state.processId = command.pid;
state.process = command;
state.status = process_status_1.ProcessStatus.RUNNING;
}).catch(error => {
this.runningProcessList.push(processState);
});
command.unref();
});
}
start(name) {
this.loadRunningProcessByName(name).then((runningProcess) => {
if (runningProcess.status !== process_status_1.ProcessStatus.RUNNING) {
this.findProcessFromConfigFileByName(name).then((process) => {
this.runProcess(process);
}).catch(error => {
console.error(error);
});
}
else {
console.log(`${name} already running. Hence ignoring the command`);
}
}).catch(error => {
console.log(error);
});
}
startAll() {
this.config.processess.forEach((process) => {
this.start(process.name);
});
}
kill(pid) {
this.loadRunningProcess(pid).then((childProcess) => {
if (childProcess.status === process_status_1.ProcessStatus.RUNNING) {
if (process.platform === 'win32') {
child_process_1.exec(`taskkill -F -T -PID ${pid}`, (error, stdout, stderr) => {
if (error) {
console.log(`Error: ${error}`);
}
console.log(`${stdout}`);
console.log(`${stderr}`);
});
}
else {
childProcess.process ? childProcess.process.ref() : null;
childProcess.process ? childProcess.process.kill('SIGHUP') : null;
}
childProcess.status = process_status_1.ProcessStatus.CLOSED;
}
else {
console.log(`Process ${pid} is not in running state.`);
}
}).catch(error => {
console.error(error);
});
}
killAll() {
this.runningProcessList.forEach((childProcess) => {
this.kill(childProcess.processId);
});
}
loadRunningProcess(pid) {
let found = false;
return new Promise((resolve, reject) => {
this.runningProcessList.forEach((state) => {
if (state.processId === pid) {
found = true;
resolve(state);
}
});
if (!found) {
reject(`Process ${pid} not found`);
}
});
}
loadRunningProcessByName(name) {
let found = false;
return new Promise((resolve, reject) => {
this.runningProcessList.forEach((state) => {
if (state.name === name) {
found = true;
resolve(state);
}
});
if (!found) {
console.log(`Unable to find process ${name} in list. Loading from config file...`);
this.findProcessFromConfigFileByName(name).then((value) => {
const processState = {
processId: 0,
name: value.name,
status: process_status_1.ProcessStatus.UNKNOWN
};
this.runningProcessList.push(processState);
found = true;
resolve(processState);
}).catch(error => {
reject(error);
});
}
});
}
findProcessFromConfigFileByName(name) {
let found = false;
return new Promise((resolve, reject) => {
this.config.processess.forEach((process) => {
if (process.name === name) {
found = true;
resolve(process);
}
});
if (!found) {
reject(`Unable to find process ${name} in Config File.`);
}
});
}
getRunningProcessess() {
return this.runningProcessList;
}
getProcessFromConfigFileByLikeName(name) {
return new Promise((resolve, reject) => {
const returnArray = new Array();
this.config.processess.forEach((process) => {
if (process.name.match(name)) {
returnArray.push(process);
}
});
resolve(returnArray);
});
}
filterConfigArray(element, index, array) {
}
}
exports.Main = Main;
//# sourceMappingURL=main.js.map