Skip to content

Commit 7fcbf79

Browse files
committed
fixed ls
1 parent e9f9571 commit 7fcbf79

File tree

1 file changed

+28
-11
lines changed
  • implement-shell-tools/ls

1 file changed

+28
-11
lines changed

implement-shell-tools/ls/ls.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ const programArgv = program.args;
2525
try {
2626
const stats = await fs.stat(pathToFile);
2727
if (stats.isFile()) {
28-
console.log("is file ? " + stats.isFile());
2928
await listFiles("file");
3029
} else if (stats.isDirectory()) {
31-
console.log("is directory? " + stats.isDirectory());
3230

3331
listFiles("directory");
3432
} else {
@@ -50,10 +48,10 @@ const programArgv = program.args;
5048
const flag_1 = (files) => {
5149
try {
5250
files.forEach(function (file) {
53-
console.log(file);
51+
console.log(file);
52+
5453
});
5554
} catch (err) {
56-
//is it goes to stderror
5755
console.error("Error reading directory:", err);
5856
}
5957
};
@@ -64,12 +62,12 @@ const flag_a = (files) => {
6462
files.unshift(".");
6563
return files;
6664
} catch (err) {
67-
//is it goes to stderror
6865
console.error("Error reading directory:", err);
6966
}
7067
};
7168

7269
async function listFiles(type) {
70+
let output = []
7371
try {
7472
let formattedPath = "";
7573
if (type == "directory") {
@@ -81,12 +79,31 @@ async function listFiles(type) {
8179
const files = await fs.readdir(formattedPath);
8280
const sortedOutput = files.sort((a, b) => a.localeCompare(b));
8381

84-
if (char["1"] && char["a"]) {
85-
flag_1(flag_a(sortedOutput));
86-
} else if (char["a"]) {
87-
console.log(flag_a(sortedOutput).join(" "));
88-
} else if (char["1"]) {
89-
flag_1(sortedOutput);
82+
// if (char["1"] && char["a"]) {
83+
// flag_1(flag_a(sortedOutput));
84+
// } else if (char["a"]) {
85+
// console.log(flag_a(sortedOutput).join(" "));
86+
// } else if (char["1"]) {
87+
// flag_1(sortedOutput);
88+
// }
89+
90+
if (char["a"]){
91+
output = flag_a(sortedOutput)
92+
} else {
93+
sortedOutput.forEach(function (file) {
94+
95+
if (file[0] != "."){
96+
output.push(file)
97+
}
98+
})
99+
100+
// output = sortedOutput
101+
}
102+
103+
if (char["1"]){
104+
flag_1(output)
105+
} else {
106+
console.log(output.join(" "));
90107
}
91108
} catch (err) {
92109
console.error("Error reading directory:", err);

0 commit comments

Comments
 (0)