Skip to content

Commit 98c1c43

Browse files
committed
ls done
1 parent abce30a commit 98c1c43

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

  • implement-shell-tools/ls

implement-shell-tools/ls/ls.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { dir } from "node:console";
21
import fs from "node:fs";
3-
import path from "node:path";
42
import process from "node:process";
53

64
const args = process.argv.slice(2);
@@ -41,8 +39,28 @@ function printEntries(entries, onePerLineFlag = flags.has("1")) {
4139
if (onePerLineFlag) {
4240
entries.forEach((e) => console.log(e));
4341
} else {
44-
console.log(entries.join("\t"));
42+
// if join on empty entries arr, add extra blank line
43+
if (entries.length !== 0) {
44+
console.log(entries.join("\t"));
45+
}
4546
}
4647
}
4748

48-
printEntries(getPathEntries(paths[0]));
49+
// this is needed to group files at the top and folers at the bottom when giving mutiple path arguements
50+
// to argv e.g. node ls.js sample-files/*
51+
const fileArgs = paths.filter((p) => !fs.statSync(p).isDirectory());
52+
const dirArgs = paths.filter((p) => fs.statSync(p).isDirectory());
53+
54+
// First print all plain file arguments together, as one group
55+
if (fileArgs.length > 0) {
56+
printEntries(fileArgs);
57+
}
58+
59+
// Then print each directory's listing, with headers if needed
60+
dirArgs.forEach((path, index) => {
61+
if (paths.length > 1) {
62+
if (index > 0 || fileArgs.length > 0) console.log("");
63+
console.log(`${path}:`);
64+
}
65+
printEntries(getPathEntries(path));
66+
});

0 commit comments

Comments
 (0)