my-cli-piyush 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/bin/index.js +65 -0
- package/package.json +15 -0
package/bin/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs/promises";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
function wordCounter(filePath, targetWord) {
|
|
7
|
+
|
|
8
|
+
}
|
|
9
|
+
// 1. Destructure arguments cleanly. Using clear variable names helps readability.
|
|
10
|
+
let [, , filePath, targetWord] = process.argv;
|
|
11
|
+
|
|
12
|
+
if (!filePath) {
|
|
13
|
+
console.error(
|
|
14
|
+
"Error: Please provide a file path. Usage: node script.js <filename> [word] <optional>"
|
|
15
|
+
);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 2. Normalize file extension safely (appends .txt only if it's missing)
|
|
20
|
+
if (!filePath.endsWith(".txt")) {
|
|
21
|
+
filePath += ".txt";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
// 3. Read the file
|
|
26
|
+
const content = await fs.readFile(filePath, "utf-8");
|
|
27
|
+
|
|
28
|
+
// 4. Extract words using a better regex
|
|
29
|
+
// [a-zA-Z0-9']+ captures standard words and words containing apostrophes (like "don't")
|
|
30
|
+
const wordsArray = content.match(/[a-zA-Z0-9']+/g) || [];
|
|
31
|
+
|
|
32
|
+
const wordCounter = {};
|
|
33
|
+
|
|
34
|
+
// Normalize target word once outside the loop if it exists
|
|
35
|
+
const searchWord = targetWord?.toLowerCase();
|
|
36
|
+
|
|
37
|
+
// 5. Count words efficiently
|
|
38
|
+
for (const word of wordsArray) {
|
|
39
|
+
const normalizedWord = word.toLowerCase();
|
|
40
|
+
|
|
41
|
+
if (searchWord) {
|
|
42
|
+
// If a specific word is requested, only increment that specific key
|
|
43
|
+
if (normalizedWord === searchWord) {
|
|
44
|
+
wordCounter[searchWord] = (wordCounter[searchWord] || 0) + 1;
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
// Otherwise, map and count every single word
|
|
48
|
+
wordCounter[normalizedWord] = (wordCounter[normalizedWord] || 0) + 1;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 6. Provide a user-friendly output if no matches were found
|
|
53
|
+
if (searchWord && !wordCounter[searchWord]) {
|
|
54
|
+
console.log(`The word "${targetWord}" was not found in ${filePath}.`);
|
|
55
|
+
} else {
|
|
56
|
+
console.log(wordCounter);
|
|
57
|
+
}
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (error.code === "ENOENT") {
|
|
60
|
+
console.error(`Error: The file "${filePath}" could not be found.`);
|
|
61
|
+
} else {
|
|
62
|
+
console.error(`An error occurred: ${error.message}`);
|
|
63
|
+
}
|
|
64
|
+
process.exit(1);
|
|
65
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "my-cli-piyush",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "My first npx package",
|
|
5
|
+
"bin": {
|
|
6
|
+
"my-cli-app": "./bin/index.js"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"cli",
|
|
11
|
+
"npx"
|
|
12
|
+
],
|
|
13
|
+
"author": "Your Name",
|
|
14
|
+
"license": "MIT"
|
|
15
|
+
}
|