Skip to content

Commit 68c884e

Browse files
committed
cat_implementation
1 parent 896a7ff commit 68c884e

3 files changed

Lines changed: 65 additions & 0 deletions

File tree

implement-shell-tools/cat/cat.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {program} from "commander";
2+
import {promises as fs} from "node:fs";
3+
import process from "node:process";
4+
5+
program
6+
.name ("cat")
7+
.description ("A JS clone for unix command cat")
8+
.argument("<files...>","One or more files to reach and print")
9+
.option("-n", "number all output lines")
10+
.action(async (files,options)=> {
11+
for (const filePath of files){
12+
try {
13+
const content = await fs.readFile(filePath,"utf8");
14+
if (options.n)
15+
{
16+
const lines = content.trimEnd().split("\n");
17+
lines.forEach((line,index) => {
18+
console.log(`${index+1} \t ${line}`);
19+
});
20+
}
21+
else
22+
{
23+
process.stdout.write(content);
24+
}
25+
26+
27+
}
28+
catch(error)
29+
{
30+
console.error("No such file or directory");
31+
process.exit(1);
32+
}
33+
34+
}
35+
36+
});
37+
38+
program.parse(process.argv);

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

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type":"module" ,
3+
"dependencies": {
4+
"commander": "^15.0.0"
5+
}
6+
}

0 commit comments

Comments
 (0)