-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathls.js
More file actions
36 lines (24 loc) · 835 Bytes
/
ls.js
File metadata and controls
36 lines (24 loc) · 835 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 process from "node:process";
import { promises as fs } from "node:fs";
import { program } from "commander";
program
.name("list-files-in-directory")
.description("List all files and directories in a directory")
.argument("<path>", "The file path to process")
.option("-1, --one", "Output one entry per line")
.option("-a", "List all files & directories, including hidden ones");
program.parse();
const path = program.args[0];
const options = program.opts();
const directoryContent = await fs.readdir(path);
let allContent = directoryContent;
if (!options.a) {
allContent = directoryContent.filter(name => !name.startsWith("."));
}
for (const item of allContent) {
if (options.one) {
process.stdout.write(item + "\n");
} else {
process.stdout.write(item + " ");
}
}