-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathmyCat.js
More file actions
40 lines (33 loc) · 926 Bytes
/
myCat.js
File metadata and controls
40 lines (33 loc) · 926 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { program } from "commander";
import { promises as fs } from "node:fs";
import process from "node:process";
program
.name("myCat")
.description("Simple file viewer")
.option("-n", "Number all output lines")
.option("-b", "Number non-blank output lines")
.argument("<path...>", "One or more file paths to show");
program.parse();
const files = program.args;
const opts = program.opts();
let lineNumber = 1;
for (const filename of files) {
const content = await fs.readFile(filename, "utf-8");
if (opts.n) {
const lines = content.split("\n");
for (const line of lines) {
console.log(lineNumber + " " + line);
lineNumber++;
}
} else if (opts.b) {
const lines = content.split("\n");
for (const line of lines) {
if (line.trim() !== "") {
console.log(lineNumber + " " + line);
lineNumber++;
}
}
} else {
console.log(content);
}
}