Skip to content

Commit b37eb2f

Browse files
committed
implemented commander library to my cat, ls and wc file
1 parent 7260c89 commit b37eb2f

7 files changed

Lines changed: 166 additions & 57 deletions

File tree

.DS_Store

6 KB
Binary file not shown.

implement-shell-tools/.DS_Store

6 KB
Binary file not shown.

implement-shell-tools/cat/cat.mjs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,49 @@
11
import process from "node:process";
22
import { promises as fs } from "node:fs";
3+
import { program } from "commander";
4+
5+
program
6+
.name("Cat")
7+
.description("My version of cat command line tool")
8+
.argument("<files...>", "Files to display")
9+
.option("-n, --number", "Number all output lines")
10+
.option("-b, --non-blank", "Number non empty output lines");
11+
12+
program.parse();
13+
14+
const { number, nonBlank } = program.opts();
15+
const filePaths = program.args;
316

4-
let args = process.argv.slice(2);
5-
const flags = args.filter((arg) => arg.startsWith("-"));
6-
const filePaths = args.filter((args) => !args.startsWith("-"));
717
let lineNumber = 1;
818

919
for (const filePath of filePaths) {
10-
const content = await fs.readFile(filePath, "utf-8");
20+
try {
21+
const content = await fs.readFile(filePath, "utf-8");
1122

12-
if (flags.length == 0) {
13-
process.stdout.write(content);
14-
continue;
15-
}
16-
17-
const lines = content.split("\n");
23+
if (!number && !nonBlank) {
24+
process.stdout.write(content);
25+
continue;
26+
}
27+
let text = content;
28+
if (content.endsWith("\n")) {
29+
text = content.slice(0, -1);
30+
}
31+
const lines = text.split("\n");
1832

19-
for (const line of lines) {
20-
if (flags.includes("-b")) {
21-
if (line === "") {
22-
console.log();
23-
} else {
33+
for (const line of lines) {
34+
if (nonBlank) {
35+
if (line == "") {
36+
console.log();
37+
} else {
38+
console.log(`${lineNumber}\t${line}`);
39+
lineNumber++;
40+
}
41+
} else if (number) {
2442
console.log(`${lineNumber}\t${line}`);
2543
lineNumber++;
2644
}
27-
} else if (flags.includes("-n")) {
28-
console.log(`${lineNumber}\t${line}`);
29-
lineNumber++;
3045
}
46+
} catch (error) {
47+
console.error(`${error}`);
3148
}
3249
}

implement-shell-tools/ls/ls.mjs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
import process from "node:process";
22
import { promises as fs } from "node:fs";
3+
import { program } from "commander";
34

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);
5+
program
6+
.name("list")
7+
.description("Implement my version of ls")
8+
.argument("[paths...]", "The file path to process")
9+
.option("-1, --one", "This list the item one per line")
10+
.option("-a, --all", "This lists all of the files");
911

10-
if (flags.includes("-a")) {
11-
files.unshift(".", "..");
12-
} else {
13-
files = files.filter((fileName) => !fileName.startsWith("."));
14-
}
12+
program.parse();
13+
14+
const { one, all } = program.opts();
15+
const paths = program.args;
16+
17+
let targetDir = paths[0] || ".";
18+
19+
try {
20+
let files = await fs.readdir(targetDir);
21+
22+
if (all) {
23+
files = [".", "..", ...files].sort();
24+
} else {
25+
files = files.filter((output) => !output.startsWith(".")).sort();
26+
}
1527

16-
if (flags.includes("-1")) {
17-
for (const file of files) {
18-
console.log(file);
28+
if (one) {
29+
for (const file of files) {
30+
console.log(file);
31+
}
32+
} else {
33+
console.log(files.join(" "));
1934
}
20-
} else {
21-
console.log(files.join(" "));
35+
} catch (error) {
36+
console.error(`${error}`);
2237
}

implement-shell-tools/package-lock.json

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

implement-shell-tools/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "implement-shell-tools",
3+
"version": "1.0.0",
4+
"description": "Your task is to re-implement shell tools you have used.",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"commander": "^15.0.0"
15+
}
16+
}

implement-shell-tools/wc/wc.mjs

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,69 @@
11
import process from "node:process";
22
import { promises as fs } from "node:fs";
3+
import { program } from "commander";
34

4-
let filePaths = [];
5-
let flag = null;
6-
let numberOfWords;
5+
program
6+
.name("Word Count")
7+
.description("my implementation of wc")
8+
.argument("<path...>", "The file path to process")
9+
.option("-l", "Count for the total number of lines")
10+
.option("-c", "Count the total number of bytes")
11+
.option("-w", "Count the total number of words");
12+
13+
program.parse();
14+
15+
let filePaths = program.args;
16+
let options = program.opts();
17+
let noFlags = !options.l && !options.w && !options.c;
18+
19+
let totalLines = 0;
20+
let totalWords = 0;
21+
let totalBytes = 0;
722

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-
}
1423
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-
);
24+
try {
25+
const content = await fs.readFile(filePath, "utf-8");
26+
const outputs = [];
27+
28+
const lines = getLineCount(content);
29+
const words = getWordCount(content);
30+
const bytes = getByteCount(content);
31+
32+
totalLines += lines;
33+
totalWords += words;
34+
totalBytes += bytes;
35+
36+
if (noFlags || options.l) {
37+
outputs.push(lines);
38+
}
39+
if (noFlags || options.w) {
40+
outputs.push(words);
41+
}
42+
if (noFlags || options.c) {
43+
outputs.push(bytes);
44+
}
45+
46+
outputs.push(filePath);
47+
console.log(outputs.join("\t"));
48+
} catch (err) {
49+
console.error(`${err.message}`);
50+
}
51+
}
52+
53+
if (filePaths.length > 1) {
54+
const totalOutputs = [];
55+
56+
if (noFlags || options.l) {
57+
totalOutputs.push(totalLines);
58+
}
59+
if (noFlags || options.w) {
60+
totalOutputs.push(totalWords);
61+
}
62+
if (noFlags || options.c) {
63+
totalOutputs.push(totalBytes);
3064
}
65+
totalOutputs.push("total");
66+
console.log(totalOutputs.join("\t"));
3167
}
3268

3369
function getWordCount(text) {

0 commit comments

Comments
 (0)