Skip to content

Commit 3c7c4f7

Browse files
committed
refactor: improve naming and simplify ls filtering logic
1 parent 0acf2ca commit 3c7c4f7

File tree

1 file changed

+15
-18
lines changed
  • implement-shell-tools/ls

1 file changed

+15
-18
lines changed

implement-shell-tools/ls/ls.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,30 @@ program
1010

1111
program.parse(process.argv);
1212

13-
// options and path from commander.
14-
const opts = program.opts();
13+
const options = program.opts();
1514

16-
let paths = program.args; // array of paths user typed
17-
if (paths.length === 0) {
18-
paths = ["."];
19-
}
15+
let paths = program.args;
16+
if (paths.length === 0) paths = ["."];
2017

2118
for (const directoryPath of paths) {
22-
let entries;
19+
let fileNames;
20+
2321
try {
24-
entries = await fs.readdir(directoryPath);
22+
fileNames = await fs.readdir(directoryPath);
2523
} catch (err) {
2624
console.error(`ls: cannot access '${directoryPath}': ${err.message}`);
27-
continue; // move to next path if this one fails
25+
continue;
2826
}
2927

30-
if (!opts.allFiles) {
31-
entries = entries.filter((name) => !name.startsWith("."));
32-
}
28+
fileNames = fileNames.filter(
29+
(file) => options.allFiles || !file.startsWith("."),
30+
);
3331

34-
if (opts.onePerLine) {
35-
entries.forEach((name) => {
36-
console.log(name);
37-
});
32+
if (options.onePerLine) {
33+
for (const file of fileNames) {
34+
console.log(file);
35+
}
3836
} else {
39-
//print files in one line if -1 is not used
40-
console.log(entries.join(" "));
37+
console.log(fileNames.join(" "));
4138
}
4239
}

0 commit comments

Comments
 (0)