Skip to content

Commit 7260c89

Browse files
committed
Implemented cat,ls, and wc commant line tool. And also created an empty txt file to check my output for wc.mjs
1 parent 4350f48 commit 7260c89

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import process from "node:process";
2+
import { promises as fs } from "node:fs";
3+
4+
let args = process.argv.slice(2);
5+
const flags = args.filter((arg) => arg.startsWith("-"));
6+
const filePaths = args.filter((args) => !args.startsWith("-"));
7+
let lineNumber = 1;
8+
9+
for (const filePath of filePaths) {
10+
const content = await fs.readFile(filePath, "utf-8");
11+
12+
if (flags.length == 0) {
13+
process.stdout.write(content);
14+
continue;
15+
}
16+
17+
const lines = content.split("\n");
18+
19+
for (const line of lines) {
20+
if (flags.includes("-b")) {
21+
if (line === "") {
22+
console.log();
23+
} else {
24+
console.log(`${lineNumber}\t${line}`);
25+
lineNumber++;
26+
}
27+
} else if (flags.includes("-n")) {
28+
console.log(`${lineNumber}\t${line}`);
29+
lineNumber++;
30+
}
31+
}
32+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import process from "node:process";
2+
import { promises as fs } from "node:fs";
3+
4+
const args = process.argv.slice(2);
5+
const flags = args.filter((arg) => arg.startsWith("-"));
6+
const path = args.filter((arg) => !arg.startsWith("-"));
7+
let targetDir = path[0] || ".";
8+
let files = await fs.readdir(targetDir);
9+
10+
if (flags.includes("-a")) {
11+
files.unshift(".", "..");
12+
} else {
13+
files = files.filter((fileName) => !fileName.startsWith("."));
14+
}
15+
16+
if (flags.includes("-1")) {
17+
for (const file of files) {
18+
console.log(file);
19+
}
20+
} else {
21+
console.log(files.join(" "));
22+
}

‎implement-shell-tools/wc/sample-files/4.txt‎

Whitespace-only changes.

‎implement-shell-tools/wc/wc.mjs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import process from "node:process";
2+
import { promises as fs } from "node:fs";
3+
4+
let filePaths = [];
5+
let flag = null;
6+
let numberOfWords;
7+
8+
if (process.argv[2].startsWith("-")) {
9+
flag = process.argv[2];
10+
filePaths = process.argv.slice(3);
11+
} else {
12+
filePaths = process.argv.slice(2);
13+
}
14+
for (const filePath of filePaths) {
15+
const content = await fs.readFile(filePath, "utf-8");
16+
17+
if (flag === "-w") {
18+
console.log(getWordCount(content));
19+
} else if (flag === "-l") {
20+
console.log(getLineCount(content));
21+
} else if (flag === "-c") {
22+
console.log(getByteCount(content), filePaths);
23+
} else {
24+
console.log(
25+
getWordCount(content),
26+
getLineCount(content),
27+
getByteCount(content),
28+
filePaths,
29+
);
30+
}
31+
}
32+
33+
function getWordCount(text) {
34+
const trimmed = text.trim();
35+
if (trimmed.length == 0) {
36+
return 0;
37+
}
38+
return trimmed.split(/\s+/).length;
39+
}
40+
41+
function getLineCount(text) {
42+
if (text.length == 0) return 0;
43+
return text.split("\n").length - 1;
44+
}
45+
46+
function getByteCount(text) {
47+
return Buffer.byteLength(text, "utf-8");
48+
}

0 commit comments

Comments
 (0)