-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathcat.js
More file actions
50 lines (40 loc) · 1.56 KB
/
cat.js
File metadata and controls
50 lines (40 loc) · 1.56 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
import { program } from "commander";
import { promises as fs } from "node:fs";
//here we Configure the program: what flags and arguments it accepts
// .option("short, long", "description")
//The < > brackets mean "required"
program
.name("cat")
.description("concatenate and print files")
.option("-n, --number", "Number all output lines")
.option("-b, --numberNonBlank", "Number non-blank output lines")
.argument("<path...>", "The file paths to process");
// here Parse command line arguments (reads process.argv and interprets it)
program.parse();
// Shared counter across all files for -n and -b flags
let lineNumber = 0;
// Process each file
for (const path of program.args) {
const hasNumberFlag = program.opts().number; // True if user used -n flag
const shouldNumberNonBlank = program.opts().numberNonBlank;
//read the file for each argument
const content = await fs.readFile(path, "utf-8");
const lines = content.split("\n"); // Split into array of lines
// Output with or without line numbers
if (hasNumberFlag) {
// Add line numbers to each line
const numberedLines = lines.map((line) => {
lineNumber = lineNumber + 1;
return `${lineNumber} ${line}`; // Format: " 1 Hello"
});
console.log(numberedLines.join("\n")); // Join back with newlines
} else if (shouldNumberNonBlank) {
const numberedLines = lines.map((line) => {
return line.trim() === "" ? line : `${++lineNumber} ${line}`;
});
console.log(numberedLines.join("\n"));
} else {
// Just print the file as-is
console.log(content);
}
}