-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcat.js
More file actions
40 lines (35 loc) · 1020 Bytes
/
cat.js
File metadata and controls
40 lines (35 loc) · 1020 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
39
import process from "node:process";
import { promises as fs } from "node:fs";
const argv = process.argv.slice(2, process.argv.length);
const flags = [];
const paths = [];
for (let i = 0; i < argv.length; i++) {
if (argv[i][0] == "-") {
flags.push(argv[i]);
} else {
paths.push(argv[i]);
}
}
displayFiles(paths, flags);
async function displayFiles(paths, flags) {
let content = '';
for (let i = 0; i < paths.length; i++) {
content += await fs.readFile(paths[i], "utf-8");
}
const lines = content.split("\n");
if (lines[lines.length - 1] == '') {
lines.pop();
}
let lineNumber = 1;
for (let i = 0; i < lines.length; i++) {
let output = lines[i];
if (
(flags.includes("-n") && !flags.includes("-b")) ||
(flags.includes("-b") && lines[i] != "")
) {
output = " " + (lineNumber).toString() + " " + lines[i];
lineNumber++;
}
console.log(output);
}
}