Skip to content

Commit c61cbdf

Browse files
committed
fixed ls
1 parent fabd47a commit c61cbdf

File tree

1 file changed

+29
-44
lines changed
  • implement-shell-tools/ls

1 file changed

+29
-44
lines changed

implement-shell-tools/ls/ls.js

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const programArgv = program.args;
2727
if (stats.isFile()) {
2828
await listFiles("file");
2929
} else if (stats.isDirectory()) {
30-
3130
listFiles("directory");
3231
} else {
3332
console.error("Not a file or directory.");
@@ -46,56 +45,42 @@ const programArgv = program.args;
4645
})();
4746

4847
const flag_1 = (files) => {
49-
try {
50-
files.forEach(function (file) {
51-
console.log(file);
52-
53-
});
54-
} catch (err) {
55-
console.error("Error reading directory:", err);
56-
}
48+
files.forEach(function (file) {
49+
console.log(file);
50+
});
5751
};
5852

5953
const flag_a = (files) => {
60-
try {
61-
files.unshift("..");
62-
files.unshift(".");
63-
return files;
64-
} catch (err) {
65-
console.error("Error reading directory:", err);
66-
}
54+
files.unshift("..");
55+
files.unshift(".");
56+
return files;
6757
};
6858

6959
async function listFiles(type) {
70-
let output = []
71-
try {
72-
let formattedPath = "";
73-
if (type == "directory") {
74-
formattedPath = pathToFile;
75-
} else if (type == "file") {
76-
formattedPath = path.dirname(pathToFile);
77-
}
78-
const char = program.opts();
79-
const files = await fs.readdir(formattedPath);
80-
const sortedOutput = files.sort((a, b) => a.localeCompare(b));
81-
82-
if (char["a"]){
83-
output = flag_a(sortedOutput)
84-
} else {
85-
sortedOutput.forEach(function (file) {
60+
let output = [];
61+
let formattedPath = "";
62+
if (type == "directory") {
63+
formattedPath = pathToFile;
64+
} else if (type == "file") {
65+
formattedPath = path.dirname(pathToFile);
66+
}
67+
const char = program.opts();
68+
const files = await fs.readdir(formattedPath);
69+
const sortedOutput = files.sort((a, b) => a.localeCompare(b));
8670

87-
if (file[0] != "."){
88-
output.push(file)
89-
}
90-
})
91-
}
71+
if (char["a"]) {
72+
output = flag_a(sortedOutput);
73+
} else {
74+
sortedOutput.forEach(function (file) {
75+
if (file[0] != ".") {
76+
output.push(file);
77+
}
78+
});
79+
}
9280

93-
if (char["1"]){
94-
flag_1(output)
95-
} else {
96-
console.log(output.join(" "));
97-
}
98-
} catch (err) {
99-
console.error("Error reading directory:", err);
81+
if (char["1"]) {
82+
flag_1(output);
83+
} else {
84+
console.log(output.join(" "));
10085
}
10186
}

0 commit comments

Comments
 (0)