-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathls.js
More file actions
32 lines (27 loc) · 849 Bytes
/
ls.js
File metadata and controls
32 lines (27 loc) · 849 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
import { program } from "commander";
import { promises as fs } from "node:fs";
program
.name("node-ls")
.description("A Node.js implementation of the Unix ls command")
.option("-1", "list one file per line")
.option(
"-a, --all",
"include directory entries whose names begin with a dot (.)"
)
.argument("[directory]", "The file path to process");
program.parse();
const { 1: onePerLine, all } = program.opts();
const directory = program.args[0] ? program.args[0] : ".";
let entries = await fs.readdir(directory);
// If -a is used, I've included "." and ".." to mimic what the Unix ls does
if (all) {
entries = [".", "..", ...entries];
} else {
// hide dotfiles
entries = entries.filter((entry) => entry[0] !== ".");
}
if (onePerLine) {
console.log(entries.join("\n"));
} else {
console.log(entries.join(" "));
}