11import { program } from "commander" ;
22import { promises as fs } from "node:fs" ;
3+ import pkg from "glob" ;
4+ const { glob } = pkg ;
35
46program
57 . name ( "wc" )
@@ -10,52 +12,50 @@ program
1012 . argument ( "<files...>" , "One or more files to process" ) ;
1113await program . parseAsync ( ) ;
1214
13- const files = program . args ;
15+ const files = program . args . flatMap ( ( path ) => glob . sync ( path ) ) ;
1416const options = program . opts ( ) ;
1517
16- function countContent ( content ) {
17- const lines = ( content . match ( / \n / g) || [ ] ) . length ;
18- const words = content . trim ( ) . split ( / \s + / ) . filter ( Boolean ) . length ;
19- const bytes = Buffer . byteLength ( content , 'utf-8' ) ;
20- return { lines, words, bytes} ;
18+ function countContent ( content ) {
19+ const lines = ( content . match ( / \n / g) || [ ] ) . length ;
20+ const words = content . trim ( ) . split ( / \s + / ) . filter ( Boolean ) . length ;
21+ const bytes = Buffer . byteLength ( content , "utf-8" ) ;
22+ return { lines, words, bytes } ;
23+ }
24+ // Helper to print counts based on options
25+ function printCounts ( { lines, words, bytes } , label ) {
26+ if ( options . lines ) {
27+ console . log ( `${ lines } ${ label } ` ) ;
28+ } else if ( options . words ) {
29+ console . log ( `${ words } ${ label } ` ) ;
30+ } else if ( options . bytes ) {
31+ console . log ( `${ bytes } ${ label } ` ) ;
32+ } else {
33+ console . log ( `${ lines } ${ words } ${ bytes } ${ label } ` ) ;
34+ }
2135}
2236
2337let totalLines = 0 ;
2438let totalWords = 0 ;
2539let totalBytes = 0 ;
2640
27- for ( const file of files ) {
28- try {
29- const content = await fs . readFile ( file , "utf-8" ) ;
30- const { lines, words, bytes } = countContent ( content ) ;
31-
32- if ( options . lines ) {
33- console . log ( `${ lines } ${ file } ` ) ;
34- } else if ( options . words ) {
35- console . log ( `${ words } ${ file } ` ) ;
36- } else if ( options . bytes ) {
37- console . log ( `${ bytes } ${ file } ` ) ;
38- } else {
39- console . log ( `${ lines } ${ words } ${ bytes } ${ file } ` ) ;
40- }
41-
42- totalLines += lines ;
43- totalWords += words ;
44- totalBytes += bytes ;
45-
46- } catch ( err ) {
41+ for ( const file of files ) {
42+ try {
43+ const content = await fs . readFile ( file , "utf-8" ) ;
44+ const counts = countContent ( content ) ;
45+
46+ printCounts ( counts , file ) ;
47+
48+ totalLines += counts . lines ;
49+ totalWords += counts . words ;
50+ totalBytes += counts . bytes ;
51+ } catch ( err ) {
4752 console . error ( `wc: ${ file } : ${ err . message } ` ) ;
4853 }
4954}
5055
5156if ( files . length > 1 ) {
52- if ( options . lines ) {
53- console . log ( `${ totalLines } total` ) //if more then 1 file is
54- } else if ( options . words ) {
55- console . log ( `${ totalWords } total` ) ;
56- } else if ( options . bytes ) {
57- console . log ( `${ totalBytes } total` ) ;
58- } else {
59- console . log ( `${ totalLines } ${ totalWords } ${ totalBytes } total` ) ;
60- }
57+ printCounts (
58+ { lines : totalLines , words : totalWords , bytes : totalBytes } ,
59+ "total"
60+ ) ;
6161}
0 commit comments