Skip to content

Commit 0edb723

Browse files
committed
basic cat implemention
1 parent dd574ac commit 0edb723

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

implement-shell-tools/cat/cat.mjs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import { program } from "commander";
2+
import { error } from "node:console";
23
import { promises as fs } from "node:fs";
34

45
program
5-
.name("implement my own version of `cat`.")
6-
.description("I do not know what I'm doing")
6+
.name("cat")
7+
.description("cat sample-files/1.txt ")
78
.argument("<path>", "The file path to process");
89
program.parse();
910

1011
const argv = program.args;
1112
if (argv.length != 1) {
12-
console.error(`Expected exactly 1 argument (a path) to be passed but got ${argv.length}`);
13-
process.exit(1);
13+
console.error(
14+
`Expected exactly 1 argument (a path) to be passed but got ${argv.length}`,
15+
);
16+
process.exit(1);
1417
}
1518
const path = argv[0];
16-
17-
const content = await fs.readFile(path, "utf-8");
18-
console.log(content);
19+
try {
20+
const content = await fs.readFile(path, "utf-8");
21+
process.stdout.write(content);
22+
} catch (error) {
23+
console.error(error.message);
24+
process.exit(1);
25+
}

0 commit comments

Comments
 (0)