File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,18 +6,27 @@ program
66 . name ( "ls" )
77 . description ( "list directory contents" )
88 . option ( "-1, --one" , "Outputs are printed one entry per line" )
9+ . option ( "-a, --all" , "Show all files including hidden files that start with a ." )
910 . argument ( "[directory]" , "Directory to list" , "." ) ; // "." means current directory
1011
1112//interpret the program
1213program . parse ( ) ;
14+ const options = program . opts ( ) ;
1315const directory = program . args [ 0 ] || "." ; //get dir arg- 1st arg in program.args array || if no arg default to current dir
1416
1517
16- const files = await fs . readdir ( directory ) ; //read the dir to get array of filenames
18+ let files = await fs . readdir ( directory ) ; //read the dir to get array of filenames
1719
1820
21+ if ( ! options . all ) { //Handle -a (include hidden files)
22+ files = files . filter ( name => ! name . startsWith ( "." ) ) ;
23+ }
1924
20- //print each file on its own line
21- for ( const file of files ) { // Loop prints each individually on separate lines
22- console . log ( file )
25+ if ( options . one ) { // Print each file on its own line
26+ for ( const file of files ) {
27+ console . log ( file ) ;
28+ }
29+ }
30+ else {
31+ console . log ( files . join ( " " ) ) ; // Default: join with spaces (like ls without -1)
2332}
You can’t perform that action at this time.
0 commit comments