File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ import { program } from "commander" ;
2+ import { promises as fs } from "node:fs" ;
3+
4+ //config the program
5+ program
6+ . name ( "ls" )
7+ . description ( "list directory contents" )
8+ . option ( "-1, --one" , "Force output to be one entry per line" )
9+ . argument ( "[directory]" , "Directory to list" , "." ) ; // "." means current directory
10+
11+ //interpret the program
12+ program . parse ( ) ;
13+
14+ // Get the directory argument (first argument in program.args array)
15+ // If no argument provided, default to current directory "."
16+ const directory = program . args [ 0 ] || "." ;
17+
18+ //read the directory to get array of filenames
19+ const files = await fs . readdir ( directory ) ;
20+
21+
22+
23+ //print each file on its own line
24+ // Note: console.log(files) would print the entire array like: ['file1', 'file2']
25+ // Loop prints each individually on separate lines
26+ for ( const file of files ) {
27+ console . log ( file )
28+ }
You can’t perform that action at this time.
0 commit comments