|
| 1 | +import { promises as fs } from "node:fs"; |
| 2 | + |
| 3 | +const args = process.argv.slice(2); |
| 4 | +console.log(args) |
| 5 | + |
| 6 | +const showLines = args.includes("-l"); |
| 7 | +const showWords = args.includes("-w"); |
| 8 | +const showChars = args.includes("-c"); |
| 9 | +const paths = args.filter(arg => !arg.startsWith("-")) || "."; |
| 10 | +console.log(paths) |
| 11 | + |
| 12 | +// const direct = await fs.readdir(path); |
| 13 | + |
| 14 | +// console.log(direct); |
| 15 | +let totalLines = 0; |
| 16 | +let totalWords = 0; |
| 17 | +let totalchars = 0; |
| 18 | + |
| 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; |
| 23 | + |
| 24 | + totalLines += lines; |
| 25 | + } |
| 26 | + console.log("line: ", totalLines); |
| 27 | +} |
| 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 | + |
| 33 | + totalWords += words; |
| 34 | + } |
| 35 | + console.log("words: ", totalWords); |
| 36 | +} |
| 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 | + totalchars += char; |
| 43 | + } |
| 44 | + console.log("chars", totalchars) |
| 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) |
| 58 | +} |
| 59 | + |
| 60 | + |
0 commit comments