-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcat.js
More file actions
44 lines (36 loc) · 1.08 KB
/
Copy pathcat.js
File metadata and controls
44 lines (36 loc) · 1.08 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
import process from "node:process";
import { promises as fs } from "node:fs";
const argv = process.argv.slice(2);
const showNumbers = argv.includes("-n");
const showNonBlankNumbers = argv.includes("-b");
const filePaths = argv.filter((arg) => !arg.startsWith("-"));
const flagsUsed = argv.filter((arg) => arg.startsWith("-"));
const supportedFlags = ["-n", "-b"];
for (const flag of flagsUsed) {
if (!supportedFlags.includes(flag)) {
console.error(`Invalid option: please try "-n or "-b"`);
process.exit(1);
}
}
let counterLines = 1;
for (const path of filePaths) {
try {
const content = await fs.readFile(path, "utf-8");
.
const splitLines = content.split("\n");
splitLines.forEach((line) => {
let prefix = "";
if (showNonBlankNumbers) {
if (line.trim() !== "") {
prefix = `${counterLines++} `;
}
} else if (showNumbers) {
prefix = `${counterLines++} `;
}
console.log(`${prefix}${line}`);
});
} catch (error) {
console.error(`Error ${path}: ${error.message}`);
process.exit(1);
}
}