File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Expand file tree Collapse file tree 1 file changed +29
-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+ program
5+ . name ( "ls command" )
6+ . description ( "Implementing 'ls' command" )
7+ . option ( "-1" , "list one file per line" )
8+ . option ( "-a" , "include hidden files" )
9+ . argument ( "[directory]" , "Directory to list" ) ;
10+
11+ program . parse ( ) ;
12+
13+ const directory = program . args [ 0 ] || "." ; //current directory as a defult if no directory provided
14+ const allFiles = program . opts ( ) . a ;
15+ const listPerLine = program . opts ( ) [ "1" ] ;
16+
17+
18+
19+ const files = await fs . readdir ( directory ) ;
20+ const visibleFiles = allFiles ? files : files . filter ( file => ! file . startsWith ( "." ) ) ;
21+
22+ if ( listPerLine ) {
23+ for ( const file of visibleFiles ) {
24+ console . log ( file )
25+ }
26+ } else {
27+ console . log ( visibleFiles . join ( " " ) )
28+ }
29+
You can’t perform that action at this time.
0 commit comments