-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathcat.js
More file actions
39 lines (35 loc) · 970 Bytes
/
cat.js
File metadata and controls
39 lines (35 loc) · 970 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 { promises as fs } from "node:fs";
import { glob } from "glob";
async function readFile() {
let fileNamePattern;
let flag;
if (process.argv[2].startsWith("-")) {
flag = process.argv[2];
fileNamePattern = process.argv[3];
} else {
fileNamePattern = process.argv[2];
}
const filesNameArray = await glob(fileNamePattern);
filesNameArray.sort();
let lineNumber = 1;
for (const file of filesNameArray) {
const fileContent = await fs.readFile(file, "utf-8");
if (flag == null) {
console.log(fileContent);
} else {
const linesOfText = fileContent.split(/\r?\n/);
if (linesOfText[linesOfText.length - 1].trim() === "") {
linesOfText.pop();
}
for (const line of linesOfText) {
if (line.trim() === "" && flag === "-b") {
console.log(line);
} else {
console.log(`${lineNumber} ${line}`);
lineNumber++;
}
}
}
}
}
readFile();