#!/usr/bin/env node
const ora = require("ora").default;
const { yellow, bgRed } = require("colors");
const { confirm, input } = require("@inquirer/prompts");
const { textSync } = require("figlet");
const { stackMenu } = require("./menu.js");
const { menuWebOpts, webTools } = require("./hash/webTools.js");
const { menuQueryOpts, queryTools } = require("./hash/queryTools.js");
const { infoTools, menuInfoOpts } = require("./hash/infoTools.js");
const { menuUtilityOpts, utilityTools } = require("./hash/utilityTools.js");
const { menuWallpaperOpts, wallpaperSelect } = require("./hash/wallpaperSelect.js");
const { aboutTool, menuAboutOpts } = require("./about.js");
const { exitMsg } = require("./utils.js");
const { quoteSelect, menuQuoteOpts } = require("./hash/quotesSelect.js");
const getStations = require("./functions/getStations.js");
const [spinner, totalTime, pageSize] = [
ora({ spinner: "dots11", color: "white" }),
1e4, 9
];
/** @returns {void} */
const exitCli = () => {
console.clear();
console.info(exitMsg);
};
/** @returns {Promise} */
async function webOpts() {
console.info(yellow(textSync("web options")));
const web = await stackMenu({
pageSize,
message: "enter a web tool option",
choices: menuWebOpts
});
web !== "return main menu"
? webTools[web](returnMain)
: mainMenu();
}
/** @returns {Promise} */
async function infoOpts() {
console.info(yellow(textSync("info options")));
const info = await stackMenu({
pageSize,
message: "enter a info tool option",
choices: menuInfoOpts
});
info === "return main menu"
? mainMenu()
: infoTools[info](returnMain);
}
/** @returns {Promise} */
async function queryOpts() {
console.info(yellow(textSync("query options")));
const query = await stackMenu({
pageSize,
message: "enter a query tool option",
choices: menuQueryOpts
});
query === "return main menu"
? mainMenu()
: queryTools[query](returnMain);
}
/** @returns {Promise} */
async function wallpapersOpts() {
console.info(yellow(textSync("wallpapers")));
const wallpaper = await stackMenu({
pageSize,
message: "enter a wallpaper selector",
choices: menuWallpaperOpts
});
wallpaper === "return main menu"
? mainMenu()
: wallpaperSelect[wallpaper](returnMain, wallpapersOpts);
}
/** @returns {Promise} */
async function quotesOpts() {
console.info(yellow(textSync("quotes")));
const quotes = await stackMenu({
pageSize,
choices: menuQuoteOpts,
message: "enter a quote option"
});
quotes === "return main menu"
? mainMenu()
: quoteSelect[quotes](returnMain);
}
/** @returns {Promise} */
async function utilityOpts() {
console.info(yellow(textSync("utility options")));
const utility = await stackMenu({
pageSize,
message: "enter a utility tool option",
choices: menuUtilityOpts
});
utility === "return main menu"
? mainMenu()
: utilityTools[utility](returnMain);
}
/** @returns {Promise} */
async function aboutOpts() {
console.info(yellow(textSync("About Menu")));
const about = await stackMenu({
pageSize,
message: "select about option info",
choices: menuAboutOpts
});
about !== "return main menu"
? aboutTool[about](aboutOpts)
: mainMenu();
}
/** @returns {Promise} */
async function mainMenu() {
console.clear();
console.info(yellow(textSync("stack-analyze")));
const menuList = {
web() {
console.clear();
webOpts();
},
info() {
console.clear();
infoOpts();
},
query() {
console.clear();
queryOpts();
},
utility() {
console.clear();
utilityOpts();
},
wallpapers() {
console.clear();
wallpapersOpts();
},
quotes() {
console.clear();
quotesOpts();
},
async stations() {
console.warn("some stations not visble only info".yellow);
const country = await input({
message: "enter a country for search:"
});
getStations(country);
setTimeout(returnMain, 5e3);
},
about() {
console.clear();
aboutOpts();
}
};
const option = await stackMenu({
message: "what option do you want to analyze stack:",
choices: [...Object.keys(menuList), "exit"],
pageSize: 10
});
option !== "exit" ? menuList[option]() : exitCli();
}
/** @returns {Promise} */
async function returnMain() {
try {
const returnMain = await confirm({
message: "do you want go to the main menu?",
}).catch();
returnMain ? mainMenu() : exitCli();
} catch (err) {
console.error(bgRed((/** @type {Error} */ (err)).message));
}
}
process.on("uncaughtException", (err) => {
if (err instanceof Error && err.name === "ExitPromptError") {
console.info(" ohh sorry:".yellow, exitMsg);
} else {
throw err;
}
});
for (let i = 50; i < totalTime; i += 50) {
const percentage = i / totalTime * 100;
setTimeout(() => {
spinner.text = `Loading app... ${Math.ceil(percentage)}%`.random;
spinner.start();
}, i);
}
setTimeout(() => {
spinner.stop();
mainMenu();
}, totalTime);