@@ -11,77 +11,80 @@ program
1111 . option ( "-1" , "List all files, one per line" )
1212 . option ( "-a, --all" , "Include hidden files (those starting with .) in the listing" )
1313 . action ( async ( directory , options ) => {
14- if ( program . args . length > 1 ) {
15- console . error ( `Expected no more than 1 argument (sample-files) but got ${ program . args . length } .` ) ;
16- process . exit ( 1 ) ;
17- }
14+ try {
15+ // default to current directory if none is specified
16+ const dir = directory || "." ;
1817
19- // default directory to current folder
20- directory = directory || "." ;
21-
22- await newLs ( directory , options [ '1' ] , options . all ) ;
18+ await newLs ( dir , options [ '1' ] , options . all ) ;
19+ } catch ( err ) {
20+ console . error ( `Error: ${ err . message } ` ) ;
21+ }
2322 } ) ;
2423
2524program . parse ( process . argv ) ;
2625
2726
27+ // filter files based on visibility (includeHidden = true includes all files)
28+ function filterFiles ( entries , includeHidden ) {
29+ return entries . filter ( name =>
30+ includeHidden ? true : ! name . startsWith ( "." )
31+ ) ;
32+ }
2833
34+ // sort entries: directories first, then files,
2935function sortEntries ( entries ) {
36+ const dirs = entries . filter ( entry => {
37+ try {
38+ return fs . statSync ( entry ) . isDirectory ( ) ;
39+ } catch ( err ) {
40+ return false ;
41+ }
42+ } ) ;
43+
44+ const files = entries . filter ( entry => {
45+ try {
46+ return fs . statSync ( entry ) . isFile ( ) ;
47+ } catch ( err ) {
48+ return false ;
49+ }
50+ } ) ;
3051 // localeCompare = take into account rules of system language/region for ordering
3152 // undefined = uses the system default, numeric = regular number sorting, base = ignore case & accents
32- return entries . sort ( ( a , b ) =>
33- a . localeCompare ( b , undefined , { numeric : true , sensitivity : ' base' } )
53+ return entries . sort ( ( a , b ) =>
54+ a . localeCompare ( b , undefined , { numeric : true , sensitivity : " base" } )
3455 ) ;
3556}
3657
3758
59+ // print entries either one per line (-1 flag)
60+ function printEntries ( entries ) {
61+ entries . forEach ( entry => console . log ( entry ) ) ;
62+ }
63+
64+
3865async function newLs ( directory , oneFlag , allFlag ) {
3966 try {
40- // check if the path exists
67+ // check if path exists and determine if file or directory
4168 const stats = await fs . stat ( directory ) ;
42-
43- // if it’s a file, just print its name
69+
70+ // if a file, just print the name
4471 if ( stats . isFile ( ) ) {
4572 console . log ( directory ) ;
4673 return ;
47- }
74+ }
4875
49- // read directory contents
76+ // reads directory contents
5077 const entries = await fs . readdir ( directory ) ;
5178
52- let finalEntries ;
53-
54- // organize -a output (visible files (doesn't start with a .) and hidden files (starts with a .))
55- const visibleFiles = entries . filter ( name => ! name . startsWith ( '.' ) ) ;
56- const hiddenFiles = entries . filter ( name => name . startsWith ( '.' ) && name !== '.' && name !== '..' ) ;
57-
58- if ( allFlag ) {
59- // add visible and hidden files to the new ['.', '..'] array literal
60- finalEntries = [ '.' , '..' ]
61- . concat ( sortEntries ( visibleFiles ) )
62- . concat ( sortEntries ( hiddenFiles ) ) ;
63- } else {
64- // return sorted array with visible files
65- finalEntries = sortEntries ( visibleFiles ) ;
66- }
67-
68- // organize -1 output
69- if ( oneFlag ) {
70- for ( const entry of finalEntries ) {
71- console . log ( entry ) ;
72- }
73- } else {
74- //no flags (separated by 2 spaces)
75- console . log ( finalEntries . join ( ' ' ) ) ;
76- }
79+ // Filter out hidden files if no -a flag
80+ const filteredEntries = filterFiles ( entries , allFlag ) ;
7781
82+ // Sort the entries using the sortEntries helper
83+ const sortedEntries = sortEntries ( filteredEntries ) ;
84+
85+ // print entries for -1 flag (one per line)
86+ printEntries ( sortedEntries ) ;
7887 } catch ( err ) {
79- console . error ( `ls: cannot access '${ directory } ': No such file or directory ` ) ;
88+ console . error ( `ls: cannot access '${ directory } ': ${ err . message } ` ) ;
8089 }
81- }
82-
83-
84-
85-
86-
87-
90+ }
0 commit comments