Skip to content

Commit 8a546e1

Browse files
committed
add counters for total lines, words & characters for when the path is either a file or a dir
1 parent eb67a84 commit 8a546e1

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ program.parse();
1313

1414
const argv = program.args;
1515

16-
1716
const path = argv[0];
1817

1918
const options = program.opts();
2019

2120

22-
2321
if (argv.length < 1) {
2422
console.error("You must pass at least one path!");
2523
process.exit(1);
@@ -32,6 +30,11 @@ function counter(item) {
3230
return { lines, words, characters };
3331
}
3432

33+
let totalLines = 0;
34+
let totalWords = 0;
35+
let totalCharacters = 0;
36+
let fileCount = 0;
37+
3538
for (const path of argv) {
3639
const pathInfo = await stat(path);
3740

@@ -43,6 +46,12 @@ if (pathInfo.isFile()) {
4346
} else {
4447
console.log(`${stats.lines} ${stats.words} ${stats.characters} ${path}`);
4548
}
49+
50+
totalLines += stats.lines;
51+
totalWords += stats.words;
52+
totalCharacters += stats.characters;
53+
fileCount++;
54+
4655
} else if (pathInfo.isDirectory()) {
4756
const files = await fs.readdir(path);
4857
for (const file of files) {
@@ -55,6 +64,11 @@ if (pathInfo.isFile()) {
5564
} else {
5665
console.log(`${stats.lines} ${stats.words} ${stats.characters} ${filePath}`);
5766
}
67+
68+
totalLines += stats.lines;
69+
totalWords += stats.words;
70+
totalCharacters += stats.characters;
71+
fileCount++;
5872
}
5973
}
6074

0 commit comments

Comments
 (0)