Skip to content

Commit fa3da24

Browse files
committed
Create ls.js
Complete ls implementation with -1 and -a support
1 parent 4350f48 commit fa3da24

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const fs = require("fs");
2+
3+
const args = process.argv.slice(2);
4+
5+
const showAll = args.includes("-a");
6+
7+
const dirArg = args.find(arg => !arg.startsWith("-"));
8+
const directory = dirArg || ".";
9+
10+
fs.readdir(directory, (err, files) => {
11+
if (err) {
12+
console.error(err.message);
13+
return;
14+
}
15+
16+
let output = files;
17+
18+
if (!showAll) {
19+
output = files.filter(file => !file.startsWith("."));
20+
} else {
21+
output = [".", "..", ...files];
22+
}
23+
24+
output.forEach(file => console.log(file));
25+
});

0 commit comments

Comments
 (0)