-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathls.js
More file actions
36 lines (26 loc) · 845 Bytes
/
ls.js
File metadata and controls
36 lines (26 loc) · 845 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
29
30
31
32
33
34
35
36
import { program } from "commander";
import process from "node:process";
import fs from "node:fs";
let vertical = false;
let showHidden = false;
program
.name("list-directory-contents")
.description("Shows all files and folders in a directory")
.option("-1", "List one file/directory per line")
.option("-a, --all", "Show hidden files")
.argument("[path]", "Path of the directory to list (defaults to .)");
program.parse();
const argv = program.args;
vertical = program.opts()[1];
showHidden = program.opts().all;
const folderPath = argv[0] || ".";
const contents = fs.readdirSync(folderPath);
const filtered = contents.filter((f) => f[0] !== ".");
const dirArr = !showHidden ? filtered : contents;
if (!vertical) {
console.log(dirArr.join(" "));
} else {
for (const content of dirArr) {
console.log(content);
}
}