Skip to content

Commit f77bcc8

Browse files
committed
partially solved wc.js
1 parent 3f563b0 commit f77bcc8

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

implement-shell-tools/package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

implement-shell-tools/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"dependencies": {
3+
"commander": "^14.0.3"
4+
},
5+
"type": "module"
6+
}

implement-shell-tools/wc/wc.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { promises as fs } from "node:fs";
2+
import { program } from "commander";
3+
import { get } from "node:http";
4+
5+
program
6+
.name("print newline, word, and byte counts for each file")
7+
.option("-a", "to do something")
8+
.argument("<paths...>", "file name");
9+
10+
program.parse();
11+
12+
const paths = program.args;
13+
14+
async function getNoFlagsOutput(paths) {
15+
let lineCountTotal = 0;
16+
let wordCountTotal = 0;
17+
let fileSizeTotal = 0;
18+
let output = [];
19+
20+
for (const filename of paths) {
21+
try {
22+
const file = await fs.stat(filename);
23+
24+
if (file.isFile()) {
25+
const fileContent = await fs.readFile(filename, "utf-8");
26+
const lineCount = fileContent.match(/\n/g).length;
27+
const wordCount = fileContent
28+
.trim()
29+
.split(/\s+/)
30+
.filter(Boolean).length;
31+
const fileSize = file.size;
32+
33+
output.push([lineCount, wordCount, fileSize, filename]);
34+
}
35+
} catch (err) {
36+
output.push([filename, `wc: ${filename} ${err.message}`]);
37+
}
38+
}
39+
40+
return output;
41+
}
42+
//console.log(`${lineCountTotal} ${wordCountTotal} ${fileSizeTotal}Total`);
43+
console.log(JSON.stringify(await getNoFlagsOutput(paths)));

0 commit comments

Comments
 (0)