uiService.js
1 import boxen from "boxen"; 2 import chalk from "chalk"; 3 import inquirer from "inquirer"; 4 import { createSpinner } from "nanospinner"; 5 6 import { ASCII_ART } from "../constants/ascii.js"; 7 8 function show(msg) { 9 console.log(msg); 10 } 11 12 export class UiService { 13 showSuccessMessage = (message) => { 14 show( 15 boxen(chalk.green(message), { 16 padding: 1, 17 margin: 1, 18 borderStyle: "round", 19 borderColor: "green", 20 title: "â SUCCESS", 21 titleAlignment: "center", 22 }), 23 ); 24 }; 25 26 showErrorMessage = (message) => { 27 show( 28 boxen(chalk.red(message), { 29 padding: 1, 30 margin: 1, 31 borderStyle: "round", 32 borderColor: "red", 33 title: "â ERROR", 34 titleAlignment: "center", 35 }), 36 ); 37 }; 38 39 showInfoMessage = (message) => { 40 show( 41 boxen(chalk.cyan(message), { 42 padding: 1, 43 margin: 1, 44 borderStyle: "round", 45 borderColor: "cyan", 46 title: "âšī¸ INFO", 47 titleAlignment: "center", 48 }), 49 ); 50 }; 51 52 showLogo = () => { 53 console.log("\n" + chalk.cyanBright(ASCII_ART)); 54 }; 55 56 askMultipleChoice = async (message, choices) => { 57 var counter = 1; 58 var promptChoices = []; 59 choices.forEach(function (choice) { 60 promptChoices.push(`${counter}. ${choice.label}`); 61 counter++; 62 }); 63 64 const { choice } = await inquirer.prompt([ 65 { 66 type: "list", 67 name: "choice", 68 message: message, 69 choices: promptChoices, 70 pageSize: counter - 1, 71 loop: true, 72 }, 73 ]); 74 75 const selectStr = choice.split(".")[0]; 76 const selectIndex = parseInt(selectStr) - 1; 77 78 await choices[selectIndex].action(); 79 }; 80 81 askPrompt = async (prompt) => { 82 const response = await inquirer.prompt([ 83 { 84 type: "input", 85 name: "valueStr", 86 message: prompt, 87 }, 88 ]); 89 return response.valueStr; 90 }; 91 92 createAndStartSpinner = (message) => { 93 return createSpinner(message).start(); 94 }; 95 96 stopSpinnerSuccess = (spinner) => { 97 if (spinner == undefined) return; 98 spinner.stop(); 99 }; 100 101 stopSpinnerError = (spinner) => { 102 if (spinner == undefined) return; 103 spinner.error(); 104 }; 105 }