|
1 | 1 | import { promises as fs } from "node:fs"; |
2 | 2 |
|
3 | 3 | const args = process.argv.slice(2); |
4 | | -console.log(args) |
5 | 4 |
|
6 | 5 | const showLines = args.includes("-l"); |
7 | 6 | const showWords = args.includes("-w"); |
8 | 7 | const showChars = args.includes("-c"); |
9 | | -const paths = args.filter(arg => !arg.startsWith("-")) || "."; |
10 | | -console.log(paths) |
| 8 | +let paths = args.filter(arg => !arg.startsWith("-")); |
| 9 | +if (paths.length === 0) paths = ["."]; |
11 | 10 |
|
12 | | -// const direct = await fs.readdir(path); |
| 11 | +// helper for formatting like real wc |
| 12 | +const pad = (n) => String(n).padStart(8, " "); |
13 | 13 |
|
14 | | -// console.log(direct); |
15 | 14 | let totalLines = 0; |
16 | 15 | let totalWords = 0; |
17 | | -let totalchars = 0; |
| 16 | +let totalChars = 0; |
18 | 17 |
|
19 | | -if(showLines){ |
20 | | - for (const path of paths){ |
21 | | - const content = await fs.readFile(path, "utf-8"); |
22 | | - const lines = content.split("\n").length; |
| 18 | +for (const path of paths) { |
| 19 | + try { |
| 20 | + const content = await fs.readFile(path, "utf-8"); |
23 | 21 |
|
24 | | - totalLines += lines; |
| 22 | + const lines = content.split("\n").length; |
| 23 | + const words = content.split(/\s+/).filter(Boolean).length; |
| 24 | + const chars = content.length; |
| 25 | + |
| 26 | + totalLines += lines; |
| 27 | + totalWords += words; |
| 28 | + totalChars += chars; |
| 29 | + |
| 30 | + } catch (err) { |
| 31 | + console.error(`Error reading file "${path}": ${err.message}`); |
25 | 32 | } |
26 | | - console.log("line: ", totalLines); |
| 33 | + |
| 34 | +} |
| 35 | +if (showLines) { |
| 36 | + console.log("lines:", totalLines); |
27 | 37 | } |
28 | | -else if(showWords){ |
29 | | - for (const path of paths){ |
30 | | - const content = await fs.readFile(path, "utf-8"); |
31 | | - const words = content.split(/\s+/).filter(Boolean).length; |
32 | 38 |
|
33 | | - totalWords += words; |
34 | | - } |
35 | | - console.log("words: ", totalWords); |
| 39 | +if (showWords) { |
| 40 | + console.log("words:", totalWords); |
36 | 41 | } |
37 | | -else if(showChars){ |
38 | | - for (const path of paths){ |
39 | | - const content = await fs.readFile(path, "utf-8"); |
40 | | - const char = content.length; |
41 | 42 |
|
42 | | - totalchars += char; |
43 | | - } |
44 | | - console.log("chars", totalchars) |
| 43 | +if (showChars) { |
| 44 | + console.log("chars:", totalChars); |
45 | 45 | } |
46 | | -else{ |
47 | | - for (const path of paths){ |
48 | | - const content = await fs.readFile(path, "utf-8"); |
49 | | - const lines = content.split("\n").length; |
50 | | - const words = content.split(/\s+/).filter(Boolean).length; |
51 | | - const char = content.length; |
52 | | - |
53 | | - totalLines += lines; |
54 | | - totalWords += words; |
55 | | - totalchars += char; |
56 | | - } |
57 | | -console.log("lines: ", totalLines, " words", totalWords, " char:", totalchars) |
| 46 | + |
| 47 | + |
| 48 | +// default output when no flags |
| 49 | +if (!showLines && !showWords && !showChars) { |
| 50 | + console.log( |
| 51 | + `${pad(totalLines)}\t${pad(totalWords)}\t${pad(totalChars)}\ttotal` |
| 52 | + ); |
58 | 53 | } |
59 | 54 |
|
60 | 55 |
|
0 commit comments