Skip to content

Commit 57a3c2a

Browse files
added code for ls exercise
1 parent e328bd5 commit 57a3c2a

File tree

1 file changed

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

1 file changed

+24
-0
lines changed

implement-shell-tools/ls/ls.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { program } from "commander"
2+
import process from "node:process"
3+
import { promises as fs } from "node:fs"
4+
import { readdir } from 'node:fs/promises'
5+
6+
program
7+
.name("ls")
8+
.description("Lists the files in a directory")
9+
.option("-1, --one", "One per line")
10+
.option("-a", "Include files starting with dot")
11+
.argument("filepath")
12+
program.parse(process.argv)
13+
14+
const argv = program.args
15+
const opts = program.opts()
16+
17+
if (argv.length != 1){
18+
console.error("Expected 1 argument")
19+
process.exit(1)
20+
}
21+
22+
const content = await fs.readdir(argv[0])
23+
24+
console.log(content.join(opts.one ? "\n": " "))

0 commit comments

Comments
 (0)