|
| 1 | +import { EventEmitter } from 'node:events'; |
| 2 | + |
| 3 | + |
| 4 | +export enum Event { |
| 5 | + STDOUT = 'stdout', |
| 6 | + STDERR = 'stderr', |
| 7 | + PLUGIN_STDOUT = 'plugin_stdout', |
| 8 | + PLUGIN_STDERR = 'plugin_stderr', |
| 9 | + DEBUG = 'debug', |
| 10 | + OUTPUT = 'output', |
| 11 | + PROCESS_START = 'process_start', |
| 12 | + PROCESS_FINISH = 'process_finish', |
| 13 | + SUB_PROCESS_START = 'sub_process_start', |
| 14 | + SUB_PROCESS_FINISH = 'sub_process_finish', |
| 15 | +} |
| 16 | + |
| 17 | +export const ctx = new class { |
| 18 | + emitter: EventEmitter; |
| 19 | + |
| 20 | + constructor() { |
| 21 | + this.emitter = new EventEmitter(); |
| 22 | + this.attachOutputEmitters(); |
| 23 | + } |
| 24 | + |
| 25 | + on(eventName: string | symbol, listener: (...args: any[]) => void): EventEmitter { |
| 26 | + return this.emitter.on(eventName, listener); |
| 27 | + } |
| 28 | + |
| 29 | + log(...args: unknown[]) { |
| 30 | + this.emitter.emit(Event.STDOUT, ...args); |
| 31 | + } |
| 32 | + |
| 33 | + pluginStdout(...args: unknown[]) { |
| 34 | + this.emitter.emit(Event.PLUGIN_STDOUT, ...args); |
| 35 | + } |
| 36 | + |
| 37 | + pluginStderr(...args: unknown[]) { |
| 38 | + this.emitter.emit(Event.PLUGIN_STDERR, ...args); |
| 39 | + } |
| 40 | + |
| 41 | + debug(...args: unknown[]) { |
| 42 | + // Add filtering here to only allow debug events when in debug mode |
| 43 | + this.emitter.emit(Event.DEBUG, ...args); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + processStarted(name: string) { |
| 48 | + this.emitter.emit(Event.PROCESS_START, name); |
| 49 | + } |
| 50 | + |
| 51 | + processFinished(name: string) { |
| 52 | + this.emitter.emit(Event.PROCESS_FINISH, name); |
| 53 | + } |
| 54 | + |
| 55 | + subprocessStarted(name: string, processName: string) { |
| 56 | + this.emitter.emit(Event.SUB_PROCESS_START, name, processName); |
| 57 | + } |
| 58 | + |
| 59 | + subprocessFinished(name: string, processName: string) { |
| 60 | + this.emitter.emit(Event.SUB_PROCESS_FINISH, name, processName); |
| 61 | + } |
| 62 | + |
| 63 | + async subprocess<T>(name: string, run: () => Promise<T>): Promise<T> { |
| 64 | + this.emitter.emit(Event.SUB_PROCESS_START, name); |
| 65 | + const result = await run(); |
| 66 | + this.emitter.emit(Event.SUB_PROCESS_FINISH, name); |
| 67 | + return result; |
| 68 | + } |
| 69 | + |
| 70 | + attachOutputEmitters() { |
| 71 | + this.emitter.prependListener(Event.STDOUT, (...args) => this.onOutputEvent(...args)); |
| 72 | + this.emitter.prependListener(Event.STDERR, (...args) => this.onOutputEvent(...args)); |
| 73 | + this.emitter.prependListener(Event.PLUGIN_STDOUT, (...args) => this.onOutputEvent(...args)); |
| 74 | + this.emitter.prependListener(Event.PLUGIN_STDERR, (...args) => this.onOutputEvent(...args)); |
| 75 | + this.emitter.prependListener(Event.DEBUG, (...args) => this.onOutputEvent(...args)); |
| 76 | + } |
| 77 | + |
| 78 | + onOutputEvent(...args: unknown[]) { |
| 79 | + this.emitter.emit(Event.OUTPUT, ...args); |
| 80 | + } |
| 81 | +} |
0 commit comments