Skip to content

Commit 176b4d5

Browse files
committed
Ls
1 parent 2691eee commit 176b4d5

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

implement-shell-tools/ls/myLs.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { program } from "commander";
2+
import { promises as fs } from "node:fs";
3+
import process from "node:process";
4+
5+
program
6+
.name("myLs")
7+
.description("my ls clone")
8+
.option("-1", "one entry per line")
9+
.option("-a", "show hidden files")
10+
.argument("[paths...]", "file or directory paths");
11+
12+
program.parse();
13+
14+
const opts = program.opts();
15+
let paths = program.args;
16+
17+
if (paths.length === 0) {
18+
paths = ["."];
19+
}
20+
21+
for (const path of paths) {
22+
const entries = await fs.readdir(path);
23+
24+
for (const file of entries) {
25+
if (!opts.a && file.startsWith(".")) {
26+
continue;
27+
}
28+
29+
if (opts["1"]) {
30+
console.log(file);
31+
} else {
32+
console.log(file + " ");
33+
}
34+
}
35+
36+
}

0 commit comments

Comments
 (0)