-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathls.js
More file actions
35 lines (27 loc) · 910 Bytes
/
ls.js
File metadata and controls
35 lines (27 loc) · 910 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
import { program } from "commander";
import { promises as fs } from "node:fs";
program
.name("ls")
.description("Displays files in a directory")
.option("-1, --one", "Display each file on a new line")// for listing one file per line
.option("-a, --all", "Include hidden files")
.argument("[filepath]", "Directory to list files from (default: current)")
await program.parseAsync();
const opts = program.opts();
const args = program.args;
const path = args[0] || "."; //Use current directory if no path provided
let files ;
try {
files = await fs.readdir(path);
}catch (err) {
console.error(`ls: cannot access '${path}': ${err.message}`);
process.exit(1);
}
if (!opts.all) {
files = files.filter(file => !file.startsWith("."));// if path not provided use current directory
}
if (opts.one) {
files.forEach(file => console.log(file));
} else {
console.log(files.join(" "));
}