UNPKG

monex

Version:

Execute one or multiple scripts, interactively or in daemon mode, and restart them whenever they crash or a watched file changes.

47 lines (46 loc) 1.3 kB
/* IMPORT */ import process from 'node:process'; import Events from './events.js'; /* MAIN */ class Stdin { /* CONSTRUCTOR */ constructor() { /* LIFECYCLE */ this.init = () => { process.stdin.on('data', this.onData); }; this.destroy = () => { process.stdin.off('data', this.onData); }; this.refresh = () => { const isListeningNext = !!this.events.get('restart').length; if (this.isListening === isListeningNext) return; this.isListening = isListeningNext; if (this.isListening) { this.init(); } else { this.destroy(); } }; /* API */ this.onData = (data) => { if (data.toString().trim().toLowerCase() !== 'rs') return; this.events.emit('restart'); }; this.onRestart = (listener) => { const disposer = this.events.on('restart', listener); this.refresh(); return () => { disposer(); this.refresh(); }; }; this.events = new Events(); this.isListening = false; } } /* EXPORT */ export default new Stdin();