Skip to content

Commit 37c7da2

Browse files
author
Craig D'Silva
committed
Format
1 parent a324eef commit 37c7da2

3 files changed

Lines changed: 78 additions & 51 deletions

File tree

implement-shell-tools/cat/cat.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,35 @@ import { promises as fs } from "node:fs";
44

55
let showNumber = false;
66

7-
program.name("cat").description("Prints the output of a file to the console").option("-n, --number", "Displays the lines along with their number").argument("<path>", "The file path").allowExcessArguments()
8-
program.parse()
7+
program
8+
.name("cat")
9+
.description("Prints the output of a file to the console")
10+
.option("-n, --number", "Displays the lines along with their number")
11+
.argument("<path>", "The file path")
12+
.allowExcessArguments();
13+
program.parse();
914

10-
const argv = program.args
15+
const argv = program.args;
1116
if (argv.length < 1) {
12-
console.error(`Expected exactly 1 or more arguments (paths) to be passed but got ${argv.length}.`);
13-
process.exit(1);
17+
console.error(
18+
`Expected exactly 1 or more arguments (paths) to be passed but got ${argv.length}.`,
19+
);
20+
process.exit(1);
1421
}
1522

16-
showNumber = program.opts().number
23+
showNumber = program.opts().number;
1724

1825
const stringArr = [];
1926
for (const path of argv) {
20-
stringArr.push(await fs.readFile(path, "utf-8"));
27+
stringArr.push(await fs.readFile(path, "utf-8"));
2128
}
2229

23-
const flatArr = stringArr.flatMap(l => l.split("\n").map((l, i, a) => i < a.length - 1 ? l + "\n" : l).filter(l => l !== ""));
30+
const flatArr = stringArr.flatMap((l) =>
31+
l
32+
.split("\n")
33+
.map((l, i, a) => (i < a.length - 1 ? l + "\n" : l))
34+
.filter((l) => l !== ""),
35+
);
2436

25-
if (!showNumber) console.log(flatArr.join(""))
26-
else console.log(flatArr.map((l, i) => `${i + 1} ${l}`).join(""))
37+
if (!showNumber) console.log(flatArr.join(""));
38+
else console.log(flatArr.map((l, i) => `${i + 1} ${l}`).join(""));

implement-shell-tools/ls/ls.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { program } from "commander";
2-
import process from 'node:process';
3-
import fs from 'node:fs';
2+
import process from "node:process";
3+
import fs from "node:fs";
44

55
let vertical = false;
66
let showHidden = false;
77

8-
program.name("list-directory-contents").description("Shows all files and folders in a directory").option("-1", "List one file/directory per line").option("-a, --all", "Show hidden files").argument("[path]", "Path of the directory to list (defaults to .)")
8+
program
9+
.name("list-directory-contents")
10+
.description("Shows all files and folders in a directory")
11+
.option("-1", "List one file/directory per line")
12+
.option("-a, --all", "Show hidden files")
13+
.argument("[path]", "Path of the directory to list (defaults to .)");
914

1015
program.parse();
1116

@@ -18,14 +23,14 @@ const folderPath = argv[0] || ".";
1823

1924
const contents = fs.readdirSync(folderPath);
2025

21-
const filtered = contents.filter(f => f[0] !== ".")
26+
const filtered = contents.filter((f) => f[0] !== ".");
2227

23-
const dirArr = !showHidden ? filtered : contents
28+
const dirArr = !showHidden ? filtered : contents;
2429

2530
if (!vertical) {
26-
console.log(dirArr.join(" "));
31+
console.log(dirArr.join(" "));
2732
} else {
28-
for (const content of dirArr) {
29-
console.log(content);
30-
}
31-
}
33+
for (const content of dirArr) {
34+
console.log(content);
35+
}
36+
}

implement-shell-tools/wc/wc.js

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@ let showLineCount = true;
66
let showWordCount = true;
77
let showByteCount = true;
88

9-
program.name("wc").description("Print the number of lines, word and bytes for each file and a total if there are multiple files")
10-
.option("-l, --lines", "Print the number of lines")
11-
.option("-w, --words", "Print the number of words")
12-
.option("-c, --bytes", "Print the number of bytes")
13-
.argument("<path>", "The file path").allowExcessArguments()
14-
program.parse()
9+
program
10+
.name("wc")
11+
.description(
12+
"Print the number of lines, word and bytes for each file and a total if there are multiple files",
13+
)
14+
.option("-l, --lines", "Print the number of lines")
15+
.option("-w, --words", "Print the number of words")
16+
.option("-c, --bytes", "Print the number of bytes")
17+
.argument("<path>", "The file path")
18+
.allowExcessArguments();
19+
program.parse();
1520

1621
const argv = program.args;
1722

1823
if (Object.keys(program.opts()).length >= 1) {
19-
showLineCount = program.opts().lines;
20-
showWordCount = program.opts().words;
21-
showByteCount = program.opts().bytes;
24+
showLineCount = program.opts().lines;
25+
showWordCount = program.opts().words;
26+
showByteCount = program.opts().bytes;
2227
}
2328

2429
const stringArr = [];
2530
for (const path of argv) {
26-
stringArr.push(await fs.readFile(path, "utf-8"));
31+
stringArr.push(await fs.readFile(path, "utf-8"));
2732
}
2833

2934
let lines = 0;
@@ -32,31 +37,36 @@ let bytes = 0;
3237

3338
const infoArr = [];
3439
for (let i = 0; i < stringArr.length; i++) {
35-
const arr = [];
36-
const line = stringArr[i].split("\n").length
37-
const wc = stringArr[i].split(" ").flatMap(l => l.split("\n").map((l, i, a) => i < a.length - 1 ? l + "\n" : l).filter(l => l.trim() !== "")).length
38-
const lineByte = new Blob([stringArr[i]]).size
39-
if (showLineCount) {
40-
arr.push(line - 1)
40+
const arr = [];
41+
const line = stringArr[i].split("\n").length;
42+
const wc = stringArr[i].split(" ").flatMap((l) =>
43+
l
44+
.split("\n")
45+
.map((l, i, a) => (i < a.length - 1 ? l + "\n" : l))
46+
.filter((l) => l.trim() !== ""),
47+
).length;
48+
const lineByte = new Blob([stringArr[i]]).size;
49+
if (showLineCount) {
50+
arr.push(line - 1);
4151
lines += line - 1;
42-
}
43-
if (showWordCount) {
52+
}
53+
if (showWordCount) {
4454
arr.push(wc);
4555
words += wc;
46-
}
47-
if (showByteCount) {
48-
arr.push(lineByte)
49-
bytes += lineByte
50-
}
51-
arr.push(argv[i])
52-
infoArr.push(arr.join(" "))
56+
}
57+
if (showByteCount) {
58+
arr.push(lineByte);
59+
bytes += lineByte;
60+
}
61+
arr.push(argv[i]);
62+
infoArr.push(arr.join(" "));
5363
}
5464

5565
if (infoArr.length > 1) {
56-
const totalArr = ["total"];
57-
if (showByteCount) totalArr.unshift(bytes);
58-
if (showWordCount) totalArr.unshift(words);
59-
if (showLineCount) totalArr.unshift(lines);
60-
infoArr.push(totalArr.join(" "))
66+
const totalArr = ["total"];
67+
if (showByteCount) totalArr.unshift(bytes);
68+
if (showWordCount) totalArr.unshift(words);
69+
if (showLineCount) totalArr.unshift(lines);
70+
infoArr.push(totalArr.join(" "));
6171
}
62-
console.log(infoArr.join("\n"))
72+
console.log(infoArr.join("\n"));

0 commit comments

Comments
 (0)