-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathls.js
More file actions
28 lines (23 loc) · 797 Bytes
/
ls.js
File metadata and controls
28 lines (23 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import fs from "node:fs";
import process from "node:process";
// This will give an array without the path to node and to the file.
const argv = process.argv.slice(2);
// filter the flag to find the target folder.
const filePaths = argv.filter((arg) => !arg.startsWith("-"));
const showHiddenFiles = argv.includes("-a");
// if no folder provide we use the current one
const target = filePaths[0] || ".";
// read the file.
const files = fs.readdirSync(target);
// save the result into the variable.
let filteredFIles = files;
if (!showHiddenFiles) {
filteredFIles = files.filter((file) => !file.startsWith("."));
} else {
// we use spread operator to merge the paths.
filteredFIles = [".", "..", ...files];
}
// Print using -1 .
filteredFIles.forEach((file) => {
console.log(file);
});