11import { program } from "commander" ;
22import { promises as fs } from "node:fs" ;
3- import process from "node:process" ;
43
54program
65 . name ( "wc command" )
76 . description ( "Implement wc command to count words and lines" )
8- . option ( "-l," , "show number of lines only" )
9- . option ( "-w," , "show number of words only" )
10- . option ( "-c," , "show number of characters only" )
11- . argument ( "[path ...]" , "The file path to process" ) ;
7+ . option ( "-l, --lines " , "show number of lines only" )
8+ . option ( "-w, --words " , "show number of words only" )
9+ . option ( "-c, --chars " , "show number of characters only" )
10+ . argument ( "[paths ...]" , "The file path to process" ) ;
1211
1312program . parse ( process . argv ) ;
1413
1514const opts = program . opts ( ) ;
1615let paths = program . args ;
1716
1817if ( paths . length === 0 ) {
19- console . error ( "wc: no file specified" ) ;
20- process . exit ( 1 ) ;
18+ paths = [ "." ] ;
2119}
2220
2321let totals = { lines : 0 , words : 0 , chars : 0 } ;
@@ -28,53 +26,38 @@ for (const filePath of paths) {
2826 content = await fs . readFile ( filePath , "utf8" ) ;
2927 } catch ( err ) {
3028 console . error ( `wc: cannot read file "${ filePath } ": ${ err . message } ` ) ;
31- continue ; // move on to next path
29+ continue ;
3230 }
3331
34- const lineCount = content . split ( "n" ) . length ;
35-
32+ const lineCount = content . split ( "\n" ) . length ;
3633 const wordCount = content . split ( / \s + / ) . filter ( Boolean ) . length ;
37-
3834 const charCount = content . length ;
3935
4036 totals . lines += lineCount ;
4137 totals . words += wordCount ;
4238 totals . chars += charCount ;
4339
44- // Decide what to print depending on flags
45- if ( ! opts . l && ! opts . w && ! opts . c ) {
46- console . log ( `${ lineCount } ${ wordCount } ${ charCount } ${ filePath } ` ) ;
47- continue ;
48- }
49-
50- if ( opts . l ) {
51- console . log ( `${ lineCount } ${ filePath } ` ) ;
52- }
53-
54- if ( opts . w ) {
55- console . log ( `${ wordCount } ${ filePath } ` ) ;
56- }
40+ const output = [ ] ;
5741
58- if ( opts . c ) {
59- console . log ( `${ charCount } ${ filePath } ` ) ;
42+ if ( ! opts . lines && ! opts . words && ! opts . chars ) {
43+ output . push ( lineCount , wordCount , charCount ) ;
44+ } else {
45+ if ( opts . lines ) output . push ( lineCount ) ;
46+ if ( opts . words ) output . push ( wordCount ) ;
47+ if ( opts . chars ) output . push ( charCount ) ;
6048 }
49+ console . log ( `${ output . join ( " " ) } ${ filePath } ` ) ;
6150}
6251
63- //Print totals if there are multiple files
6452if ( paths . length > 1 ) {
65- if ( ! opts . l && ! opts . w && ! opts . c ) {
66- console . log ( `${ totals . lines } ${ totals . words } ${ totals . chars } total` ) ;
67- }
68-
69- if ( opts . l ) {
70- console . log ( `${ totals . lines } total` ) ;
71- }
72-
73- if ( opts . w ) {
74- console . log ( `${ totals . words } total` ) ;
75- }
53+ const totalOutput = [ ] ;
7654
77- if ( opts . c ) {
78- console . log ( `${ totals . chars } total` ) ;
55+ if ( ! opts . lines && ! opts . words && ! opts . chars ) {
56+ totalOutput . push ( totals . lines , totals . words , totals . chars ) ;
57+ } else {
58+ if ( opts . lines ) totalOutput . push ( totals . lines ) ;
59+ if ( opts . words ) totalOutput . push ( totals . words ) ;
60+ if ( opts . chars ) totalOutput . push ( totals . chars ) ;
7961 }
62+ console . log ( `${ totalOutput . join ( " " ) } total` ) ;
8063}
0 commit comments