generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwc.js
131 lines (121 loc) · 3.4 KB
/
wc.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
import { program } from "commander";
import * as fs from "node:fs";
import process from "node:process";
import path from "node:path";
program
.name("wc")
.description(`word, line, character, and byte count`)
.option(
"-l, --lines",
"The number of lines in each input file is written to the standard output."
)
.option(
"-w, --words",
"The number of words in each input file is written to the standard output."
)
.option(
"-c, --bytes",
"The number of bytes in each input file is written to the standard output."
)
.argument("<paths...>", "The file paths to process");
program.parse();
const options = program.opts();
const arg = program.args;
let currentDirName = process.cwd();
let totalLines = 0;
let totalWords = 0;
let totalBytes = 0;
function countLines(content) {
let counter = 0;
for (const line of content.split("\n")) {
counter += 1;
}
return counter - 1;
}
function countWords(content) {
let counter = 0;
for (const word of content.split(" ")) {
for (const w of word.split("\n")) {
if (w) {
counter += 1;
}
}
}
return counter;
}
function countBytes(content) {
let size = new Blob([content]).size;
return size;
}
async function fileContentInfo(filePath, file) {
try {
let content = await fs.promises.readFile(filePath, "utf-8");
let output = "";
if (Object.keys(options).length == 0) {
let numLines = countLines(content);
let numWords = countWords(content);
let numBytes = countBytes(content);
output += numLines + " ";
output += numWords + " ";
output += numBytes + " ";
totalLines += numLines;
totalWords += numWords;
totalBytes += numBytes;
}
if (options.lines) {
let numLines = countLines(content);
output += numLines + " ";
totalLines += numLines;
}
if (options.words) {
let numWords = countWords(content);
output += numWords + " ";
totalWords += numWords;
}
if (options.bytes) {
let numBytes = countBytes(content);
output += numBytes + " ";
totalBytes += numBytes;
}
output += file;
console.log(output);
} catch (err) {
console.log(err);
}
}
async function main() {
const promises = arg.map((argument) => {
const filePath = path.join(currentDirName, argument);
return new Promise((resolve, reject) => {
fs.stat(filePath, (err, stats) => {
if (err) {
if (err.code === "ENOENT") {
console.error(`wc: ${argument}: read: No such file or directory`);
} else {
console.error(`wc: ${argument}: read: Error: ${err.message}`);
}
resolve();
} else if (stats.isFile()) {
fileContentInfo(filePath, argument).then(resolve);
} else if (stats.isDirectory()) {
console.log(`wc: ${argument}: read: Is a directory`);
resolve();
} else {
console.error(`wc: ${argument}: read: Unknown file type`);
resolve();
}
});
});
});
await Promise.all(promises);
if (Object.keys(options).length == 0) {
console.log(totalLines, totalWords, totalBytes, "total");
} else {
let totalOutput = "";
options.lines ? (totalOutput += totalLines + " ") : null;
options.words ? (totalOutput += totalWords + " ") : null;
options.bytes ? (totalOutput += totalBytes + " ") : null;
console.log(totalOutput + "total");
}
}
main();