Skip to content

Commit 9ff7642

Browse files
ls implementation complete
1 parent 3a92500 commit 9ff7642

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,21 @@ program
1414
.argument("[directory]", "The file path to process");
1515
program.parse();
1616

17-
const paths = program.args;
1817
const { 1: onePerLine, all } = program.opts();
19-
20-
const directory = paths.length === 0 ? "." : paths[0];
18+
const directory = program.args[0];
2119

2220
let entries = await fs.readdir(directory);
2321

24-
if (onePerLine) {
25-
console.log(entries.filter((entry) => entry[0] !== ".").join("\n"));
26-
// entries.filter((entry) => { entry[0] === "." })
22+
// If -a is used, I've included "." and ".." to mimic what the Unix ls does
23+
if (all) {
24+
entries = [".", "..", ...entries];
25+
} else {
26+
// hide dotfiles
27+
entries = entries.filter((entry) => entry[0] !== ".");
2728
}
2829

30+
if (onePerLine) {
31+
console.log(entries.join("\n"));
32+
} else {
33+
console.log(entries.join(" "));
34+
}

0 commit comments

Comments
 (0)