-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (174 loc) · 5.07 KB
/
index.js
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
#!/usr/bin/env node
import figlet from "figlet";
import { program } from "commander";
import { createPromptModule } from "inquirer";
import { writeFile, readFile } from "fs/promises";
import fetchWeather from "./weather.js";
import boxen from "boxen";
import chalk from "chalk";
const prompt = createPromptModule();
const listLocationOptions = [
{
type: "list",
name: "locationoption",
message: "Choose how will you inform us about your location:",
choices: ["City Name", "Zip Code"],
default: "City Name",
},
];
const listTemperatureUnit = [
{
type: "list",
name: "unit",
message: `Choose temperature unit:`,
choices: ["°C", "°F"],
default: process.env.UNIT,
},
];
const checkWeather = () => {
prompt(listTemperatureUnit).then((answersUnit) => {
prompt(listLocationOptions).then((answersList) => {
prompt({
type: "input",
name: "data",
message: `Enter ${answersList.locationoption} (Temperature will be in ${answersUnit.unit}):`,
}).then((answersInput) => {
let answers = {
data: answersInput.data,
locationoption: answersList.locationoption,
unit: answersUnit.unit,
};
fetchWeather(answers);
});
});
});
};
function generateAsciiArt(text) {
return new Promise((resolve, reject) => {
figlet(text, function (err, data) {
if (err) {
reject("Error generating ASCII art");
}
resolve(data);
});
});
}
async function displayAppInfo() {
process.stdout.write("\x1Bc");
const appName = "WEATHER-CLI"; // Your app name
const asciiArt = await generateAsciiArt(appName);
console.log(chalk.yellow(asciiArt));
console.log("\n");
console.log(chalk.magenta("Usage: weather-cli weather --search\n"));
console.log(
chalk.green(
boxen("Description: Weather app which fetches weather information.", {
padding: 1,
})
)
);
console.log("\nOptions:");
console.log(" -v, --version Output the current version.");
console.log(" -h, --help Display help for command.");
console.log("\nCommands:");
console.log(" exit");
console.log(
" set-location|sl Allow users to set a default location."
);
console.log(" set-units|su Allow users to set default units.");
console.log(
" weather|w [options] Fetch and display the current weather."
);
console.log(" help [command] Display help for command.");
}
program
.name("weather-cli")
.version("1.0.0", "-v, --version", "output the current version")
.description("Fetch and display the current weather.")
.usage("weather --search")
.action(displayAppInfo);
program.command("exit").action(() => {
process.exit();
});
program
.command("set-location")
.alias("sl")
.description(
"Allow users to set a default location for quicker weather checks."
)
.action(() => {
prompt({
type: "input",
name: "location",
message: `Enter default location:`,
}).then((answers) => {
readFile("./config.env", "utf-8")
.then((data) => {
const locationRegex = /^LOCATION=(.*)$/m;
if (!data.match(locationRegex)) {
data += "\nLOCATION=" + answers.location;
} else {
data = data.replace(locationRegex, `LOCATION=${answers.location}`);
}
return writeFile("./config.env", data);
})
.then(() => {
console.log("Setting default location was successful.");
process.exit();
})
.catch((err) => {
console.log("Error while setting default location.", err);
process.exit(1);
});
});
});
program
.command("set-units")
.alias("su")
.description(
"Allow users to set default units (Celsius or Fahrenheit) for displaying."
)
.action(() => {
prompt(listTemperatureUnit).then((answers) => {
readFile("./config.env", "utf-8")
.then((data) => {
const unitRegex = /^UNIT=(.*)$/m;
if (!data.match(unitRegex)) {
data += "\nUNIT=" + answers.unit;
} else {
data = data.replace(unitRegex, `UNIT=${answers.unit}`);
}
return writeFile("./config.env", data);
})
.then(() => {
console.log("Setting default unit was successful.");
process.exit();
})
.catch((err) => {
console.log("Error while setting default unit.", err);
process.exit(1);
});
});
});
program
.command("weather")
.alias("w")
.option(
"-s, --search",
"Fetch and display the current weather based on the user's input (city or zip code)."
)
.description("Fetch and display the current weather.")
.action((option) => {
if (option.s || option.search) {
checkWeather();
} else if (process.env.LOCATION) {
console.log("Getting data from api...");
fetchWeather({
data: process.env.LOCATION,
locationoption: "City Name",
});
} else {
console.log("You do not have default location specified.");
}
});
program.parse(process.argv);