77. option ( "-l, --line" , "counts number of lines in the file" )
88. option ( "-w, --words" , "counts number of words in the file" )
99. option ( "-c, --bytes" , "counts number of bytes in the file" )
10- . argument ( "<filepath >" )
10+ . argument ( "<filepaths... >" )
1111
1212program . parse ( ) ;
1313
@@ -19,37 +19,54 @@ if(args.length === 0){
1919 program . help ( ) ;
2020}
2121
22- const path = args [ 0 ] ;
23- const content = await fs . readFile ( path , "utf-8" ) ;
22+ const countLines = content => content . split ( / \r ? \n / ) . length ;
23+ const countWords = content => content . trim ( ) . split ( / \s + / ) . filter ( Boolean ) . length ;
24+ const countBytes = content => Buffer . byteLength ( content , "utf-8" ) ;
2425
26+ // store totals for multiple files
27+ let totalLines = 0 ;
28+ let totalWords = 0 ;
29+ let totalBytes = 0 ;
2530
26- // function to count the number of lines in a files
27- function countLines ( content ) {
28- const lines = content . split ( / \r ? \n / ) ;
29- return lines . length ;
30- }
31+ for ( const path of args ) {
32+ try {
33+ const content = await fs . readFile ( path , "utf-8" ) ;
3134
32- //function to count the words
33- function countWords ( content ) {
34- const words = content . trim ( ) . split ( / \s + / ) ;
35- return words . length ;
36- }
35+ const lines = countLines ( content ) ;
36+ const words = countWords ( content ) ;
37+ const bytes = countBytes ( content ) ;
38+
39+ totalLines += lines ;
40+ totalWords += words ;
41+ totalBytes += bytes ;
42+
43+ // collect counts based on options
44+ const output = [ ] ;
45+ if ( opts . line ) output . push ( lines ) ;
46+ if ( opts . words ) output . push ( words ) ;
47+ if ( opts . bytes ) output . push ( bytes ) ;
3748
38- //function to count the bytes
39- function countBytes ( content ) {
40- return Buffer . byteLength ( content , "utf-8" ) ;
49+ // if no options are passed, print all
50+ if ( ! opts . line && ! opts . words && ! opts . bytes ) {
51+ output . push ( lines , words , bytes ) ;
52+ }
53+
54+ console . log ( ...output , path ) ;
55+ } catch ( err ) {
56+ console . error ( `Error reading file "${ path } ": ${ err . message } ` ) ;
57+ }
4158}
4259
43- const lines = countLines ( content ) ;
44- const words = countWords ( content ) ;
45- const bytes = countBytes ( content ) ;
46-
47- if ( opts . line ) {
48- console . log ( lines ) ;
49- } else if ( opts . words ) {
50- console . log ( words , path ) ;
51- } else if ( opts . bytes ) {
52- console . log ( bytes , path ) ;
53- } else {
54- console . log ( lines , words , bytes , path ) ;
60+ // print totals if more than one file
61+ if ( args . length > 1 ) {
62+ const totalOutput = [ ] ;
63+ if ( opts . line ) totalOutput . push ( totalLines ) ;
64+ if ( opts . words ) totalOutput . push ( totalWords ) ;
65+ if ( opts . bytes ) totalOutput . push ( totalBytes ) ;
66+
67+ if ( ! opts . line && ! opts . words && ! opts . bytes ) {
68+ totalOutput . push ( totalLines , totalWords , totalBytes ) ;
69+ }
70+
71+ console . log ( ... totalOutput , "total" ) ;
5572}
0 commit comments