Skip to content

Commit 8675a8b

Browse files
committed
Solving The exercise for ls command.
1 parent 276fc2b commit 8675a8b

File tree

1 file changed

+28
-0
lines changed
  • implement-shell-tools/ls

1 file changed

+28
-0
lines changed

implement-shell-tools/ls/ls.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fs from "node:fs";
2+
import process from "node:process";
3+
4+
// This will give an array without the path to node and to the file.
5+
const argv = process.argv.slice(2);
6+
7+
// filter the flag to find the target folder.
8+
const filePaths = argv.filter((arg) => !arg.startsWith("-"));
9+
const showHiddenFiles = argv.includes("-a");
10+
11+
// if no folder provide we use the current one
12+
const target = filePaths[0] || ".";
13+
// read the file.
14+
const files = fs.readdirSync(target);
15+
16+
// save the result into the variable.
17+
let filteredFIles = files;
18+
if (!showHiddenFiles) {
19+
filteredFIles = files.filter((file) => !file.startsWith("."));
20+
} else {
21+
// we use spread operator to merge the paths.
22+
filteredFIles = [".", "..", ...files];
23+
}
24+
25+
// Print using -1 .
26+
filteredFIles.forEach((file) => {
27+
console.log(file);
28+
});

0 commit comments

Comments
 (0)