-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·169 lines (152 loc) · 4.35 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
#!/usr/bin/env node
const packageJson = require("./package.json")
const fs = require("fs");
const readline = require("readline");
//Function to write file
function creatingFile(filePath){
if(!fs.existsSync(filePath)){
try{
fs.writeFileSync(filePath, ' ');
}catch(error){
console.log(error);
}
}else{
console.log('file already exists');
}
}
// Function to count the number of bytes in a file
function countBytes(filePath) {
try {
const stats = fs.statSync(filePath);
if (stats.isFile()) {
const byteCount = stats.size;
console.log(`${byteCount} ${filePath}`);
return byteCount;
} else {
console.error(`${filePath} is a directory`);
process.exit(1);
}
} catch (error) {
console.error(`No such file or directory`);
process.exit(1);
}
}
// Function to count the number of lines in a file
function countLines(filePath) {
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: fs.createReadStream(filePath),
output: process.stdout,
terminal: false,
});
let lineCount = 0;
rl.on("line", (line) => {
if(line.trim()!== ''){
lineCount++;
}
});
rl.on("close", () => {
console.log(`${lineCount} ${filePath}`); // Log the line count
resolve(lineCount); // Resolve the promise with the line count
});
rl.on("error", (error) => {
console.error(`No such file or directory`);
reject(error); // Reject the promise in case of an error
});
});
}
//Function to count the words in a file
function countWords(filePath) {
try {
const data = fs.readFileSync(filePath, "utf-8");
const words = data.split(/\s+/).filter((word) => word.trim() !== "");
words.forEach((word, index) => {
// console.log(`${index + 1} ${word}`);
});
console.log(`${words.length} ${filePath}`);
return words.length;
} catch (error) {
console.error(`Error reading ${filePath}: ${error}`);
process.exit(1);
}
}
// Function to count ch in a file
function countCharacters(filePath) {
try {
const content = fs.readFileSync(filePath, 'utf-8');
const characterCount = content.length;
console.log(`${characterCount} characters in ${filePath}`);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1); // Exit with an error code
}
}
// Funtion to count bytes,lines and words with no args
async function countBytesLinesWords(filePath) {
const byteCount = countBytes(filePath);
const wordCount = countWords(filePath);
const lineCount = await countLines(filePath);
console.log(`${byteCount} ${wordCount} ${lineCount} ${filePath}`);
}
// Extracting command-line arguments, excluding the first two elements (node and script name)
const [, , ...args] = process.argv;
// switch case to handle different command-line options
switch (args[0]) {
case "-c":
if (args.length !== 2 || args[0] !== "-c") {
console.error("Usage: ccwc -c <file>");
process.exit(1);
}
countBytes(args[1]);
break;
case "-l":
if (args.length !== 2 || args[0] !== "-l") {
console.error("Usage: ccwc -l <file>");
process.exit(1);
}
countLines(args[1]);
break;
case "-w":
if (args.length != 2 || args[0] !== "-w") {
console.error("Usage: ccwc -w <file>");
process.exit(1);
}
countWords(args[1]);
break;
case "-m":
if (args.length != 2 || args[0] !== "-m") {
console.error("Usage: ccwc -m <file>");
process.exit(1);
}
countCharacters(args[1]);
break;
case "-t":
if(args.length !== 2 || args[0] !== "-t"){
console.error("Usage: ccwc -t <file>");
process.exit(1);
}
creatingFile(args[1]);
break;
case "-version":
if(args[0] === "-version"){
console.log(`ccwc version:\n ${(packageJson.version)}`)
process.exit(0);
}
break;
case "-help":
console.log(`Usage: ccwc [options] <file>
Options:
-c, --bytes Print the byte counts
-l, --lines Print the newline counts
-w --words Print the word counts
-m, --chars Print the character counts
-t, --file Creating an empty file
`);
break;
default:
if(args.length !== 1){
console.error("Usage: ccwc -help");
process.exit(1);
}
countBytesLinesWords(args[0]);
}