-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathcat.js
More file actions
53 lines (35 loc) · 1.06 KB
/
cat.js
File metadata and controls
53 lines (35 loc) · 1.06 KB
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
41
42
43
44
import process from "node:process";
import {promises as fs} from "node:fs";
import {program} from "commander";
program
.name("display-file-content")
.description("Output the content of a file to the terminal")
.argument("<path...>", "The file path to process")
.option("-n", "Number the output lines")
.option("-b","Number the non-blank output lines")
program.parse();
const paths = program.args;
const options = program.opts();
let lineNumber = 1;
for (const path of paths) {
const filesContent = await fs.readFile(path, "utf-8");
const lines = filesContent.split("\n");
if (lines[lines.length - 1] === "") {
lines.pop();
}
for (let line of lines) {
if (options.n) {
process.stdout.write(`${lineNumber} ${line}\n`);
lineNumber++;
} else if (options.b) {
if (line != "") {
process.stdout.write(`${lineNumber} ${line}\n`);
lineNumber++;
} else {
process.stdout.write("\n");
}
} else {
process.stdout.write(line + "\n");
}
}
}