Skip to content

Commit 86cfa5a

Browse files
committed
ls command
1 parent b5dedf5 commit 86cfa5a

File tree

1 file changed

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

1 file changed

+27
-0
lines changed

implement-shell-tools/ls/ls.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import process from "node:process";
2+
import { promises as fs } from "node:fs";
3+
4+
const argv = process.argv.slice(2, process.argv.length);
5+
const currentDir = './';
6+
const flags = [];
7+
let path = '';
8+
for (let i = 0; i < argv.length; i++) {
9+
if (argv[i][0] == "-") {
10+
flags.push(argv[i]);
11+
} else {
12+
path = argv[i];
13+
}
14+
}
15+
if (path == '') path = currentDir;
16+
17+
const content = await fs.readdir(path);
18+
19+
if (flags.includes("-l")) {
20+
for (let i = 0; i < content.length; i++) {
21+
let line = content[i];
22+
if (!flags.includes("-a") && line[0] == ".") continue;
23+
console.log(line);
24+
}
25+
} else {
26+
console.log(content.join(" "));
27+
}

0 commit comments

Comments
 (0)