Skip to content

Commit 9983327

Browse files
committed
Fix -a flag handling to include current and parent dir while running with node to mimic ls
1 parent 1527ac2 commit 9983327

File tree

1 file changed

+9
-2
lines changed
  • implement-shell-tools/ls

1 file changed

+9
-2
lines changed

implement-shell-tools/ls/ls.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@ const directory = program.args[0] || "."; //get dir arg- 1st arg in program.args
1717

1818
let files = await fs.readdir(directory); //read the dir to get array of filenames
1919

20-
21-
if (!options.all) { //Handle -a (include hidden files)
20+
//Handle -a (include hidden files)
21+
// Node's fs.readdir() does not include the special directory entries "." (current dir)
22+
// and ".." (parent dir). The real `ls -a` command shows them, so we add them manually here
23+
// to match the behavior of `ls -a`.
24+
if (options.all) {
25+
26+
files.unshift("..");
27+
files.unshift(".");
28+
} else {
2229
files = files.filter(name => !name.startsWith("."));
2330
}
2431

0 commit comments

Comments
 (0)