-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcat.js
More file actions
62 lines (50 loc) · 1.49 KB
/
Copy pathcat.js
File metadata and controls
62 lines (50 loc) · 1.49 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { program } from "commander";
import { promises as fs } from "node:fs";
program
.name("node-cat")
.description("A Node.js implementation of the Unix cat command")
.option("-n, --number", "Number all output lines")
.option(
"-b, --numberNonBlank",
"Numbers only non-empty lines. Overrides -n option"
)
.argument("<path...>", "The file path to process");
program.parse();
const paths = program.args;
const { number, numberNonBlank } = program.opts();
// --- Read files ---
let content = "";
for (const path of paths) {
content += await fs.readFile(path, "utf-8");
}
// Remove the trailing newline
// I do realise that this is not exactly how cat works, but for the files that we have, we get a trailing new line and this makes the output look just as it would with the Unix cat command.
if (content.endsWith("\n")) {
content = content.slice(0, -1);
}
const contentLines = content.split("\n");
// --- Numbering functions ---
function numberAll(lines) {
return lines.map(
(line, index) => `${String(index + 1).padStart(6, " ")} ${line}`
);
}
function numberNonEmpty(lines) {
let lineCounter = 1;
return lines.map((line) =>
line.trim() === ""
? line
: `${String(lineCounter++).padStart(6, " ")} ${line}`
);
}
// --- Output logic ---
let output;
if (numberNonBlank) {
output = numberNonEmpty(contentLines);
} else if (number) {
output = numberAll(contentLines);
} else {
output = contentLines;
}
// --- Print output ---
console.log(output.join("\n"));