-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.min.mjs
executable file
·2 lines (2 loc) · 3.78 KB
/
cli.min.mjs
1
2
#!/usr/bin/env node
import{fileURLToPath}from"url";import path from"path";import chalk from"chalk";import fs from"fs";import Table from"cli-table3";let __filename=fileURLToPath(import.meta.url),__dirname=path.dirname(__filename),colors={no:"#64ffda",city:"#a3be8c",time:"#b48ead",country:"#64ffda",border:"#4C566A",margin:"\n"},COUNTRY_TIMEZONE_FILE=path.join(__dirname,"countries_timezones.json"),getCountryTimezones=o=>{fs.existsSync(COUNTRY_TIMEZONE_FILE)||(console.error(chalk.red("Country to timezone mapping file not found.")),process.exit(1));let r=JSON.parse(fs.readFileSync(COUNTRY_TIMEZONE_FILE));var e=o.toLowerCase(),l=Object.keys(r).reduce((o,e)=>(o[e.toLowerCase()]=r[e],o),{});return l[e]||(console.error(chalk.red(`No timezones found for ${o}. Please check the country name or try another country.`)),process.exit(1)),l[e]},showTimeForCountry=async(e,o=!1)=>{console.log(`Fetching time for ${e}...`);try{var r=getCountryTimezones(e);o&&console.log(colors.margin);let l=new Table({head:[chalk.hex(colors.no).bold("No"),chalk.hex(colors.city).bold("City"),chalk.hex(colors.time).bold("Time")],style:{head:["cyan"],border:[]},chars:{top:chalk.hex(colors.border)("═"),"top-mid":chalk.hex(colors.border)("╤"),"top-left":chalk.hex(colors.border)("╔"),"top-right":chalk.hex(colors.border)("╗"),bottom:chalk.hex(colors.border)("═"),"bottom-mid":chalk.hex(colors.border)("╧"),"bottom-left":chalk.hex(colors.border)("╚"),"bottom-right":chalk.hex(colors.border)("╝"),left:chalk.hex(colors.border)("║"),"left-mid":chalk.hex(colors.border)("╟"),mid:chalk.hex(colors.border)("─"),"mid-mid":chalk.hex(colors.border)("┼"),right:chalk.hex(colors.border)("║"),"right-mid":chalk.hex(colors.border)("╢"),middle:chalk.hex(colors.border)("│")}});r.forEach((o,e)=>{var r=(new Date).toLocaleString("en-US",{timeZone:o,weekday:"long",year:"numeric",month:"long",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",hour12:!0}),o=o.split("/")[1].replace("_"," ");l.push([chalk.hex(colors.no).bold(e+1),chalk.hex(colors.city)(o),chalk.hex(colors.time)(r)])});var a=chalk.hex(colors.country).bold(e.toUpperCase()),c=chalk.bold(`Current time for ${a}:`);console.log(colors.margin+c),console.log(l.toString()),console.log(colors.margin)}catch(o){console.error(`Error fetching time for ${e}.`,o)}},showPopularCountriesTime=async()=>{console.log(chalk.green("Displaying popular countries and their times..."));var o;for(o of["United States","United Kingdom","Pakistan","Australia","Canada","Germany"])await showTimeForCountry(o,!0);process.exit(0)},showAllCountriesTime=async()=>{console.log(chalk.blue("Displaying all countries and their times..."));var o,e,r=JSON.parse(fs.readFileSync(COUNTRY_TIMEZONE_FILE)),l=new Set;for([o,e]of Object.entries(r)){var a=JSON.stringify(e);l.has(a)||(l.add(a),await showTimeForCountry(o))}process.exit(0)},showSupportedCountries=()=>{console.log(chalk.hex("#64ffda").bold("\nSupported Countries:\n")),console.log(chalk.green.bold("wtime <country name/country code>:\n"));try{var o=JSON.parse(fs.readFileSync(COUNTRY_TIMEZONE_FILE));Object.keys(o).forEach(o=>{console.log(chalk.hex("#a3be8c")(o))}),console.log("\n")}catch(o){console.error(chalk.red("Error loading countries list."))}},args=process.argv.slice(2),command=args[0],country=args[1];switch(command){case"help":console.log(chalk.hex("#64ffda").bold("\nAvailable Commands:\n")),console.log(chalk.hex("#64ffda")("1. wtime <country>: Show the time for a specific country.")),console.log(chalk.hex("#64ffda")("2. wtime --all: Show the time for all available countries.")),console.log(chalk.hex("#64ffda")("3. wtime list: List all available countries supported by the CLI.")),console.log("\n");break;case"list":showSupportedCountries();break;case"--all":showAllCountriesTime();break;default:command?showTimeForCountry(command):showPopularCountriesTime()}