-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcat.js
More file actions
38 lines (29 loc) · 942 Bytes
/
Copy pathcat.js
File metadata and controls
38 lines (29 loc) · 942 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
import { program } from "commander";
import * as fs from "node:fs/promises";
program
.name("cat")
.description("Reads file(s) and writes them to the standard output")
.argument("<paths...>", "The file path(s) to process")
.option("-n", "Number the output lines, starting at 1.")
.option("-b", "Number only non-blank output lines, starting at 1.");
program.parse();
try {
const filePaths = program.args;
const options = program.opts();
for (const filePath of filePaths) {
const file = await fs.open(filePath);
let lineNum = 1;
try {
for await (const line of file.readLines()) {
const isBlank = line.trim() === "";
const shouldNumber = options.n || (options.b && !isBlank);
console.log(shouldNumber ? `${lineNum} ${line}` : line);
if (shouldNumber) lineNum++;
}
} finally {
await file.close();
}
}
} catch (err) {
console.error(err.message);
}