|
1 | | -import { dir } from "node:console"; |
2 | 1 | import fs from "node:fs"; |
3 | | -import path from "node:path"; |
4 | 2 | import process from "node:process"; |
5 | 3 |
|
6 | 4 | const args = process.argv.slice(2); |
@@ -41,8 +39,28 @@ function printEntries(entries, onePerLineFlag = flags.has("1")) { |
41 | 39 | if (onePerLineFlag) { |
42 | 40 | entries.forEach((e) => console.log(e)); |
43 | 41 | } 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 | + } |
45 | 46 | } |
46 | 47 | } |
47 | 48 |
|
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