datahaven/test/utils/input.ts
Tim B 7ac340e465
ci: 🪓 Cutting BS (#43)
Due to our constrained CI resources available to us, this PR disables
`blockscout` from running in the CI.

This PR makes it so `minimal` runs don't use blockscout, and changes the
CI to use this instead of the verified network.
2025-04-15 20:06:17 +01:00

51 lines
1.6 KiB
TypeScript

import readline from "node:readline";
import chalk from "chalk";
// Helper function to create an interactive prompt with timeout
export const promptWithTimeout = async (
question: string,
defaultValue: boolean,
timeoutSeconds: number
): Promise<boolean> => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise<boolean>((resolve) => {
const defaultText = defaultValue ? "Y/n" : "y/N";
// Create a visually striking prompt
const border = chalk.yellow("=".repeat(question.length + 40));
console.log("\n");
console.log(border);
console.log(chalk.yellow("▶ ") + chalk.bold.cyan(question));
console.log(
chalk.magenta(
`⏱ Will default to ${chalk.bold(defaultValue ? "YES" : "NO")} in ${chalk.bold(timeoutSeconds)} seconds`
)
);
console.log(border);
const fullQuestion = chalk.green(`\n➤ Please enter your choice [${chalk.bold(defaultText)}]: `);
const timer = setTimeout(() => {
console.log(
`\n${chalk.yellow("⏱")} ${chalk.bold("Timeout reached, using default:")} ${chalk.green(defaultValue ? "YES" : "NO")}\n`
);
rl.close();
resolve(defaultValue);
}, timeoutSeconds * 1000);
rl.question(fullQuestion, (answer) => {
clearTimeout(timer);
rl.close();
if (answer.trim() === "") {
resolve(defaultValue);
} else {
const normalizedAnswer = answer.trim().toLowerCase();
console.log("");
resolve(normalizedAnswer === "y" || normalizedAnswer === "yes");
}
});
});
};