Skip to content

Commit 2bec857

Browse files
committed
fixed the errors.
1 parent e1d7c80 commit 2bec857

3 files changed

Lines changed: 50 additions & 38 deletions

File tree

implement-shell-tools/cat/cat.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
import process from "node:process";
22
import { promises as fs } from "node:fs";
33

4-
// THis will give an array without the path to node and to the file.
54
const argv = process.argv.slice(2);
65

7-
//Get line numbers.
86
const showNumbers = argv.includes("-n");
97
const showNonBlankNumbers = argv.includes("-b");
108

11-
//filter the - from the array argv as it's a flag.
129
const filePaths = argv.filter((arg) => !arg.startsWith("-"));
1310

1411
const flagsUsed = argv.filter((arg) => arg.startsWith("-"));
1512
const supportedFlags = ["-n", "-b"];
1613
for (const flag of flagsUsed) {
1714
if (!supportedFlags.includes(flag)) {
18-
console.error(`Invalid option try 'node cat.js --help' for more info.`);
15+
console.error(`Invalid option: please try "-n or "-b"`);
1916
process.exit(1);
2017
}
2118
}
@@ -25,9 +22,8 @@ let counterLines = 1;
2522
for (const path of filePaths) {
2623
try {
2724
const content = await fs.readFile(path, "utf-8");
28-
29-
//split the text at the new line character.
30-
const splitLines = content.split("\n");
25+
.
26+
const splitLines = content.split("\n");
3127

3228
splitLines.forEach((line) => {
3329
let prefix = "";
@@ -42,7 +38,7 @@ for (const path of filePaths) {
4238
console.log(`${prefix}${line}`);
4339
});
4440
} catch (error) {
45-
console.error(`Could not read: ${path}`);
41+
console.error(`Error ${path}: ${error.message}`);
4642
process.exit(1);
4743
}
4844
}

implement-shell-tools/ls/ls.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
import fs from "node:fs";
22
import process from "node:process";
33

4-
// This will give an array without the path to node and to the file.
54
const argv = process.argv.slice(2);
65

7-
// filter the flag to find the target folder.
86
const filePaths = argv.filter((arg) => !arg.startsWith("-"));
97
const showHiddenFiles = argv.includes("-a");
108

119
if (filePaths.length > 1) {
1210
console.error("Error: This version only supports one directory path a time.");
1311
process.exit(1);
1412
}
15-
// if no folder provide we use the current one
13+
1614
const target = filePaths[0] || ".";
17-
// read the file.
15+
1816
const files = fs.readdirSync(target);
1917

20-
// save the result into the variable.
2118
let filteredFIles = files;
2219
if (!showHiddenFiles) {
2320
filteredFIles = files.filter((file) => !file.startsWith("."));
2421
} else {
25-
// we use spread operator to merge the paths.
2622
filteredFIles = [".", "..", ...files];
2723
}
2824

29-
// Print using -1 .
3025
filteredFIles.forEach((file) => {
3126
console.log(file);
3227
});

implement-shell-tools/wc/wc.js

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ let showLines = argv.includes("-l");
88
let showBytes = argv.includes("-c");
99
let showCharacters = argv.includes("-m");
1010

11-
// filter flags, and getting the string of filename
1211
const filePaths = argv.filter((arg) => !arg.startsWith("-"));
1312
// if no flags enable all.
1413
if (!showLines && !showCharacters && !showWords && !showBytes) {
@@ -17,30 +16,52 @@ if (!showLines && !showCharacters && !showWords && !showBytes) {
1716
showWords = true;
1817
showBytes = true;
1918
}
20-
// fix bug .length ===0 will be true if nothing provided, instead !filePath will return empty array which result true.
19+
2120
if (filePaths.length === 0) {
2221
console.error("PLease provide a file path");
2322
process.exit(1);
2423
}
25-
// loop trough the array to get each file path.
24+
25+
let totalLines = 0;
26+
let totalWords = 0;
27+
let totalBytes = 0;
28+
let totalChars = 0;
29+
2630
filePaths.forEach((filePath) => {
27-
const content = fs.readFileSync(filePath, "utf-8");
28-
29-
const lines = content.split("\n").length - 1;
30-
const words = content
31-
.trim()
32-
.split(/\s+/)
33-
.filter((word) => word != "").length;
34-
// here I used Buffer.byteLength even if characters and bytes can be the same number .length, however sometimes an emoji or special characters can be heavier 2b or 4b
35-
const bytes = Buffer.byteLength(content);
36-
const characters = content.length;
37-
38-
let output = "";
39-
40-
if (showLines) output += `${lines} `;
41-
if (showWords) output += `${words} `;
42-
if (showBytes) output += `${bytes} `;
43-
if (showCharacters) output += `${characters} `;
44-
45-
console.log(`${output} ${filePath}`);
46-
});
31+
try {
32+
const content = fs.readFileSync(filePath, "utf-8");
33+
const lineArray = content.split("\n");
34+
if (content.endsWith("\n")) lineArray.pop();
35+
const lines = lineArray.length;
36+
37+
const words = content.trim().split(/\s+/).filter((word) => word != "").length;
38+
const bytes = Buffer.byteLength(content);
39+
const characters = content.length;
40+
41+
totalLines += lines;
42+
totalWords += words;
43+
totalBytes += bytes;
44+
totalChars += characters;
45+
46+
let output = "";
47+
if (showLines) output += lines.toString().padStart(8);
48+
if (showWords) output += words.toString().padStart(8);
49+
if (showBytes) output += bytes.toString().padStart(8);
50+
if (showCharacters) output += characters.toString().padStart(8)
51+
52+
console.log(`${output} ${filePath}`);
53+
} catch(error) {
54+
console.error(`Error reading ${filePath}: ${error.message}`);
55+
}
56+
});
57+
58+
if (filePaths.length > 1) {
59+
let totalOutput = "";
60+
if (showLines) totalOutput += `${totalLines.toString().padStart(8)}`;
61+
if (showWords) totalOutput += `${totalWords.toString().padStart(8)}`;
62+
if (showBytes) totalOutput += `${totalBytes.toString().padStart(8)}`;
63+
if (showCharacters) totalOutput += `${totalChars.toString().padStart(8)}`;
64+
65+
console.log(`${totalOutput} total`);
66+
}
67+

0 commit comments

Comments
 (0)