|
| 1 | +# import { program } from "commander"; |
| 2 | +# import { promises as fs } from "node:fs"; |
| 3 | +# import process from "node:process"; |
| 4 | +# import { stat } from "node:fs/promises"; |
| 5 | + |
| 6 | +# program |
| 7 | +# .name("count-containing-lines-words-characters") |
| 8 | +# .description("Counts lines, words or characters in a file (or all files) inside a directory") |
| 9 | +# .option("-l, --line", "The number of lines in each file") |
| 10 | +# .option("-w, --word", "The number of words in each file") |
| 11 | +# .option("-c, --character", "The number of characters in each file") |
| 12 | +# .argument("<path...>", "The file path to process"); |
| 13 | + |
| 14 | +# program.parse(); |
| 15 | + |
| 16 | +# const argv = program.args; |
| 17 | + |
| 18 | +# const options = program.opts(); |
| 19 | + |
| 20 | + |
| 21 | +# function counter(item) { |
| 22 | +# const lines = item.trim().split("\n").length; |
| 23 | +# const words = item.split(/\s+/).filter(Boolean).length; |
| 24 | +# const characters = item.length; |
| 25 | +# return { lines, words, characters }; |
| 26 | +# } |
| 27 | + |
| 28 | +# let totalLines = 0; |
| 29 | +# let totalWords = 0; |
| 30 | +# let totalCharacters = 0; |
| 31 | +# let fileCount = 0; |
| 32 | + |
| 33 | +# for (const path of argv) { |
| 34 | +# const pathInfo = await stat(path); |
| 35 | + |
| 36 | +# if (pathInfo.isFile()) { |
| 37 | +# const content = await fs.readFile(path, "utf-8"); |
| 38 | +# const stats = counter(content); |
| 39 | +# if (options.line) { |
| 40 | +# console.log(`${stats.lines} ${path}`); |
| 41 | +# } else if (options.word) { |
| 42 | +# console.log(`${stats.words} ${path}`); |
| 43 | +# } else if (options.character) { |
| 44 | +# console.log(`${stats.characters} ${path}`); |
| 45 | +# } else { |
| 46 | +# console.log(`${stats.lines} ${stats.words} ${stats.characters} ${path}`); |
| 47 | +# } |
| 48 | + |
| 49 | +# totalLines += stats.lines; |
| 50 | +# totalWords += stats.words; |
| 51 | +# totalCharacters += stats.characters; |
| 52 | +# fileCount++; |
| 53 | + |
| 54 | +# } else if (pathInfo.isDirectory()) { |
| 55 | +# const files = await fs.readdir(path); |
| 56 | +# for (const file of files) { |
| 57 | +# const filePath = `${path}/${file}`; |
| 58 | +# const fileContent = await fs.readFile(filePath, "utf-8"); |
| 59 | +# const stats = counter(fileContent); |
| 60 | + |
| 61 | +# if (options.line) { |
| 62 | +# console.log(`${stats.lines} ${filePath}`); |
| 63 | +# } else if (options.word) { |
| 64 | +# console.log(`${stats.words} ${filePath}`); |
| 65 | +# } else if (options.character) { |
| 66 | +# console.log(`${stats.characters} ${filePath}`); |
| 67 | +# } else { |
| 68 | +# console.log(`${stats.lines} ${stats.words} ${stats.characters} ${filePath}`); |
| 69 | +# } |
| 70 | + |
| 71 | +# totalLines += stats.lines; |
| 72 | +# totalWords += stats.words; |
| 73 | +# totalCharacters += stats.characters; |
| 74 | +# fileCount++; |
| 75 | +# } |
| 76 | +# } |
| 77 | + |
| 78 | +# } |
| 79 | + |
| 80 | +# if (fileCount > 1) { |
| 81 | +# if (options.line) { |
| 82 | +# console.log(`${totalLines} total`); |
| 83 | +# } else if (options.word) { |
| 84 | +# console.log(`${totalWords} total`); |
| 85 | +# } else if (options.character) { |
| 86 | +# console.log(`${totalCharacters} total`); |
| 87 | +# } else { |
| 88 | +# console.log(`${totalLines} ${totalWords} ${totalCharacters} total`); |
| 89 | +# } |
| 90 | +# } |
0 commit comments