File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { program } from "commander" ;
2+ import { error } from "node:console" ;
3+ import { promises as fs , link } from "node:fs" ;
4+
5+ program
6+ . name ( "wc" )
7+ . description ( "wc implementation" )
8+ . argument ( "<paths...>" , "the file path to process" ) ;
9+ program . parse ( ) ;
10+
11+ const paths = program . args ;
12+ const total = {
13+ linesCounter : 0 ,
14+ wordsCounter : 0 ,
15+ characterCounter : 0 ,
16+ } ;
17+ try {
18+ for ( const path of paths ) {
19+ const content = await fs . readFile ( path , "utf-8" ) ;
20+
21+ const linesCounter = content . split ( "\n" ) . length - 1 ;
22+ const wordsCounter = content . trim ( ) . split ( / \s + / ) . length ;
23+ const characterCounter = content . length ;
24+
25+ total . linesCounter += linesCounter ;
26+ total . wordsCounter += wordsCounter ;
27+ total . characterCounter += characterCounter ;
28+
29+ console . log ( ` ${ linesCounter } ${ wordsCounter } ${ characterCounter } ${ path } ` ) ;
30+ }
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+ }
You can’t perform that action at this time.
0 commit comments