diff --git a/src/utils/ps-manager.ts b/src/utils/ps-manager.ts new file mode 100644 index 0000000..0e627ea --- /dev/null +++ b/src/utils/ps-manager.ts @@ -0,0 +1,30 @@ +import globals from '~/globals'; +import { IOfType } from '~/types'; +import logger from './logger'; + +const processes: IOfType> = globals('processses', {}).get(); + +function remove(pid: number): void { + try { + delete processes[pid]; + } catch (_) { + logger.error(`Removal of child process ${pid} failed`); + } +} + +export default { + add(pid: number, promise: Promise): void { + processes[pid] = promise.then(() => remove(pid)).catch(() => remove(pid)); + }, + kill(signal: string): void { + Object.keys(processes) + .map(Number) + .forEach((pid) => process.kill(pid, signal)); + }, + isDone(): boolean { + return !Object.keys(processes).length; + }, + async promise(): Promise { + await Promise.all(Object.values(processes)); + } +};