|
1 | 1 | import { program } from "commander"; |
2 | 2 | import { promises as fs } from "node:fs"; |
3 | | -import path from "node:path"; |
4 | 3 | import { stat } from "node:fs/promises"; |
5 | 4 |
|
6 | 5 | program |
7 | 6 | .name("my-wc") |
8 | | - .description("simplified implementation of wc") |
| 7 | + .description("Simplified implementation of wc") |
9 | 8 | .argument("[paths...]", "One or more file or directory paths") |
10 | | - .option("-l, --line", "count lines") |
11 | | - .option("-w, --word", "count words") |
12 | | - .option("-c, --character", "count characters"); |
| 9 | + .option("-l, --line", "Count lines") |
| 10 | + .option("-w, --word", "Count words") |
| 11 | + .option("-c, --character", "Count characters"); |
13 | 12 |
|
14 | 13 | program.parse(); |
15 | 14 |
|
16 | 15 | const filePaths = program.args.length > 0 ? program.args : ['.']; |
17 | 16 | const options = program.opts(); |
18 | 17 |
|
19 | 18 | const countContent = (content) => { |
20 | | - const lines = content.split('\n').length; |
21 | | - const words = content.trim().split(/\s+/).filter(Boolean).length; |
22 | | - const characters = content.length; |
23 | | - return { lines, words, characters }; |
| 19 | + const lines = content.split('\n').length; |
| 20 | + const words = content.trim().split(/\s+/).filter(Boolean).length; |
| 21 | + const characters = content.length; |
| 22 | + return { lines, words, characters }; |
24 | 23 | }; |
25 | 24 |
|
26 | 25 | const total = { |
27 | | - lines: 0, |
28 | | - words: 0, |
29 | | - characters: 0 |
| 26 | + lines: 0, |
| 27 | + words: 0, |
| 28 | + characters: 0 |
30 | 29 | }; |
31 | 30 |
|
32 | | -(async () => { |
33 | | - let fileCount = 0; |
| 31 | +//Output formatting function |
| 32 | +function formatOutput(counts, label, options) { |
| 33 | + const { lines, words, characters } = counts; |
| 34 | + const showAll = !options.line && !options.word && !options.character; |
34 | 35 |
|
35 | | - for (const inputPath of filePaths) { |
36 | | - try { |
37 | | - const stats = await stat(inputPath); |
38 | | - if (stats.isDirectory()) { |
39 | | - console.log(`${inputPath} is a directory. Skipping.`); |
40 | | - continue; |
41 | | - } |
| 36 | + const parts = []; |
42 | 37 |
|
43 | | - const content = await fs.readFile(inputPath, "utf-8"); |
44 | | - const { lines, words, characters } = countContent(content); |
| 38 | + if (options.line || showAll) parts.push(lines.toString().padStart(8)); |
| 39 | + if (options.word || showAll) parts.push(words.toString().padStart(8)); |
| 40 | + if (options.character || showAll) parts.push(characters.toString().padStart(8)); |
45 | 41 |
|
46 | | - total.lines += lines; |
47 | | - total.words += words; |
48 | | - total.characters += characters; |
49 | | - fileCount++; |
| 42 | + parts.push(label); |
| 43 | + return parts.join(" "); |
| 44 | +} |
50 | 45 |
|
51 | | - let output = ""; |
52 | | - if (options.line) output += `${lines.toString().padStart(8)} `; |
53 | | - if (options.word) output += `${words.toString().padStart(8)} `; |
54 | | - if (options.character) output += `${characters.toString().padStart(8)} `; |
| 46 | +(async () => { |
| 47 | + let fileCount = 0; |
55 | 48 |
|
56 | | - // If no options given, show all |
57 | | - if (!options.line && !options.word && !options.character) { |
58 | | - output += `${lines.toString().padStart(8)} `; |
59 | | - output += `${words.toString().padStart(8)} `; |
60 | | - output += `${characters.toString().padStart(8)} `; |
61 | | - } |
| 49 | + for (const inputPath of filePaths) { |
| 50 | + try { |
| 51 | + const stats = await stat(inputPath); |
| 52 | + if (stats.isDirectory()) { |
| 53 | + console.log(`${inputPath} is a directory. Skipping.`); |
| 54 | + continue; |
| 55 | + } |
62 | 56 |
|
63 | | - output += inputPath; |
64 | | - console.log(output); |
65 | | - } catch (err) { |
66 | | - console.error(`Error reading "${inputPath}": ${err.message}`); |
67 | | - } |
68 | | - } |
| 57 | + const content = await fs.readFile(inputPath, "utf-8"); |
| 58 | + const counts = countContent(content); |
69 | 59 |
|
70 | | - // Print total only if multiple files were processed |
71 | | - if (fileCount > 1) { |
72 | | - let output = ""; |
73 | | - if (options.line) output += `${total.lines.toString().padStart(8)} `; |
74 | | - if (options.word) output += `${total.words.toString().padStart(8)} `; |
75 | | - if (options.character) output += `${total.characters.toString().padStart(8)} `; |
| 60 | + total.lines += counts.lines; |
| 61 | + total.words += counts.words; |
| 62 | + total.characters += counts.characters; |
| 63 | + fileCount++; |
76 | 64 |
|
77 | | - if (!options.line && !options.word && !options.character) { |
78 | | - output += `${total.lines.toString().padStart(8)} `; |
79 | | - output += `${total.words.toString().padStart(8)} `; |
80 | | - output += `${total.characters.toString().padStart(8)} `; |
81 | | - } |
82 | | - |
83 | | - output += "total"; |
84 | | - console.log(output); |
| 65 | + console.log(formatOutput(counts, inputPath, options)); |
| 66 | + } catch (err) { |
| 67 | + console.error(`Error reading "${inputPath}": ${err.message}`); |
85 | 68 | } |
| 69 | + } |
| 70 | + |
| 71 | + if (fileCount > 1) { |
| 72 | + console.log(formatOutput(total, "total", options)); |
| 73 | + } |
86 | 74 | })(); |
0 commit comments