-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.mjs
executable file
·249 lines (211 loc) · 7.66 KB
/
cli.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/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";
// Create __dirname for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Define colors based on your portfolio
const colors = {
no: "#64ffda", // Highlighted text color for numbers (light teal)
city: "#a3be8c", // Light green for city names
time: "#b48ead", // Light violet for time
country: "#64ffda", // Light teal for country name
border: "#4C566A", // Medium gray for table borders
margin: "\n", // Add margin between tables
};
// Paths for country data files
const COUNTRY_TIMEZONE_FILE = path.join(__dirname, "countries_timezones.json");
// 1. Get Country Timezones (fetch timezones based on country, case-insensitive)
const getCountryTimezones = (country) => {
if (!fs.existsSync(COUNTRY_TIMEZONE_FILE)) {
console.error(chalk.red("Country to timezone mapping file not found."));
process.exit(1);
}
const countryTimezones = JSON.parse(fs.readFileSync(COUNTRY_TIMEZONE_FILE));
const normalizedCountry = country.toLowerCase();
const normalizedTimezones = Object.keys(countryTimezones).reduce(
(acc, key) => {
acc[key.toLowerCase()] = countryTimezones[key];
return acc;
},
{}
);
if (!normalizedTimezones[normalizedCountry]) {
console.error(
chalk.red(`No timezones found for ${country}. Please check the country name or try another country.`)
);
process.exit(1);
}
return normalizedTimezones[normalizedCountry];
};
// 2. Show time for a specific country
const showTimeForCountry = async (country, isFirstCountry = false) => {
console.log(`Fetching time for ${country}...`);
try {
const timezones = getCountryTimezones(country);
// Add a margin before the first table
if (isFirstCountry) {
console.log(colors.margin); // Add space before the first table
}
// Create a table with thick borders and matching colors
const table = new Table({
head: [
chalk.hex(colors.no).bold("No"), // Light teal for headers
chalk.hex(colors.city).bold("City"), // Light green for headers
chalk.hex(colors.time).bold("Time"), // Light violet for headers
],
style: {
head: ["cyan"], // Default style
border: [], // No border color in cli-table3, but chars will have color
},
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)("│"),
},
});
// Populate the table with timezones and human-readable times
timezones.forEach((timezone, index) => {
const currentTime = new Date().toLocaleString("en-US", {
timeZone: timezone,
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: true,
});
// Extract city name from timezone (e.g., America/Toronto -> Toronto)
const cityName = timezone.split("/")[1].replace("_", " "); // Replace _ with space for better readability
// Add a row to the table with portfolio colors
table.push([
chalk.hex(colors.no).bold(index + 1), // No (light teal)
chalk.hex(colors.city)(cityName), // City (light green)
chalk.hex(colors.time)(currentTime), // Time (light violet)
]);
});
// Styled country output
const styledCountry = chalk.hex(colors.country).bold(country.toUpperCase()); // Light teal for country name
const styledText = chalk.bold(`Current time for ${styledCountry}:`);
console.log(colors.margin + styledText); // Add margin before and print country title
console.log(table.toString()); // Display the custom table
// Add margin after each table
console.log(colors.margin); // Add a blank line after each country's table
} catch (error) {
console.error(`Error fetching time for ${country}.`, error);
}
};
// 3. Show time for popular countries
const showPopularCountriesTime = async () => {
console.log(chalk.green("Displaying popular countries and their times..."));
const popularCountries = [
"United States",
"United Kingdom",
"Pakistan",
"Australia",
"Canada",
"Germany",
];
for (const country of popularCountries) {
await showTimeForCountry(country, true); // Ensure we wait for each country's time to be displayed
}
process.exit(0); // Exit after showing times
};
// 4. Show time for all countries
const showAllCountriesTime = async () => {
console.log(chalk.blue("Displaying all countries and their times..."));
// Load the country timezones
const countryTimezones = JSON.parse(fs.readFileSync(COUNTRY_TIMEZONE_FILE));
// Create a Set to track processed timezones to avoid duplicates
const processedCountries = new Set();
// Loop through all country names and codes in the file
for (const [countryNameOrCode, timezones] of Object.entries(
countryTimezones
)) {
// Skip if this set of timezones has already been processed
const timezoneKey = JSON.stringify(timezones);
if (processedCountries.has(timezoneKey)) {
continue;
}
// Mark this set of timezones as processed
processedCountries.add(timezoneKey);
// Display the time for the current country (name or code)
await showTimeForCountry(countryNameOrCode);
}
process.exit(0); // Exit after displaying all times
};
// 5. Show supported countries list
const showSupportedCountries = () => {
console.log(chalk.hex("#64ffda").bold("\nSupported Countries:\n"));
console.log(chalk.green.bold("wtime <country name/country code>:\n"));
try {
// Use fs to read JSON file synchronously instead of dynamic import
const countryTimezones = JSON.parse(
fs.readFileSync(COUNTRY_TIMEZONE_FILE)
);
const countryList = Object.keys(countryTimezones);
// Loop through and print each country
countryList.forEach((country) => {
console.log(chalk.hex("#a3be8c")(country));
});
console.log("\n");
} catch (error) {
console.error(chalk.red("Error loading countries list."));
}
};
// 6. Parse CLI arguments manually
const args = process.argv.slice(2);
const command = args[0];
const country = args[1];
// Handle different commands
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:
if (command) {
showTimeForCountry(command);
} else {
showPopularCountriesTime();
}
break;
}