import process from 'node:process'; /** * Result from creating a prompt interface */ export interface IPromptInterface { /** Function to prompt for user input */ prompt: (question: string) => Promise; /** Function to close the prompt interface */ close: () => void; } /** * Create a readline prompt interface for interactive CLI input * @returns Promise resolving to prompt function and close function */ export async function createPrompt(): Promise { const readline = await import('node:readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const prompt = (question: string): Promise => { return new Promise((resolve) => { rl.question(question, (answer: string) => { resolve(answer); }); }); }; const close = (): void => { rl.close(); process.stdin.destroy(); }; return { prompt, close }; } /** * Run an async function with a prompt interface, ensuring cleanup * @param fn Function to run with the prompt interface * @returns Promise resolving to the function's return value */ export async function withPrompt( fn: (prompt: (question: string) => Promise) => Promise, ): Promise { const { prompt, close } = await createPrompt(); try { return await fn(prompt); } finally { close(); } }