11import { program } from "commander" ;
2- import { error } from "node:console" ;
3- import { promises as fs , link } from "node:fs" ;
2+
3+ import { promises as fs } from "node:fs" ;
44
55program
66 . name ( "wc" )
77 . description ( "wc implementation" )
8- . argument ( "<paths...>" , "the file path to process" ) ;
8+ . argument ( "<paths...>" , "the file path to process" )
9+ . option ( "-l" , "count lines" )
10+ . option ( "-w" , "count words" )
11+ . option ( "-c" , "count characters" ) ;
912program . parse ( ) ;
1013
1114const paths = program . args ;
15+
16+ if ( paths . length === 0 ) {
17+ console . error ( "Expected at least one argument (a path)" ) ;
18+ process . exit ( 1 ) ;
19+ }
20+ const options = program . opts ( ) ;
21+
1222const total = {
1323 linesCounter : 0 ,
1424 wordsCounter : 0 ,
1525 characterCounter : 0 ,
1626} ;
17- try {
18- for ( const path of paths ) {
19- const content = await fs . readFile ( path , "utf-8" ) ;
2027
21- const linesCounter = content . split ( "\n" ) . length - 1 ;
22- const wordsCounter = content . trim ( ) . split ( / \s + / ) . length ;
23- const characterCounter = content . length ;
28+ try {
29+ for ( const path of paths ) {
30+ const content = await fs . readFile ( path , "utf-8" ) ;
31+
32+ const linesCounter = content . split ( "\n" ) . length - 1 ;
33+ const wordsCounter = content . trim ( ) . split ( / \s + / ) . length ;
34+ const characterCounter = content . length ;
35+
36+ total . linesCounter += linesCounter ;
37+ total . wordsCounter += wordsCounter ;
38+ total . characterCounter += characterCounter ;
2439
25- total . linesCounter += linesCounter ;
26- total . wordsCounter += wordsCounter ;
27- total . characterCounter += characterCounter ;
40+ let results = [ ] ;
41+ if ( options . l ) results . push ( linesCounter ) ;
42+ if ( options . w ) results . push ( wordsCounter ) ;
43+ if ( options . c ) results . push ( characterCounter ) ;
2844
29- console . log ( ` ${ linesCounter } ${ wordsCounter } ${ characterCounter } ${ path } ` ) ;
45+ if ( ! options . l && ! options . w && ! options . c )
46+ console . log (
47+ ` ${ linesCounter } ${ wordsCounter } ${ characterCounter } ${ path } ` ,
48+ ) ;
49+ else {
50+ console . log ( results . join ( " " ) + " " + path ) ;
51+ }
52+ }
53+ if ( paths . length > 1 ) {
54+ if ( ! options . l && ! options . w && ! options . c ) {
55+ console . log (
56+ ` ${ total . linesCounter } ${ total . wordsCounter } ${ total . characterCounter } total` ,
57+ ) ;
58+ } else {
59+ const totalWithFlags = [ ] ;
60+ if ( options . l ) totalWithFlags . push ( total . linesCounter ) ;
61+ if ( options . w ) totalWithFlags . push ( total . wordsCounter ) ;
62+ if ( options . c ) totalWithFlags . push ( total . characterCounter ) ;
63+ console . log ( totalWithFlags . join ( " " ) + " total" ) ;
64+ }
65+ }
66+ } catch ( error ) {
67+ console . error ( error . message ) ;
3068}
31- if ( paths . length > 1 )
32- console . log (
33- ` ${ total . linesCounter } ${ total . wordsCounter } ${ total . characterCounter } total` ,
34- ) ;
35- } catch ( error ) {
36- console . log ( error . message ) ;
37- }
0 commit comments