-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathls.js
More file actions
24 lines (16 loc) · 668 Bytes
/
ls.js
File metadata and controls
24 lines (16 loc) · 668 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
import { program } from "commander";
import { promises as fs } from "node:fs";
program
.name("my-ls")
.description("Reimplementation of the Unix `ls` command with -1 and -a options")
.option("-1", "list one file per line")
.option("-a", "include hidden files")
.argument("[directory]", "directory to list");
program.parse();
const options = program.opts();
const directory = program.args[0] || "."; // Use current directory as default if no argument is provided
const files = await fs.readdir(directory);
const visibleFiles = options.a ? files : files.filter(file => !file.startsWith("."));
for (const file of visibleFiles) {
console.log(file);
}