UNPKG

git-bolt

Version:

Git utilities for faster development

54 lines (47 loc) 1.58 kB
import chalk from 'chalk'; import { createInterface } from 'readline'; import { promisify } from 'util'; let rl; export function getReadlineInterface() { if (!rl) { rl = createInterface({ input: process.stdin, output: process.stdout }); } return rl; } export function closeReadlineInterface() { if (rl) { rl.close(); rl = null; } } export async function askQuestion(query) { const readline = getReadlineInterface(); const question = promisify(readline.question).bind(readline); try { const answer = await question(chalk.blue(query)); return answer.trim(); } catch (error) { console.error(chalk.red('Error reading input:'), error); process.exit(1); } } export async function askQuestionWithDefault(query, defaultValue, mask = false) { const readline = getReadlineInterface(); const question = promisify(readline.question).bind(readline); try { let displayDefault = defaultValue; if (mask && defaultValue) { // Show only first 4 and last 4 characters of the token displayDefault = defaultValue.substring(0, 4) + '...' + defaultValue.substring(defaultValue.length - 4); } const prompt = chalk.blue(`${query} (Press Enter to use existing: ${displayDefault}): `); const answer = await question(prompt); return answer.trim() || defaultValue; } catch (error) { console.error(chalk.red('Error reading input:'), error); process.exit(1); } }