Skip to content

Commit 07dd218

Browse files
author
Fatma Degirmenci
committed
implement ls command
1 parent 268ea98 commit 07dd218

File tree

5 files changed

+84
-139
lines changed

5 files changed

+84
-139
lines changed

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

Lines changed: 0 additions & 118 deletions
This file was deleted.

implement-shell-tools/cat/package.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

implement-shell-tools/ls/ls.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { program } from "commander";
2+
import { promises as fs } from "node:fs";
3+
4+
program
5+
.name("ls command")
6+
.description("create ls command")
7+
.option("-1", "One entry per line")
8+
.option("-a", "Show all files including hidden")
9+
.argument("[paths...]", "File paths");
10+
11+
program.parse();
12+
13+
const opts = program.opts();
14+
const paths = program.args.length > 0 ? program.args : ["."];
15+
const onePerLine = opts["1"];
16+
const showAll = opts.a;
17+
18+
for (const targetPath of paths) {
19+
try {
20+
let files = await fs.readdir(targetPath);
21+
22+
if (!showAll) {
23+
files = files.filter((f) => !f.startsWith("."));
24+
}
25+
26+
27+
if (paths.length > 1) {
28+
console.log(`${targetPath}:`);
29+
}
30+
31+
if (onePerLine) {
32+
for (const f of files) console.log(f);
33+
} else {
34+
console.log(files.join(" "));
35+
}
36+
37+
if (paths.length > 1) {
38+
console.log();
39+
}
40+
} catch (err) {
41+
console.error(`ls: cannot access '${targetPath}': ${err.message}`);
42+
}
43+
}

implement-shell-tools/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.

implement-shell-tools/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "implement-shell-tools",
3+
"version": "1.0.0",
4+
"description": "Your task is to re-implement shell tools you have used.",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"commander": "^14.0.2"
15+
}
16+
}

0 commit comments

Comments
 (0)