Skip to content

Commit 006540b

Browse files
committed
Implement ls tool examples
1 parent 5aebf47 commit 006540b

6 files changed

Lines changed: 38 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require("fs");
2+
const path = "./sample-files";
3+
const content = fs.readdirSync(path);
4+
console.log(".");
5+
console.log("..");
6+
7+
const files = content.map((item) => item);
8+
console.log(files.join("\n"));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fs = require("fs");
2+
const path = "./sample-files";
3+
const contents = fs.readdirSync(path).filter((file) => !file.startsWith("."));
4+
5+
console.log(contents.join("\n"));

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const fs = require("fs");
2+
const path = "../ls";
3+
const files = fs.readdirSync(path);
4+
console.log(files.join("\n"));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const fs = require("fs");
2+
const path = "./sample-files/1.txt";
3+
4+
console.log(path);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const fs = require("fs");
2+
const path = "./sample-files";
3+
const directories = fs.readdirSync(path);
4+
5+
directories.forEach((file) => {
6+
if (file === "dir") {
7+
console.log("dir:");
8+
console.log(fs.readdirSync("./sample-files/dir").join(" "));
9+
} else {
10+
console.log(file);
11+
}
12+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fs = require("fs");
2+
const folderPath = "./sample-files";
3+
const contents = fs.readdirSync(folderPath);
4+
5+
console.log(contents.join(" "));

0 commit comments

Comments
 (0)