Skip to content

Commit 383f3a6

Browse files
committed
wc implementation for mulit paths without any flag
1 parent 248f9de commit 383f3a6

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.mjs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { program } from "commander";
2+
import { error } from "node:console";
3+
import { promises as fs, link } from "node:fs";
4+
5+
program
6+
.name("wc")
7+
.description("wc implementation")
8+
.argument("<paths...>", "the file path to process");
9+
program.parse();
10+
11+
const paths = program.args;
12+
const total = {
13+
linesCounter: 0,
14+
wordsCounter: 0,
15+
characterCounter: 0,
16+
};
17+
try{
18+
for (const path of paths) {
19+
const content = await fs.readFile(path, "utf-8");
20+
21+
const linesCounter = content.split("\n").length - 1;
22+
const wordsCounter = content.trim().split(/\s+/).length;
23+
const characterCounter = content.length;
24+
25+
total.linesCounter += linesCounter;
26+
total.wordsCounter += wordsCounter;
27+
total.characterCounter += characterCounter;
28+
29+
console.log(` ${linesCounter} ${wordsCounter} ${characterCounter} ${path}`);
30+
}
31+
if (paths.length > 1)
32+
console.log(
33+
` ${total.linesCounter} ${total.wordsCounter} ${total.characterCounter} total`,
34+
);
35+
}catch(error){
36+
console.log(error.message);
37+
}

0 commit comments

Comments
 (0)