Skip to content

Commit 268ea98

Browse files
author
Fatma Degirmenci
committed
cat implementation
1 parent 407b010 commit 268ea98

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

implement-shell-tools/cat/cat.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { program } from "commander";
2+
import { promises as fs } from "node:fs";
3+
4+
program
5+
.name("cat command")
6+
.description("create cat command")
7+
.option("-n", "Number all lines")
8+
.option("-b", "Number non-empty lines")
9+
.argument("<paths...>", "File paths");
10+
11+
program.parse();
12+
const paths = program.args;
13+
const number = program.opts().n;
14+
const nonEmptyLine = program.opts().b;
15+
16+
let lineNumber = 1;
17+
18+
for (const path of paths) {
19+
try {
20+
const read = await fs.readFile(path, "utf-8");
21+
const lines = read.split("\n");
22+
for (let i of lines) {
23+
if (number) {
24+
console.log(`${String(lineNumber).padStart(6, " ")} ${i}`);
25+
lineNumber++;
26+
} else if (nonEmptyLine) {
27+
if (i.trim() !== "") {
28+
console.log(`${String(lineNumber).padStart(6, " ")} ${i}`);
29+
lineNumber++;
30+
} else {
31+
console.log(i);
32+
}
33+
} else {
34+
console.log(i);
35+
}
36+
}
37+
} catch (err) {
38+
console.error(`File could not read: ${path}`);
39+
}
40+
}

implement-shell-tools/cat/package-lock.json

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "cat",
3+
"version": "1.0.0",
4+
"description": "You should already be familiar with the `cat` command line tool.",
5+
"main": "cat.js",
6+
"type": "module",
7+
"dependencies": {
8+
"commander": "^14.0.2",
9+
"glob": "^13.0.0",
10+
"lru-cache": "^11.2.2",
11+
"minimatch": "^10.1.1",
12+
"minipass": "^7.1.2",
13+
"path-scurry": "^2.0.1"
14+
},
15+
"scripts": {
16+
"test": "echo \"Error: no test specified\" && exit 1"
17+
},
18+
"keywords": [],
19+
"author": "",
20+
"license": "ISC"
21+
}

0 commit comments

Comments
 (0)