Skip to content

Commit 0f7b2b0

Browse files
committed
Implement ls CLI with -a options
1 parent e2983ea commit 0f7b2b0

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { promises as fs } from "node:fs";
2+
import { program } from "commander";
3+
4+
program
5+
.name("ls ")
6+
.description("ls implementation")
7+
.argument("[path]", "The path to process") //zero or one path
8+
.option("-1, --one-per-line", "one file per line")
9+
.option("-a", "show hidden files");
10+
program.parse();
11+
12+
const path = program.args[0] || ".";
13+
const options = program.opts();
14+
try {
15+
const files = await fs.readdir(path);
16+
17+
for (const file of files) {
18+
if (options.a || !file.startsWith(".")) {
19+
console.log(file);
20+
}
21+
}
22+
} catch (error) {
23+
console.error(error.message);
24+
}

0 commit comments

Comments
 (0)