Skip to content

Commit 93a4ead

Browse files
adjusted naming convention for ls
1 parent af61ea9 commit 93a4ead

2 files changed

Lines changed: 24 additions & 31 deletions

File tree

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ let lineNumber = 1;
1919
for (const path of paths) {
2020
const content = await fs.readFile(path, "utf-8");
2121

22-
// Split into lines. If the file ends with a newline, split() leaves an
23-
// extra empty string at the end - remove that so we don't print a
24-
// phantom blank line.
2522
const endsWithNewline = content.endsWith("\n");
2623
let lines = content.split("\n");
2724
if (endsWithNewline) {

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

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,43 @@
1+
import { program } from "commander";
12
import { promises as fs } from "node:fs";
2-
import process from "node:process";
33

4-
const argv = process.argv.slice(2);
4+
program
5+
.name("check-for-ls")
6+
.description("Implement my own version of ls")
7+
.argument("[paths...]", "The file paths to process")
8+
.option("-1, --one", "This lists one file per line")
9+
.option("-a", "This shows all files");
510

6-
const flags = [];
7-
const paths = [];
11+
program.parse();
812

9-
for (const arg of argv) {
10-
if (arg.startsWith("-")) {
11-
flags.push(arg);
12-
} else {
13-
paths.push(arg);
14-
}
15-
}
13+
const showOneLine = program.opts().one;
14+
const showAllFiles = program.opts().a;
1615

17-
const showOnePerLine = flags.includes("-1");
18-
const showHiddenFiles = flags.includes("-a");
16+
const paths = program.args;
1917

20-
let targets = paths;
21-
if (targets.length === 0) {
22-
targets = ["."];
18+
let filePath = paths;
19+
if (filePath.length === 0) {
20+
filePath = ["."];
2321
}
2422

25-
for (const target of targets) {
23+
for (const target of filePath) {
2624
const info = await fs.stat(target);
2725

28-
let namesToShow;
29-
26+
let showFiles;
3027
if (info.isDirectory()) {
31-
namesToShow = await fs.readdir(target);
28+
showFiles = await fs.readdir(target);
3229

33-
if (showHiddenFiles) {
34-
namesToShow = [".", "..", ...namesToShow];
30+
if (showAllFiles) {
31+
showFiles = [".", "..", ...showFiles];
3532
} else {
36-
namesToShow = namesToShow.filter((name) => !name.startsWith("."));
33+
showFiles = showFiles.filter((name) => !name.startsWith("."));
3734
}
38-
39-
namesToShow.sort();
35+
showFiles.sort();
4036
} else {
41-
namesToShow = [target];
37+
showFiles = [target];
4238
}
4339

44-
for (const name of namesToShow) {
45-
console.log(name);
40+
for (const file of showFiles) {
41+
console.log(file);
4642
}
4743
}

0 commit comments

Comments
 (0)