Skip to content

Commit 3a30d97

Browse files
Implement ls with -1 flag and directory argument support
1 parent e50422e commit 3a30d97

File tree

1 file changed

+28
-0
lines changed
  • implement-shell-tools/ls

1 file changed

+28
-0
lines changed

implement-shell-tools/ls/ls.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { program } from "commander";
2+
import { promises as fs } from "node:fs";
3+
4+
//config the program
5+
program
6+
.name("ls")
7+
.description("list directory contents")
8+
.option("-1, --one", "Force output to be one entry per line")
9+
.argument("[directory]", "Directory to list", "."); // "." means current directory
10+
11+
//interpret the program
12+
program.parse();
13+
14+
// Get the directory argument (first argument in program.args array)
15+
// If no argument provided, default to current directory "."
16+
const directory = program.args[0] || ".";
17+
18+
//read the directory to get array of filenames
19+
const files = await fs.readdir(directory);
20+
21+
22+
23+
//print each file on its own line
24+
// Note: console.log(files) would print the entire array like: ['file1', 'file2']
25+
// Loop prints each individually on separate lines
26+
for (const file of files) {
27+
console.log(file)
28+
}

0 commit comments

Comments
 (0)