Skip to content

Commit b91a308

Browse files
Add -a flag to ls to show hidden files
1 parent dec157e commit b91a308

File tree

1 file changed

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

1 file changed

+17
-2
lines changed

implement-shell-tools/ls/ls.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ program
66
.name("ls")
77
.description("list directory contents")
88
.option("-1, --one", "Force output to be one entry per line")
9+
.option(
10+
"-a, --all",
11+
"shows all the files including the hidden ones which start with a dot"
12+
)
913
.argument("[directory]", "Directory to list", "."); // "." means current directory
1014

1115
//interpret the program
@@ -18,11 +22,22 @@ const directory = program.args[0] || ".";
1822
//read the directory to get array of filenames
1923
const files = await fs.readdir(directory);
2024

25+
//check for flags
26+
const hasAflag = program.opts().all;
27+
28+
// Filter the files array BEFORE looping
29+
// If hasAflag is true, keep all files
30+
// If hasAflag is false, remove files that start with "."
31+
32+
const fileToShow = hasAflag
33+
? files
34+
: files.filter(file => !file.startsWith("."))
2135

2236

2337
//print each file on its own line
2438
// Note: console.log(files) would print the entire array like: ['file1', 'file2']
2539
// Loop prints each individually on separate lines
26-
for (const file of files) {
40+
41+
for (const file of fileToShow) {
2742
console.log(file)
28-
}
43+
}

0 commit comments

Comments
 (0)