@v1sh999/npm-submission-project-m3 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -5
- package/index.js +129 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,11 +1,96 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Everyday CLI Toolbox
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A lightweight command-line utility package built with **Node.js** and **Commander.js**. It provides a collection of simple everyday utilities through a single CLI interface.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Install the package globally using npm:
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
```bash
|
|
10
|
+
npm install -g @v1sh999/npm-submission-project-m3
|
|
11
|
+
```
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
Once installed, the `npm-toolbox` command will be available in your terminal.
|
|
14
|
+
|
|
15
|
+
## Available Commands
|
|
16
|
+
|
|
17
|
+
### 1. Greeting
|
|
18
|
+
|
|
19
|
+
Display a greeting for a given name.
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm-toolbox greet World
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 2. Calculator
|
|
26
|
+
|
|
27
|
+
Perform arithmetic operations on three numbers.
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm-toolbox calc 10 5 2 --add
|
|
31
|
+
npm-toolbox calc 10 5 2 --sub
|
|
32
|
+
npm-toolbox calc 10 5 2 --mul
|
|
33
|
+
npm-toolbox calc 10 5 2 --div
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Available options:
|
|
37
|
+
|
|
38
|
+
| Option | Description |
|
|
39
|
+
|---|---|
|
|
40
|
+
| `-a, --add` | Add the numbers |
|
|
41
|
+
| `-s, --sub` | Subtract the numbers |
|
|
42
|
+
| `-m, --mul` | Multiply the numbers |
|
|
43
|
+
| `-d, --div` | Divide the numbers |
|
|
44
|
+
|
|
45
|
+
Exactly one operation must be selected.
|
|
46
|
+
|
|
47
|
+
### 3. Random Number Generator
|
|
48
|
+
|
|
49
|
+
Generate a random whole number within a specified range (inclusive).
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm-toolbox random 1 100
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 4. Date and Time
|
|
56
|
+
|
|
57
|
+
Display the current date and time.
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm-toolbox date
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 5. Programming Joke
|
|
64
|
+
|
|
65
|
+
Fetch a random programming joke using the JokeAPI.
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npm-toolbox joke
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
An active internet connection is required for this command.
|
|
72
|
+
|
|
73
|
+
## Additional Options
|
|
74
|
+
|
|
75
|
+
Commander.js also provides built-in help and version options.
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
npm-toolbox --help
|
|
79
|
+
npm-toolbox --version
|
|
80
|
+
npm-toolbox calc --help
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Technologies Used
|
|
84
|
+
|
|
85
|
+
- **Node.js** – JavaScript runtime.
|
|
86
|
+
- **Commander.js** – Command-line argument and option handling.
|
|
87
|
+
- **JavaScript Fetch API** – Making HTTP requests to an external API.
|
|
88
|
+
|
|
89
|
+
## Project Links
|
|
90
|
+
|
|
91
|
+
- **npm Package:** https://www.npmjs.com/package/@v1sh999/npm-submission-project-m3
|
|
92
|
+
- **GitHub Repository:** https://github.com/v1sh999/npm-submission-project
|
|
93
|
+
|
|
94
|
+
## Purpose
|
|
95
|
+
|
|
96
|
+
This project demonstrates the process of creating, configuring, publishing, and using an npm package. It also explores command-line application development using Commander.js and fundamental JavaScript concepts.
|
package/index.js
CHANGED
|
@@ -8,4 +8,132 @@ program
|
|
|
8
8
|
console.log(`Hello ${name}`);
|
|
9
9
|
});
|
|
10
10
|
|
|
11
|
-
program
|
|
11
|
+
program
|
|
12
|
+
.command("calc <n1> <n2> <n3>")
|
|
13
|
+
.option("-a, --add", "Add the numbers")
|
|
14
|
+
.option("-s, --sub", "Subtract the numbers")
|
|
15
|
+
.option("-m, --mul", "Multiply the numbers")
|
|
16
|
+
.option("-d, --div", "Divide the numbers")
|
|
17
|
+
.action((n1, n2, n3, options) => {
|
|
18
|
+
const numbers = [n1, n2, n3].map(Number);
|
|
19
|
+
|
|
20
|
+
if (numbers.some((num) => !Number.isFinite(num))) {
|
|
21
|
+
console.error("Error: Please provide valid numbers.");
|
|
22
|
+
process.exitCode = 1;
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const operations = [
|
|
27
|
+
options.add,
|
|
28
|
+
options.sub,
|
|
29
|
+
options.mul,
|
|
30
|
+
options.div,
|
|
31
|
+
].filter(Boolean);
|
|
32
|
+
|
|
33
|
+
if (operations.length !== 1) {
|
|
34
|
+
console.error("Error: Please specify exactly one operation.");
|
|
35
|
+
process.exitCode = 1;
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let result;
|
|
40
|
+
|
|
41
|
+
if (options.add) {
|
|
42
|
+
result = numbers.reduce((sum, num) => sum + num, 0);
|
|
43
|
+
} else if (options.sub) {
|
|
44
|
+
result = numbers.reduce((total, num) => total - num);
|
|
45
|
+
} else if (options.mul) {
|
|
46
|
+
result = numbers.reduce((total, num) => total * num, 1);
|
|
47
|
+
} else if (options.div) {
|
|
48
|
+
if (numbers.slice(1).some((num) => num === 0)) {
|
|
49
|
+
console.error("Error: Division by zero is not allowed.");
|
|
50
|
+
process.exitCode = 1;
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
result = numbers.reduce((total, num) => total / num);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
console.log(`Result: ${result}`);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
program
|
|
61
|
+
.command("random <min> <max>")
|
|
62
|
+
.description("Generate a random whole number within a range")
|
|
63
|
+
.action((min, max) => {
|
|
64
|
+
min = Number(min);
|
|
65
|
+
max = Number(max);
|
|
66
|
+
|
|
67
|
+
if (!Number.isInteger(min) || !Number.isInteger(max)) {
|
|
68
|
+
console.error("Error: Please provide valid whole numbers.");
|
|
69
|
+
process.exitCode = 1;
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (min > max) {
|
|
74
|
+
console.error("Error: Minimum cannot be greater than maximum.");
|
|
75
|
+
process.exitCode = 1;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const result = Math.floor(Math.random() * (max - min + 1)) + min;
|
|
80
|
+
|
|
81
|
+
console.log(`Random number: ${result}`);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
program
|
|
85
|
+
.command("date")
|
|
86
|
+
.description("Display the current date and time")
|
|
87
|
+
.action(() => {
|
|
88
|
+
const now = new Date();
|
|
89
|
+
|
|
90
|
+
const currentDate = now.toLocaleDateString("en-IN", {
|
|
91
|
+
weekday: "long",
|
|
92
|
+
year: "numeric",
|
|
93
|
+
month: "long",
|
|
94
|
+
day: "numeric",
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const currentTime = now.toLocaleTimeString("en-IN", {
|
|
98
|
+
hour: "2-digit",
|
|
99
|
+
minute: "2-digit",
|
|
100
|
+
second: "2-digit",
|
|
101
|
+
hour12: true,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
console.log(`Current Date: ${currentDate}`);
|
|
105
|
+
console.log(`Current Time: ${currentTime}`);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
program
|
|
109
|
+
.command("joke")
|
|
110
|
+
.description("Fetch a random programming joke")
|
|
111
|
+
.action(async () => {
|
|
112
|
+
try {
|
|
113
|
+
const response = await fetch(
|
|
114
|
+
"https://v2.jokeapi.dev/joke/Programming?safe-mode"
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
if (!response.ok) {
|
|
118
|
+
throw new Error(`API request failed: ${response.status}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const data = await response.json();
|
|
122
|
+
|
|
123
|
+
if (data.error) {
|
|
124
|
+
throw new Error("The API returned an error.");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (data.type === "single") {
|
|
128
|
+
console.log(data.joke);
|
|
129
|
+
} else {
|
|
130
|
+
console.log(data.setup);
|
|
131
|
+
console.log(data.delivery);
|
|
132
|
+
}
|
|
133
|
+
} catch (error) {
|
|
134
|
+
console.error("Error fetching joke:", error.message);
|
|
135
|
+
process.exitCode = 1;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
program.parse(process.argv);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@v1sh999/npm-submission-project-m3",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A simple CLI utility package built with Node.js and Commander",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -20,5 +20,8 @@
|
|
|
20
20
|
"homepage": "https://github.com/v1sh999/npm-submission-project#readme",
|
|
21
21
|
"bin": {
|
|
22
22
|
"npm-toolbox": "./index.js"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=18.0.0"
|
|
23
26
|
}
|
|
24
27
|
}
|