Skip to content

Commit e366e20

Browse files
committed
refactored ls implementation in a single file
1 parent 1ef8fc7 commit e366e20

7 files changed

Lines changed: 40 additions & 38 deletions

File tree

‎implement-shell-tools/ls/ls-1-a.js‎

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

‎implement-shell-tools/ls/ls-1-sample-files.js‎

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

‎implement-shell-tools/ls/ls-1.js‎

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

‎implement-shell-tools/ls/ls-sample-1txt.js‎

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

‎implement-shell-tools/ls/ls-sample-files*.js‎

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

‎implement-shell-tools/ls/ls-sample-files.js‎

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

‎implement-shell-tools/ls/ls.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const fs = require("fs");
2+
3+
const args = process.argv.slice(2);
4+
5+
let flags = [];
6+
let paths = [];
7+
8+
// Check arguments: separate flags and paths
9+
args.forEach((arg) => {
10+
if (arg.startsWith("-")) {
11+
flags.push(arg);
12+
} else {
13+
paths.push(arg);
14+
}
15+
});
16+
17+
// If no path is provided, use the current directory
18+
if (paths.length === 0) {
19+
paths = ["."];
20+
}
21+
22+
paths.forEach((path) => {
23+
if (fs.statSync(path).isFile()) {
24+
console.log(path);
25+
} else {
26+
let contents = fs.readdirSync(path);
27+
28+
// Hide hidden files unless -a exists
29+
if (!flags.includes("-a")) {
30+
contents = contents.filter((file) => !file.startsWith("."));
31+
}
32+
33+
// Check -1 flag
34+
if (flags.includes("-1")) {
35+
console.log(contents.join("\n"));
36+
} else {
37+
console.log(contents.join(" "));
38+
}
39+
}
40+
});

0 commit comments

Comments
 (0)