Skip to content

Commit 85bb502

Browse files
committed
implement ls
1 parent 8020365 commit 85bb502

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.mjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {program} from "commander";
2+
import {promises as fs} from "node:fs";
3+
4+
program
5+
.name("ls")
6+
.description("List all the files in a directory")
7+
.option("-a, --all", "Include hidden files")
8+
.option("-1", "One entry per line")
9+
.argument("[dir]", "directory to list", ".");
10+
11+
program.parse();
12+
13+
const options = program.opts();
14+
const dir = program.args[0] || ".";
15+
16+
const entries = await fs.readdir(dir, { withFileTypes: true });
17+
18+
const visibleNames = [];
19+
20+
for(const entry of entries){
21+
if(!options.all && entry.name.startsWith(".")) continue;
22+
visibleNames.push(entry.name);
23+
}
24+
25+
if(options["1"]){
26+
console.log(visibleNames.join("\n"));
27+
} else{
28+
console.log(visibleNames.join(" "));
29+
}

0 commit comments

Comments
 (0)