|
1 | 1 | import { promises as fs } from "node:fs"; |
2 | 2 | import { program } from "commander"; |
| 3 | +import { dirxml } from "node:console"; |
3 | 4 |
|
4 | 5 | program |
5 | | - .name("ls implementation") |
6 | | - .description("ls -1 program") |
| 6 | + .name("ls ") |
| 7 | + .description("ls implementation") |
7 | 8 | .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"); |
9 | 11 | program.parse(); |
10 | 12 |
|
11 | 13 | const path = program.args[0] || "."; |
12 | 14 | const options = program.opts(); |
13 | 15 | try { |
14 | 16 | 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) { |
17 | 22 | for (const file of files) { |
18 | 23 | if (!file.startsWith(".")) { |
19 | 24 | console.log(file); |
20 | 25 | } |
21 | 26 | } |
| 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 | + } |
22 | 37 | } |
23 | 38 | } catch (error) { |
24 | 39 | console.error(error.message); |
|
0 commit comments