-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathcat.js
More file actions
65 lines (58 loc) · 2.2 KB
/
cat.js
File metadata and controls
65 lines (58 loc) · 2.2 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
65
import { program } from "commander";
import { promises as fs } from "node:fs";
import process from "node:process";
// configure the CLI program with its name, description, arguments, options, and actions (the help instructions)
program
.name("cat")
.description("An alternative to the 'cat' command")
.argument("<files...>", "The file(s) to process")
.option("-n, --number", "Number all output lines")
.option("-b, --number-nonblank", "Number non-blank output lines")
.action(async (files, options) => {
try {
await newCat(files, options);
} catch (err) {
console.error(`Error: ${err.message}`);
}
});
program.parse(process.argv);
//helper function to format output
function printLine(line, lineNumber, padWidth) {
if (lineNumber !== null) {
console.log(`${lineNumber.toString().padStart(padWidth, ' ')} ${line}`);
} else {
console.log(line);
}
}
async function newCat(files, options) {
let lineNumber = 1;
const padWidth = 6;
for (const file of files) {
// read each file into a single text string
try {
const data = await fs.readFile(file, "utf8");
// split that string into an array at \n where each element is a line from the file
// e.g. lines = ["Line 1", "Line 2", "Line 3"]
const lines = data.split("\n")
// remove trailing blank line caused by a trailing newline
if (lines[lines.length - 1] === "") {
lines.pop();
}
lines.forEach(line => {
//line trim: truthy = text, falsy = blank
if (options.numberNonblank && line.trim()) {
// number non-blank lines only
printLine(line, lineNumber++, padWidth);
} else if (options.number){
// number all lines
printLine(line, lineNumber++, padWidth);
} else {
// neither flag, print normally
printLine(line, null, padWidth)
}
});
} catch (err) {
console.error(`Error reading file ${file}: ${err.message}`);
}
}
}