Skip to content

Commit de8dd1a

Browse files
committed
wc accounts for invalid file/folder paths
1 parent c8e7c29 commit de8dd1a

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,21 @@ if (Object.keys(options).length === 0) {
2727
// incrementally updated in the loop below
2828
const totals = { l: 0, w: 0, c: 0 };
2929

30+
// wc actually prints total if there is more than one argument, not more than one file
31+
// e.g. wc invalid.txt valid.txt
32+
// there will be a total line with the same stats are the valid.txt stats
3033
let fileCount = 0;
34+
3135
for (const path of paths) {
36+
//
37+
try {
38+
const validPath = fs.statSync(path).isDirectory();
39+
} catch (err) {
40+
console.error(`wc: ${path} open: No such file or directory`);
41+
fileCount++;
42+
continue;
43+
}
44+
3245
if (fs.statSync(path).isDirectory()) {
3346
console.eror(`wc: ${path}: read: Is a directory`);
3447
} else {

0 commit comments

Comments
 (0)