Skip to content

Commit 19cae55

Browse files
committed
implement ls with -1 and -a flags
1 parent b40010c commit 19cae55

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { promises as fs } from "node:fs";
2+
import process from "node:process";
3+
4+
const args = process.argv.slice(2);
5+
6+
const showHidden = args.includes("-a");
7+
8+
const paths = args.filter((arg) => !arg.startsWith("-"));
9+
10+
const targetPath = paths.length > 0 ? paths[0] : process.cwd();
11+
12+
let entries = await fs.readdir(targetPath);
13+
14+
if (!showHidden) {
15+
entries = entries.filter((entry) => !entry.startsWith("."));
16+
}
17+
18+
if (showHidden) {
19+
entries = [".", "..", ...entries];
20+
}
21+
22+
entries.sort((a, b) => {
23+
const cleanA = a.replace(/^\.+/, "");
24+
const cleanB = b.replace(/^\.+/, "");
25+
return cleanA.localeCompare(cleanB);
26+
});
27+
28+
for (const entry of entries) {
29+
process.stdout.write(entry + "\n");
30+
}

0 commit comments

Comments
 (0)