11const fs = require ( "fs" ) ;
22const { allowedNodeEnvironmentFlags } = require ( "process" ) ;
33
4- const args = process . argv ;
5- const userArgs = args . slice ( 2 ) ;
6-
7- const index = args [ 1 ] . lastIndexOf ( "/" ) ;
8- const currentWorkingDirectory = args [ 1 ] . slice ( 0 , index ) ;
9-
10- const flags = userArgs
11- . filter ( ( arg ) => arg . startsWith ( "-" ) )
12- . map ( ( arg ) => arg . slice ( 1 ) )
13- . join ( "" ) ;
14-
154const flagHandlers = {
165 1 : Flag1 ,
176 a : Flaga ,
@@ -20,81 +9,106 @@ const flagHandlers = {
209let printInList = false ;
2110let showAll = false ;
2211
23- const fsItems = userArgs . filter ( ( arg ) => ! arg . startsWith ( "-" ) ) ;
24- let dirArgs ;
25- let fileArgs ;
26- let outputString = "" ;
12+ ls ( process . argv ) ;
13+
14+ // ----------
15+
16+ function ls ( args ) {
17+ const userArgs = getUserArgs ( args ) ;
18+ const currentWorkingDirectory = getCurrentWorkingDirectory ( args ) ;
19+ const fsItems = checkArgsLength ( getFsItems ( userArgs ) ) ;
20+
21+ executeFlags ( getFlags ( userArgs ) ) ;
22+
23+ const dirArgs = getDirArgs ( fsItems , currentWorkingDirectory ) ;
24+ const fileArgs = getFileArgs ( fsItems , currentWorkingDirectory ) ;
25+
26+ print ( populateOutput ( fsItems , dirArgs , fileArgs , currentWorkingDirectory ) ) ;
27+ }
28+
29+ function getUserArgs ( args ) {
30+ return args . slice ( 2 ) ;
31+ }
32+
33+ function getFsItems ( userArgs ) {
34+ return userArgs . filter ( ( arg ) => ! arg . startsWith ( "-" ) ) ;
35+ }
36+
37+ function getCurrentWorkingDirectory ( args ) {
38+ const index = args [ 1 ] . lastIndexOf ( "/" ) ;
39+ return args [ 1 ] . slice ( 0 , index ) ;
40+ }
2741
28- checkArgsLength ( ) ;
29- executeFlags ( ) ;
30- filterFilesAndDirs ( ) ;
31- populateOutput ( ) ;
32- print ( ) ;
42+ function getFlags ( userArgs ) {
43+ return userArgs
44+ . filter ( ( arg ) => arg . startsWith ( "-" ) )
45+ . map ( ( arg ) => arg . slice ( 1 ) )
46+ . join ( "" ) ;
47+ }
3348
34- function executeFlags ( ) {
49+ function executeFlags ( flags ) {
3550 for ( const flag of flags ) {
36- if ( flagHandlers [ flag ] ) {
37- allFilesContents = flagHandlers [ flag ] ( ) ;
38- } else {
39- console . error (
40- `ls: invalid option -- ${ flag } \nusage: ls [-@ABCFGHILOPRSTUWXabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]` ,
41- ) ;
42- process . exit ( 1 ) ;
43- }
51+ if ( flagHandlers [ flag ] ) flagHandlers [ flag ] ( ) ;
52+ else invalidFlagError ( ) ;
4453 }
4554}
4655
47- function Flag1 ( ) {
48- if ( printInList ) {
49- outputString = outputString . replaceAll ( "\t" , "\n" ) ;
50- outputString = outputString . replaceAll ( "\n\n" , "\n" ) ;
51- } else {
52- printInList = true ;
53- }
56+ function invalidFlagError ( ) {
57+ console . error (
58+ `ls: invalid option -- ${ flag } \nusage: ls [-@ABCFGHILOPRSTUWXabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]` ,
59+ ) ;
60+ process . exit ( 1 ) ;
61+ }
62+
63+ function Flag1 ( outputString ) {
64+ if ( printInList ) return outputString . replaceAll ( "\t" , "\n" ) . replaceAll ( "\n\n" , "\n" ) ;
65+ else printInList = true ;
5466}
5567
5668function Flaga ( ) {
5769 showAll = true ;
5870}
5971
60- function checkArgsLength ( ) {
61- if ( fsItems . length == 0 ) {
62- fsItems . push ( "." ) ;
63- }
72+ function checkArgsLength ( fsItems ) {
73+ if ( fsItems . length == 0 ) fsItems . push ( "." ) ;
74+ return fsItems ;
6475}
6576
66- function filterFilesAndDirs ( ) {
67- dirArgs = fsItems . filter ( ( p ) => fs . statSync ( currentWorkingDirectory + "/" + p ) . isDirectory ( ) ) ;
68- fileArgs = fsItems . filter ( ( p ) => ! fs . statSync ( currentWorkingDirectory + "/" + p ) . isDirectory ( ) ) ;
69- if ( showAll == false ) {
70- removeDotFiles ( fileArgs ) ;
71- }
77+ function getDirArgs ( fsItems , currentWorkingDirectory ) {
78+ return fsItems . filter ( ( p ) => fs . statSync ( currentWorkingDirectory + "/" + p ) . isDirectory ( ) ) ;
79+ }
80+
81+ function getFileArgs ( fsItems , currentWorkingDirectory ) {
82+ const fileArgs = fsItems . filter ( ( p ) => ! fs . statSync ( currentWorkingDirectory + "/" + p ) . isDirectory ( ) ) ;
83+ if ( showAll == false ) removeDotFiles ( fileArgs ) ;
84+ return fileArgs ;
7285}
7386
74- function populateOutput ( ) {
87+ function populateOutput ( fsItems , dirArgs , fileArgs , currentWorkingDirectory ) {
88+ let outputString = "" ;
7589 if ( fsItems . length == 1 ) {
7690 fileArgs . forEach ( ( file ) => {
7791 outputString += file + " " ;
7892 } ) ;
7993 dirArgs . forEach ( ( dir ) => {
80- outputString += dirOutput ( dir ) ;
94+ outputString += dirOutput ( dir , currentWorkingDirectory ) ;
8195 } ) ;
8296 } else {
8397 fileArgs . forEach ( ( file ) => {
8498 outputString += file + "\t" ;
8599 } ) ;
86100 dirArgs . forEach ( ( dir ) => {
87- outputString += "\n\n" + dir + ":\n" + dirOutput ( dir ) ;
101+ outputString += "\n\n" + dir + ":\n" + dirOutput ( dir , currentWorkingDirectory ) ;
88102 } ) ;
89103 }
104+ return outputString ;
90105}
91106
92- function dirOutput ( dir ) {
107+ function dirOutput ( dir , currentWorkingDirectory ) {
93108 let contents = fs . readdirSync ( currentWorkingDirectory + "/" + dir ) ;
94109 let outputStr = "" ;
95- if ( showAll ) {
96- outputStr += ".\t..\t" ;
97- } else {
110+ if ( showAll ) outputStr += ".\t..\t" ;
111+ else {
98112 for ( let i = contents . length - 1 ; i >= 0 ; i -- ) {
99113 contents = removeDotFiles ( contents ) ;
100114 }
@@ -107,16 +121,13 @@ function dirOutput(dir) {
107121
108122function removeDotFiles ( fileList ) {
109123 for ( let i = fileList . length - 1 ; i >= 0 ; i -- ) {
110- if ( fileList [ i ] [ 0 ] == "." ) {
111- fileList . splice ( i , 1 ) ;
112- }
124+ if ( fileList [ i ] [ 0 ] == "." ) fileList . splice ( i , 1 ) ;
113125 }
114126 return fileList ;
115127}
116128
117- function print ( ) {
118- if ( printInList ) {
119- Flag1 ( ) ;
120- }
121- console . log ( outputString . trimEnd ( ) ) ;
129+ function print ( outputString ) {
130+ let output = outputString ;
131+ if ( printInList ) output = Flag1 ( output ) ;
132+ console . log ( output . trimEnd ( ) ) ;
122133}
0 commit comments