|
| 1 | +import boxen from "boxen"; |
| 2 | +import chalk from "chalk"; |
| 3 | +import inquirer from "inquirer"; |
| 4 | +import { titleScreen } from "pickitt"; |
| 5 | + |
| 6 | +import { blankBoxenStyle } from "./constants"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Displays Main Menu to user. |
| 10 | + * @param {AppState} state State of application. |
| 11 | + * @returns {Promise} Resolves with menuAction value. |
| 12 | + */ |
| 13 | +export const displayMainMenu = (state) => |
| 14 | + new Promise(async (resolve, reject) => { |
| 15 | + try { |
| 16 | + const { menuAction } = await inquirer.prompt([ |
| 17 | + { |
| 18 | + type: "list", |
| 19 | + message: "Main Menu", |
| 20 | + name: "menuAction", |
| 21 | + choices: [ |
| 22 | + { value: "option1", name: "Option 1" }, |
| 23 | + { value: "option2", name: "Option 2" }, |
| 24 | + { value: "option3", name: "Option 3" }, |
| 25 | + new inquirer.Separator(), |
| 26 | + { value: "about", name: "About" }, |
| 27 | + { value: "exit", name: "Exit" }, |
| 28 | + ], |
| 29 | + }, |
| 30 | + ]); |
| 31 | + state.menuAction = menuAction; |
| 32 | + resolve(menuAction); |
| 33 | + } catch (e) { |
| 34 | + reject(e); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | +/** |
| 39 | + * Pauses the process execution and waits for the user to hit a key. |
| 40 | + * @returns {Promise} Resolves when user has entered a keystroke. |
| 41 | + * @async |
| 42 | + */ |
| 43 | +const keypress = async () => { |
| 44 | + try { |
| 45 | + process.stdin.setRawMode(true); |
| 46 | + return new Promise((resolve, reject) => { |
| 47 | + try { |
| 48 | + process.stdin.resume(); |
| 49 | + process.stdin.once("data", () => { |
| 50 | + process.stdin.setRawMode(false); |
| 51 | + resolve(); |
| 52 | + }); |
| 53 | + } catch (e) { |
| 54 | + return reject(e); |
| 55 | + } |
| 56 | + }); |
| 57 | + } catch (e) { |
| 58 | + throw new Error(e); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Interprets user selected menu action. |
| 64 | + * @param {AppState} state State of application. |
| 65 | + * @returns {Promise} |
| 66 | + */ |
| 67 | +export const interpretMenuAction = async (state) => { |
| 68 | + try { |
| 69 | + if (state.menuAction === null) { |
| 70 | + throw new Error("menuAction can not be `null`"); |
| 71 | + } |
| 72 | + const actions = { |
| 73 | + about: async (state) => { |
| 74 | + await titleScreen("APP NAME"); |
| 75 | + console.log( |
| 76 | + boxen(chalk.yellow(`Author: `) + "YOUR NAME", blankBoxenStyle) |
| 77 | + ); |
| 78 | + |
| 79 | + console.log("Press any key to return to Main Menu ..."); |
| 80 | + await keypress(); |
| 81 | + state.menuActionEmitter.emit("actionCompleted", state); |
| 82 | + }, |
| 83 | + option1: async (state) => { |
| 84 | + await titleScreen("APP NAME"); |
| 85 | + console.log("Option 1 Logic would take place here :)"); |
| 86 | + console.log(""); |
| 87 | + |
| 88 | + console.log("Press any key to return to Main Menu ..."); |
| 89 | + await keypress(); |
| 90 | + state.menuActionEmitter.emit("actionCompleted", state); |
| 91 | + }, |
| 92 | + option2: async (state) => { |
| 93 | + await titleScreen("APP NAME"); |
| 94 | + console.log("Option 2 Logic would take place here :)"); |
| 95 | + console.log(""); |
| 96 | + |
| 97 | + console.log("Press any key to return to Main Menu ..."); |
| 98 | + await keypress(); |
| 99 | + state.menuActionEmitter.emit("actionCompleted", state); |
| 100 | + }, |
| 101 | + option3: async (state) => { |
| 102 | + await titleScreen("APP NAME"); |
| 103 | + console.log("Option 3 Logic would take place here :)"); |
| 104 | + console.log(""); |
| 105 | + |
| 106 | + console.log("Press any key to return to Main Menu ..."); |
| 107 | + await keypress(); |
| 108 | + state.menuActionEmitter.emit("actionCompleted", state); |
| 109 | + }, |
| 110 | + exit: (state) => process.exit(), |
| 111 | + }; |
| 112 | + |
| 113 | + await actions[state.menuAction](state); |
| 114 | + } catch (e) { |
| 115 | + throw new Error(e); |
| 116 | + } |
| 117 | +}; |
0 commit comments