UNPKG

@contextvm/ctxcn

Version:

A command-line utility inspired by shadcn that streamlines the integration of ContextVM (CVM) servers into your TypeScript projects

77 lines 2.52 kB
import * as readline from "readline"; let rl = null; export function createReadlineInterface() { if (!rl) { rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); } return rl; } export function closeReadlineInterface() { if (rl) { rl.close(); rl = null; } } export function askQuestion(query, defaultValue) { const readlineInterface = createReadlineInterface(); return new Promise((resolve) => { try { readlineInterface.question(`${query} (${defaultValue}): `, (answer) => { resolve(answer || defaultValue); }); } catch (error) { console.error("Error reading input:", error); resolve(defaultValue); } }); } export function askYesNo(query, defaultValue) { const readlineInterface = createReadlineInterface(); return new Promise((resolve) => { try { const defaultStr = defaultValue ? "Y/n" : "y/N"; readlineInterface.question(`${query} (${defaultStr}): `, (answer) => { if (!answer) { resolve(defaultValue); } else { resolve(answer.toLowerCase() === "y" || answer.toLowerCase() === "yes"); } }); } catch (error) { console.error("Error reading input:", error); resolve(defaultValue); } }); } export function askChoice(query, options, defaultIndex = 0) { const readlineInterface = createReadlineInterface(); return new Promise((resolve) => { try { console.log(query); options.forEach((option, index) => { const marker = index === defaultIndex ? " (default)" : ""; console.log(` ${index + 1}. ${option}${marker}`); }); readlineInterface.question(`Choose an option (1-${options.length})`, (answer) => { const choice = parseInt(answer); if (isNaN(choice) || choice < 1 || choice > options.length) { resolve(options[defaultIndex] || ""); } else { resolve(options[choice - 1] || ""); } }); } catch (error) { console.error("Error reading input:", error); resolve(options[defaultIndex] || ""); } }); } //# sourceMappingURL=cli.js.map