11import { promises as fs } from "node:fs" ;
22import { program } from "commander" ;
3- import { get } from "node:http" ;
43
54program
65 . name ( "print newline, word, and byte counts for each file" )
7- . option ( "-a" , "to do something" )
6+ . option ( "-l" , "count lines" )
7+ . option ( "-w" , "count words" )
8+ . option ( "-c" , "count bytes" )
89 . argument ( "<paths...>" , "file name" ) ;
910
1011program . parse ( ) ;
1112
13+ const opts = program . opts ( ) ;
14+ const flags = Object . keys ( opts ) ;
15+ const columns = flags . length > 0 ? flags : [ "l" , "w" , "c" ] ;
1216const paths = program . args ;
1317
14- async function getNoFlagsOutput ( paths ) {
15- let lineCountTotal = 0 ;
16- let wordCountTotal = 0 ;
17- let fileSizeTotal = 0 ;
18+ async function getFilesData ( paths ) {
1819 let output = [ ] ;
1920
2021 for ( const filename of paths ) {
@@ -23,14 +24,16 @@ async function getNoFlagsOutput(paths) {
2324
2425 if ( file . isFile ( ) ) {
2526 const fileContent = await fs . readFile ( filename , "utf-8" ) ;
26- const lineCount = fileContent . match ( / \n / g) . length ;
27+ const lineCount = ( fileContent . match ( / \n / g) || [ ] ) . length ;
2728 const wordCount = fileContent
2829 . trim ( )
2930 . split ( / \s + / )
3031 . filter ( Boolean ) . length ;
3132 const fileSize = file . size ;
3233
33- output . push ( [ lineCount , wordCount , fileSize , filename ] ) ;
34+ output . push ( [ filename , lineCount , wordCount , fileSize ] ) ;
35+ } else {
36+ output . push ( [ filename , `wc: ${ filename } : Is a directory` ] ) ;
3437 }
3538 } catch ( err ) {
3639 output . push ( [ filename , `wc: ${ filename } ${ err . message } ` ] ) ;
@@ -39,5 +42,41 @@ async function getNoFlagsOutput(paths) {
3942
4043 return output ;
4144}
42- //console.log(`${lineCountTotal} ${wordCountTotal} ${fileSizeTotal}Total`);
43- console . log ( JSON . stringify ( await getNoFlagsOutput ( paths ) ) ) ;
45+
46+ function displayWcOutput ( output , columns ) {
47+ const results = output . filter ( ( entry ) => entry . length === 4 ) ;
48+
49+ const totalLines = results . reduce ( ( sum , e ) => sum + e [ 1 ] , 0 ) ;
50+ const totalWords = results . reduce ( ( sum , e ) => sum + e [ 2 ] , 0 ) ;
51+ const totalBytes = results . reduce ( ( sum , e ) => sum + e [ 3 ] , 0 ) ;
52+
53+ const allRows =
54+ results . length > 1
55+ ? [ ...results , [ "total" , totalLines , totalWords , totalBytes ] ]
56+ : results ;
57+
58+ const colMap = { l : 1 , w : 2 , c : 3 } ;
59+ const w = columns . map ( ( col ) =>
60+ Math . max ( ...allRows . map ( ( e ) => String ( e [ colMap [ col ] ] ) . length ) ) ,
61+ ) ;
62+
63+ const formatRow = ( entry ) =>
64+ columns
65+ . map ( ( col , i ) => String ( entry [ colMap [ col ] ] ) . padStart ( w [ i ] ) )
66+ . join ( " " ) ;
67+
68+ for ( const entry of output ) {
69+ if ( entry . length === 2 ) {
70+ console . log ( entry [ 1 ] ) ;
71+ } else {
72+ console . log ( `${ formatRow ( entry ) } ${ entry [ 0 ] } ` ) ;
73+ }
74+ }
75+
76+ if ( results . length > 1 ) {
77+ const totalsRow = [ "total" , totalLines , totalWords , totalBytes ] ;
78+ console . log ( `${ formatRow ( totalsRow ) } total` ) ;
79+ }
80+ }
81+
82+ displayWcOutput ( await getFilesData ( paths ) , columns ) ;
0 commit comments