Skip to content

Commit 20f8448

Browse files
committed
Implemented node that act the same as script shelling ls
1 parent 820efc8 commit 20f8448

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

implement-shell-tools/ls/ls.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { program } from 'commander'
2+
import process from 'process'
3+
import fs from 'fs'
4+
program
5+
.command('ls')
6+
.description('Output available files on directory')
7+
.option('-a, --all', 'Output all files')
8+
.option('-1, --line', 'List one file per line')
9+
.action((options) => {
10+
const contents = fs.readdirSync(process.cwd(), { withFileTypes: true })
11+
12+
if (options.all) {
13+
contents.forEach((content) => {
14+
console.log(content.name)
15+
})
16+
}
17+
if (options.line) {
18+
console.log(contents[0].name)
19+
}
20+
})
21+
22+
program.parse()

implement-shell-tools/ls/package-lock.json

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "ls",
3+
"version": "1.0.0",
4+
"description": "You should already be familiar with the `ls` command line tool.",
5+
"main": "ls.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"type": "module",
13+
"dependencies": {
14+
"commander": "^14.0.3"
15+
}
16+
}

0 commit comments

Comments
 (0)