|
| 1 | +import { EventEmitter } from 'node:events'; |
| 2 | + |
| 3 | +export enum Event { |
| 4 | + STDOUT = 'stdout', |
| 5 | + STDERR = 'stderr', |
| 6 | + PLUGIN_STDOUT = 'plugin_stdout', |
| 7 | + PLUGIN_STDERR = 'plugin_stderr', |
| 8 | + DEBUG = 'debug', |
| 9 | + OUTPUT = 'output', |
| 10 | + PROCESS_START = 'process_start', |
| 11 | + PROCESS_FINISH = 'process_finish', |
| 12 | + SUB_PROCESS_START = 'sub_process_start', |
| 13 | + SUB_PROCESS_FINISH = 'sub_process_finish', |
| 14 | +} |
| 15 | + |
| 16 | +export enum ProcessName { |
| 17 | + PLAN = 'plan', |
| 18 | + APPLY = 'apply' |
| 19 | +} |
| 20 | + |
| 21 | +export enum SubProcessName { |
| 22 | + PARSE = 'parse', |
| 23 | + INITIALIZE_PLUGINS = 'initialize_plugins', |
| 24 | + VALIDATE = 'validate', |
| 25 | + GENERATE_PLAN = 'generate_plan', |
| 26 | + APPLYING_RESOURCE = 'apply_resource', |
| 27 | +} |
| 28 | + |
| 29 | +export const ctx = new class { |
| 30 | + emitter: EventEmitter; |
| 31 | + |
| 32 | + constructor() { |
| 33 | + this.emitter = new EventEmitter(); |
| 34 | + this.attachOutputEmitters(); |
| 35 | + } |
| 36 | + |
| 37 | + on(eventName: string | symbol, listener: (...args: any[]) => void): EventEmitter { |
| 38 | + return this.emitter.on(eventName, listener); |
| 39 | + } |
| 40 | + |
| 41 | + log(...args: unknown[]) { |
| 42 | + this.emitter.emit(Event.STDOUT, ...args); |
| 43 | + } |
| 44 | + |
| 45 | + pluginStdout(...args: unknown[]) { |
| 46 | + this.emitter.emit(Event.PLUGIN_STDOUT, ...args); |
| 47 | + } |
| 48 | + |
| 49 | + pluginStderr(...args: unknown[]) { |
| 50 | + this.emitter.emit(Event.PLUGIN_STDERR, ...args); |
| 51 | + } |
| 52 | + |
| 53 | + debug(...args: unknown[]) { |
| 54 | + const debug = process.env.DEBUG; |
| 55 | + if (!debug?.toLowerCase().includes('codify') && !debug?.includes('*')) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + this.emitter.emit(Event.DEBUG, ...args); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + processStarted(name: string) { |
| 64 | + this.emitter.emit(Event.PROCESS_START, name); |
| 65 | + } |
| 66 | + |
| 67 | + processFinished(name: string) { |
| 68 | + this.emitter.emit(Event.PROCESS_FINISH, name); |
| 69 | + } |
| 70 | + |
| 71 | + subprocessStarted(name: string, additionalName?: string) { |
| 72 | + this.emitter.emit(Event.SUB_PROCESS_START, name, additionalName); |
| 73 | + } |
| 74 | + |
| 75 | + subprocessFinished(name: string, additionalName?: string) { |
| 76 | + this.emitter.emit(Event.SUB_PROCESS_FINISH, name, additionalName); |
| 77 | + } |
| 78 | + |
| 79 | + async subprocess<T>(name: string, run: () => Promise<T>): Promise<T> { |
| 80 | + this.emitter.emit(Event.SUB_PROCESS_START, name); |
| 81 | + const result = await run(); |
| 82 | + this.emitter.emit(Event.SUB_PROCESS_FINISH, name); |
| 83 | + return result; |
| 84 | + } |
| 85 | + |
| 86 | + attachOutputEmitters() { |
| 87 | + this.emitter.prependListener(Event.STDOUT, (...args) => this.onOutputEvent(...args)); |
| 88 | + this.emitter.prependListener(Event.STDERR, (...args) => this.onOutputEvent(...args)); |
| 89 | + this.emitter.prependListener(Event.PLUGIN_STDOUT, (...args) => this.onOutputEvent(...args)); |
| 90 | + this.emitter.prependListener(Event.PLUGIN_STDERR, (...args) => this.onOutputEvent(...args)); |
| 91 | + this.emitter.prependListener(Event.DEBUG, (...args) => this.onOutputEvent(...args)); |
| 92 | + } |
| 93 | + |
| 94 | + onOutputEvent(...args: unknown[]) { |
| 95 | + this.emitter.emit(Event.OUTPUT, ...args); |
| 96 | + } |
| 97 | +} |
0 commit comments