File tree Expand file tree Collapse file tree
implement-shell-tools/cat Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { program } from "commander" ;
2+ import { error } from "node:console" ;
23import { promises as fs } from "node:fs" ;
34
45program
5- . name ( "implement my own version of ` cat`. " )
6- . description ( "I do not know what I'm doing " )
6+ . name ( "cat" )
7+ . description ( "cat sample-files/1.txt " )
78 . argument ( "<path>" , "The file path to process" ) ;
89program . parse ( ) ;
910
1011const argv = program . args ;
1112if ( argv . length != 1 ) {
12- console . error ( `Expected exactly 1 argument (a path) to be passed but got ${ argv . length } ` ) ;
13- process . exit ( 1 ) ;
13+ console . error (
14+ `Expected exactly 1 argument (a path) to be passed but got ${ argv . length } ` ,
15+ ) ;
16+ process . exit ( 1 ) ;
1417}
1518const path = argv [ 0 ] ;
16-
17- const content = await fs . readFile ( path , "utf-8" ) ;
18- console . log ( content ) ;
19+ try {
20+ const content = await fs . readFile ( path , "utf-8" ) ;
21+ process . stdout . write ( content ) ;
22+ } catch ( error ) {
23+ console . error ( error . message ) ;
24+ process . exit ( 1 ) ;
25+ }
You can’t perform that action at this time.
0 commit comments