File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed
Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change 66 . name ( "ls" )
77 . description ( "list directory contents" )
88 . option ( "-1, --one" , "Force output to be one entry per line" )
9+ . option (
10+ "-a, --all" ,
11+ "shows all the files including the hidden ones which start with a dot"
12+ )
913 . argument ( "[directory]" , "Directory to list" , "." ) ; // "." means current directory
1014
1115//interpret the program
@@ -18,11 +22,22 @@ const directory = program.args[0] || ".";
1822//read the directory to get array of filenames
1923const files = await fs . readdir ( directory ) ;
2024
25+ //check for flags
26+ const hasAflag = program . opts ( ) . all ;
27+
28+ // Filter the files array BEFORE looping
29+ // If hasAflag is true, keep all files
30+ // If hasAflag is false, remove files that start with "."
31+
32+ const fileToShow = hasAflag
33+ ? files
34+ : files . filter ( file => ! file . startsWith ( "." ) )
2135
2236
2337//print each file on its own line
2438// Note: console.log(files) would print the entire array like: ['file1', 'file2']
2539// Loop prints each individually on separate lines
26- for ( const file of files ) {
40+
41+ for ( const file of fileToShow ) {
2742 console . log ( file )
28- }
43+ }
You can’t perform that action at this time.
0 commit comments