Skip to content

Commit 6c2dcfa

Browse files
committed
ls program has been completed
1 parent 58f7d44 commit 6c2dcfa

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.mjs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
import { promises as fs } from "node:fs";
22
import { program } from "commander";
3+
import { dirxml } from "node:console";
34

45
program
5-
.name("ls implementation")
6-
.description("ls -1 program")
6+
.name("ls ")
7+
.description("ls implementation")
78
.argument("[path]", "The path to process")
8-
.option("-1, --one-per-line", "one file per line");
9+
.option("-1, --one-per-line", "one file per line")
10+
.option("-a", "show hidden files");
911
program.parse();
1012

1113
const path = program.args[0] || ".";
1214
const options = program.opts();
1315
try {
1416
const files = await fs.readdir(path);
15-
16-
if (options.onePerLine) {
17+
if (options.onePerLine && options.a) {
18+
for (const file of files) {
19+
console.log(file);
20+
}
21+
} else if (options.onePerLine) {
1722
for (const file of files) {
1823
if (!file.startsWith(".")) {
1924
console.log(file);
2025
}
2126
}
27+
} else if (options.a) {
28+
for (const file of files) {
29+
process.stdout.write(file + " ");
30+
}
31+
} else {
32+
for (const file of files) {
33+
if (!file.startsWith(".")) {
34+
process.stdout.write(file + " ");
35+
}
36+
}
2237
}
2338
} catch (error) {
2439
console.error(error.message);

0 commit comments

Comments
 (0)