-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcat.js
More file actions
45 lines (38 loc) · 1.23 KB
/
cat.js
File metadata and controls
45 lines (38 loc) · 1.23 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
import { promises as fs } from "node:fs";
// get all command line arguments after "node cat.js" - like -n or -b
const args = process.argv.slice(2);
// put them in variables
const showAllNumbers = args.includes("-n");
const showNonEmptyNumbers = args.includes("-b");
// getting the paths of files
const paths = args.filter(arg => arg !== "-n" && arg !== "-b");
// loop over each file
for (const path of paths) {
try {
// read file as text
const content = await fs.readFile(path, "utf-8");
// split them into lines
const lines = content.split("\n");
let lineNumber = 1; // tracks line numbers for -b and -n
lines.forEach((line) => {
if (showNonEmptyNumbers) {
// -b: number only non-empty lines
if (line.trim() !== "") {
console.log(`${lineNumber} ${line}`);
lineNumber++;
} else {
console.log(line); // shows empty line with no number
}
} else if (showAllNumbers) {
// -n: number all lines
console.log(`${lineNumber} ${line}`);
lineNumber++;
} else {
// no flags: just print line
console.log(line);
}
});
} catch (err) {
console.error(`Error reading file "${path}": ${err.message}`);
}
}