-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathwcFile.mjs
More file actions
55 lines (46 loc) · 1.72 KB
/
Copy pathwcFile.mjs
File metadata and controls
55 lines (46 loc) · 1.72 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
46
47
48
49
50
51
52
53
54
55
import process from "node:process";
import { promises as fs } from "node:fs";
const argv = process.argv.slice(2);
const dash = argv.filter(arg => arg.startsWith('-'));
const filePaths = argv.filter(arg => !arg.startsWith('-'));
const forLines = dash.includes('-l');
const forWords = dash.includes('-w');
const forBytes = dash.includes('-c');
let totalLines = 0;
let totalWords = 0;
let totalBytes = 0;
for (const filePath of filePaths) {
let content;
try {
content = await fs.readFile(filePath, 'utf8');
} catch {
console.error(`wc: ${filePath}: No file or directory exists`);
process.exit(1);
}
const lines = content.split('\n').length - 1;
const words = content.trim() === '' ? 0 : content.trim().split(/\s+/).length;
const bytes = Buffer.byteLength(content, 'utf8');
totalLines += lines;
totalWords += words;
totalBytes += bytes;
if (forLines) {
console.log(`${String(lines).padStart(8)} ${filePath}`);
} else if (forWords) {
console.log(`${String(words).padStart(8)} ${filePath}`);
} else if (forBytes) {
console.log(`${String(bytes).padStart(8)} ${filePath}`);
} else {
console.log(`${String(lines).padStart(8)} ${String(words).padStart(8)} ${String(bytes).padStart(8)} ${filePath}`);
}
}
if (filePaths.length > 1) {
if (forLines) {
console.log(`${String(totalLines).padStart(8)} total`);
} else if (forWords) {
console.log(`${String(totalWords).padStart(8)} total`);
} else if (forBytes) {
console.log(`${String(totalBytes).padStart(8)} total`);
} else {
console.log(`${String(totalLines).padStart(8)} ${String(totalWords).padStart(8)} ${String(totalBytes).padStart(8)} total`);
}
}