forked from CodeYourFuture/Module-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.js
More file actions
54 lines (40 loc) · 908 Bytes
/
Copy pathcat.js
File metadata and controls
54 lines (40 loc) · 908 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import process from "node:process";
import { promises as fs } from "node:fs";
const args = process.argv.slice(2);
let option = "none";
const paths = [];
for (const arg of args) {
if (arg === "-n") {
option = "n";
} else if (arg === "-b") {
option = "b";
} else {
paths.push(arg);
}
}
if (paths.length === 0) {
console.error("Expected at least one file path.");
process.exit(1);
}
for (const path of paths) {
const content = await fs.readFile(path, "utf-8");
const lines = content.split("\n");
let lineNumber = 1;
for (const line of lines) {
if (option === "n") {
console.log(`${lineNumber} ${line}`);
lineNumber++;
}
else if (option === "b") {
if (line !== "") {
console.log(`${lineNumber} ${line}`);
lineNumber++;
} else {
console.log("");
}
}
else {
console.log(line);
}
}
}