-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathcat.js
More file actions
64 lines (51 loc) · 1.55 KB
/
cat.js
File metadata and controls
64 lines (51 loc) · 1.55 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
63
64
import { program } from "commander";
import { promises as fs } from "node:fs";
program
.name("cat")
.description("concatenate and print files")
.option("-n, --number", "number all output lines")
.option("-b, --number-nonblank", "number nonempty output lines")
.argument("<paths...>", "the file paths to process");
program.parse();
const options = program.opts();
const paths = program.args;
const numberAll = options.number;
const numberNonBlank = options.numberNonblank;
const shouldNumberAll = numberNonBlank ? false : numberAll;
let lineNumber = 1;
let nonBlankNumber = 1;
for (const filePath of paths) {
try {
const content = await fs.readFile(filePath, "utf8");
process.stdout.write(formatContent(content));
} catch (error) {
process.exitCode = 1;
process.stderr.write(`cat: ${filePath}: ${error.message}\n`);
}
}
function formatContent(text) {
const normalized = text.replace(/\r\n/g, "\n");
const endsWithNewline = normalized.endsWith("\n");
const lines = endsWithNewline
? normalized.slice(0, -1).split("\n")
: normalized.split("\n");
const output = [];
for (const line of lines) {
if (numberNonBlank) {
if (line === "") {
output.push("");
} else {
output.push(`${String(nonBlankNumber++).padStart(6, " ")}\t${line}`);
}
} else if (shouldNumberAll) {
output.push(`${String(lineNumber++).padStart(6, " ")}\t${line}`);
} else {
output.push(line);
}
}
let result = output.join("\n");
if (endsWithNewline) {
result += "\n";
}
return result;
}