-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathls.js
More file actions
28 lines (21 loc) · 921 Bytes
/
ls.js
File metadata and controls
28 lines (21 loc) · 921 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
import { promises as fs } from "node:fs";
//getting all commands
const args = process.argv.slice(2);
const showOnePerLine = args.includes("-1");
const showAllFilesWithHidden = args.includes("-a");
//current path
const path = args.find(arg => !arg.startsWith("-")) || ".";
// if we do console.log("path=> ",path," args=> " ,args); it will give us this =>: path=> sample-files args=> [ '-1', '-a', 'sample-files' ]
const direc = await fs.readdir(path)
// if the path is <sample-files> console.log(direc) gives us =>: [ '.hidden.txt', '1.txt', '2.txt', '3.txt', 'dir' ]
if(showOnePerLine && showAllFilesWithHidden){
direc.forEach(element => {
console.log(element)
})
}
else if(showOnePerLine){
const visibleFiles = direc.filter(element => !element.startsWith(".")); // filtering hidden files which starts with "."
visibleFiles.forEach(element => {
console.log(element)
});
}