|
| 1 | +import { PlanResponseData } from 'codify-schemas'; |
| 2 | +import readline from 'node:readline'; |
| 3 | +import createDebug, { Debugger } from 'debug'; |
| 4 | + |
| 5 | +import { ctx, Event } from '../../events/context.js'; |
| 6 | +import { Reporter } from './reporter.js'; |
| 7 | + |
| 8 | +const debug = createDebug('codify'); |
| 9 | + |
| 10 | +export class DebugReporter implements Reporter { |
| 11 | + private readonly rl = readline.createInterface(process.stdin, process.stdout); |
| 12 | + private debuggerCache = new Map<string, Debugger>(); |
| 13 | + |
| 14 | + constructor() { |
| 15 | + ctx.on(Event.PLUGIN_STDOUT, (name, args) => this.getPluginDebug(name)(args)); |
| 16 | + ctx.on(Event.PLUGIN_STDERR, (name, args) => this.getPluginDebug(name)(args)); |
| 17 | + ctx.on(Event.STDOUT, (args) => debug(args)); |
| 18 | + ctx.on(Event.STDERR, (args) => debug(args)); |
| 19 | + ctx.on(Event.DEBUG, (args) => debug(args)); |
| 20 | + ctx.on(Event.PROCESS_START, (name) => debug(name)) |
| 21 | + ctx.on(Event.PROCESS_FINISH, (name) => debug(name)) |
| 22 | + ctx.on(Event.SUB_PROCESS_START, (name) => debug(name)) |
| 23 | + ctx.on(Event.SUB_PROCESS_FINISH, (name) => debug(name)) |
| 24 | + } |
| 25 | + |
| 26 | + async promptApplyConfirmation(): Promise<boolean> { |
| 27 | + const response = await new Promise((resolve) => { |
| 28 | + this.rl.question('Is this okay?\n', (answer) => resolve(answer)); |
| 29 | + }); |
| 30 | + |
| 31 | + return response === 'yes'; |
| 32 | + } |
| 33 | + |
| 34 | + displayPlan(plan: PlanResponseData[]): void { |
| 35 | + console.log(JSON.stringify(plan)); |
| 36 | + } |
| 37 | + |
| 38 | + private getPluginDebug(name: string): Debugger { |
| 39 | + const debuggerName = `plugin:${name}`; |
| 40 | + |
| 41 | + if (!this.debuggerCache.has(name)) { |
| 42 | + this.debuggerCache.set(name, createDebug(debuggerName)); |
| 43 | + } |
| 44 | + |
| 45 | + return this.debuggerCache.get(name)!; |
| 46 | + } |
| 47 | +} |
0 commit comments