nodejs-cli-toolkit 0.0.0-stage ā 1.0.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/app.js +76 -0
- package/commands/apiUtils.js +20 -0
- package/commands/fileUtils.js +63 -0
- package/commands/stringUtils.js +20 -0
- package/files/file1.txt +1 -0
- package/files/file1.txt.gz +0 -0
- package/package.json +16 -4
- package/README.md +0 -3
package/app.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
require('dotenv').config()
|
|
4
|
+
|
|
5
|
+
const { uppercase, wordCount, isPalindrome } = require('./commands/StringUtils.js');
|
|
6
|
+
|
|
7
|
+
const { compressFile, decompressFile } = require('./commands/FileUtils.js')
|
|
8
|
+
|
|
9
|
+
const getJoke = require('./commands/apiUtils.js')
|
|
10
|
+
|
|
11
|
+
const [operation, ...values] = process.argv.slice(2)
|
|
12
|
+
|
|
13
|
+
const text = values.join(" ")
|
|
14
|
+
|
|
15
|
+
switch (operation) {
|
|
16
|
+
case "uppercase":
|
|
17
|
+
console.log(uppercase(text));
|
|
18
|
+
break;
|
|
19
|
+
case "wordCount":
|
|
20
|
+
console.log("Word count : ", wordCount(text));
|
|
21
|
+
break;
|
|
22
|
+
case "isPalindrome":
|
|
23
|
+
console.log("Is palindrome : ", isPalindrome(text));
|
|
24
|
+
break;
|
|
25
|
+
case "compress":
|
|
26
|
+
compressFile(text)
|
|
27
|
+
.then(outputFile => {
|
|
28
|
+
console.log("File compressed : ", outputFile);
|
|
29
|
+
}).catch(error => {
|
|
30
|
+
console.log("Compression failed : ",error.message);
|
|
31
|
+
})
|
|
32
|
+
break;
|
|
33
|
+
case "decompress":
|
|
34
|
+
decompressFile(text)
|
|
35
|
+
.then(outputFile => {
|
|
36
|
+
console.log("File compressed : ", outputFile);
|
|
37
|
+
}).catch(error => {
|
|
38
|
+
console.log("Compression failed : ",error.message);
|
|
39
|
+
})
|
|
40
|
+
break;
|
|
41
|
+
case "joke":
|
|
42
|
+
getJoke()
|
|
43
|
+
break;
|
|
44
|
+
case "help":
|
|
45
|
+
console.log(`
|
|
46
|
+
${process.env.APP_NAME || "Node CLI Utility"}
|
|
47
|
+
|
|
48
|
+
Available Commands:
|
|
49
|
+
|
|
50
|
+
String Manipulation:
|
|
51
|
+
uppercase <text> Convert text to uppercase
|
|
52
|
+
wordcount <text> Count words
|
|
53
|
+
palindrome <text> Check whether text is a palindrome
|
|
54
|
+
|
|
55
|
+
File Operations:
|
|
56
|
+
compress <file> Compress a file
|
|
57
|
+
decompress <file.gz> Decompress a file
|
|
58
|
+
|
|
59
|
+
API:
|
|
60
|
+
joke Fetch a random joke
|
|
61
|
+
|
|
62
|
+
General:
|
|
63
|
+
help Display available commands
|
|
64
|
+
`);
|
|
65
|
+
break;
|
|
66
|
+
default:
|
|
67
|
+
console.log("Available commands:");
|
|
68
|
+
console.log(' uppercase "your text"');
|
|
69
|
+
console.log(' wordCount "your text"');
|
|
70
|
+
console.log(' isPalindrome "your text"');
|
|
71
|
+
console.log(' compress "filename"');
|
|
72
|
+
console.log(' decompress "filename.gz"');
|
|
73
|
+
console.log(' joke');
|
|
74
|
+
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
async function getJoke(){
|
|
2
|
+
try{
|
|
3
|
+
const response = await fetch("https://official-joke-api.appspot.com/random_joke")
|
|
4
|
+
|
|
5
|
+
if (!response.ok) {
|
|
6
|
+
throw new Error(`HTTP Error : ${response.status}`)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const joke = await response.json()
|
|
10
|
+
|
|
11
|
+
console.log("\nš Random Joke");
|
|
12
|
+
console.log(joke.setup);
|
|
13
|
+
console.log(joke.punchline);
|
|
14
|
+
|
|
15
|
+
}catch(error){
|
|
16
|
+
console.log("Error fetching joke : ",error);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = getJoke
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const zlib = require('zlib')
|
|
3
|
+
const path =require('path')
|
|
4
|
+
|
|
5
|
+
function validateInputFile(inputFile){
|
|
6
|
+
if (!inputFile) {
|
|
7
|
+
throw new Error("Please provide a file path")
|
|
8
|
+
}
|
|
9
|
+
if (!fs.existsSync(inputFile)) {
|
|
10
|
+
throw new Error(`File not found : ${inputFile}`)
|
|
11
|
+
}
|
|
12
|
+
if (!fs.statSync(inputFile).isFile()) {
|
|
13
|
+
throw new Error("The provided path is not a file")
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function compressFile(inputFile) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
|
|
20
|
+
validateInputFile(inputFile)
|
|
21
|
+
|
|
22
|
+
const outputFile = `${inputFile}.gz`
|
|
23
|
+
const source = fs.createReadStream(inputFile)
|
|
24
|
+
const destination = fs.createWriteStream(outputFile)
|
|
25
|
+
|
|
26
|
+
source
|
|
27
|
+
.pipe(zlib.createGzip())
|
|
28
|
+
.pipe(destination)
|
|
29
|
+
|
|
30
|
+
source.on("error",reject)
|
|
31
|
+
destination.on("error",reject)
|
|
32
|
+
|
|
33
|
+
destination.on("finish", ()=>{
|
|
34
|
+
resolve(outputFile)
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function decompressFile(inputFile) {
|
|
40
|
+
return new Promise((resolve, reject)=>{
|
|
41
|
+
if (!inputFile.endsWith(".gz")) {
|
|
42
|
+
return reject(
|
|
43
|
+
new Error("Input file must have a .gz extension")
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
const outputFile = inputFile.slice(0,-3)
|
|
47
|
+
const source = fs.createReadStream(inputFile)
|
|
48
|
+
const destination = fs.createWriteStream(outputFile)
|
|
49
|
+
|
|
50
|
+
source
|
|
51
|
+
.pipe(zlib.createGunzip())
|
|
52
|
+
.pipe(destination)
|
|
53
|
+
|
|
54
|
+
source.on("error",reject)
|
|
55
|
+
destination.on("error",reject)
|
|
56
|
+
|
|
57
|
+
destination.on("finish",()=>{
|
|
58
|
+
resolve(outputFile)
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = { compressFile, decompressFile}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function uppercase(text){
|
|
2
|
+
return text.toUpperCase()
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function wordCount(text) {
|
|
6
|
+
const trimmedText = text.trim()
|
|
7
|
+
if (!trimmedText) {
|
|
8
|
+
return 0;
|
|
9
|
+
}
|
|
10
|
+
return trimmedText.split(/\s+/).length;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isPalindrome(text) {
|
|
14
|
+
const cleanedText = text.toLowerCase().replace(/[^a-z0-9]/g,"")
|
|
15
|
+
return cleanedText === cleanedText.split("").reverse().join("");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = {
|
|
19
|
+
uppercase, wordCount, isPalindrome
|
|
20
|
+
}
|
package/files/file1.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
File1
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nodejs-cli-toolkit",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "app.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"bin": {
|
|
10
|
+
"nodejs-cli-toolkit":"app.js"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [],
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"dotenv": "^18.0.9"
|
|
17
|
+
}
|
|
18
|
+
}
|
package/README.md
DELETED