@@ -11,11 +11,13 @@ function countFile(file, options) {
1111 const bytes = Buffer . byteLength ( data , 'utf8' ) ;
1212
1313 const results = [ ] ;
14- if ( options . lines ) results . push ( lines ) ;
15- if ( options . words ) results . push ( words ) ;
16- if ( options . bytes ) results . push ( bytes ) ;
14+ if ( options . lines || ( ! options . lines && ! options . words && ! options . bytes ) ) results . push ( lines ) ;
15+ if ( options . words || ( ! options . lines && ! options . words && ! options . bytes ) ) results . push ( words ) ;
16+ if ( options . bytes || ( ! options . lines && ! options . words && ! options . bytes ) ) results . push ( bytes ) ;
1717
1818 console . log ( `${ results . join ( '\t' ) } \t${ file } ` ) ;
19+
20+ return { lines, words, bytes } ;
1921 } catch ( err ) {
2022 console . error ( `wc: ${ file } : ${ err . code === 'ENOENT' ? 'No such file or directory' : 'An error occurred' } ` ) ;
2123 process . exit ( 1 ) ;
@@ -49,9 +51,25 @@ function main() {
4951 process . exit ( 1 ) ;
5052 }
5153
54+ let totalLines = 0 ;
55+ let totalWords = 0 ;
56+ let totalBytes = 0 ;
57+
5258 files . forEach ( ( file ) => {
53- countFile ( file , options ) ;
59+ const { lines, words, bytes } = countFile ( file , options ) ;
60+ totalLines += lines ;
61+ totalWords += words ;
62+ totalBytes += bytes ;
5463 } ) ;
64+
65+ if ( files . length > 1 ) {
66+ const totalResults = [ ] ;
67+ if ( options . lines || ( ! options . lines && ! options . words && ! options . bytes ) ) totalResults . push ( totalLines ) ;
68+ if ( options . words || ( ! options . lines && ! options . words && ! options . bytes ) ) totalResults . push ( totalWords ) ;
69+ if ( options . bytes || ( ! options . lines && ! options . words && ! options . bytes ) ) totalResults . push ( totalBytes ) ;
70+
71+ console . log ( `${ totalResults . join ( '\t' ) } \ttotal` ) ;
72+ }
5573}
5674
5775main ( ) ;
0 commit comments