Skip to content

Commit e1d7c80

Browse files
committed
FIx: Implement the missing logics.
1 parent 99cb233 commit e1d7c80

4 files changed

Lines changed: 41 additions & 22 deletions

File tree

implement-shell-tools/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.venv

implement-shell-tools/cat/cat.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ const showNonBlankNumbers = argv.includes("-b");
1010

1111
//filter the - from the array argv as it's a flag.
1212
const filePaths = argv.filter((arg) => !arg.startsWith("-"));
13+
14+
const flagsUsed = argv.filter((arg) => arg.startsWith("-"));
15+
const supportedFlags = ["-n", "-b"];
16+
for (const flag of flagsUsed) {
17+
if (!supportedFlags.includes(flag)) {
18+
console.error(`Invalid option try 'node cat.js --help' for more info.`);
19+
process.exit(1);
20+
}
21+
}
22+
1323
let counterLines = 1;
1424

1525
for (const path of filePaths) {
@@ -20,21 +30,19 @@ for (const path of filePaths) {
2030
const splitLines = content.split("\n");
2131

2232
splitLines.forEach((line) => {
23-
if (showNumbers) {
24-
console.log(`${counterLines++} ${line}`);
25-
} else if (showNonBlankNumbers) {
26-
// increment and show numbers only if the line is not empty.
33+
let prefix = "";
34+
35+
if (showNonBlankNumbers) {
2736
if (line.trim() !== "") {
28-
console.log(`${counterLines++} ${line}`);
29-
} else {
30-
// print empty lines
31-
console.log(line);
37+
prefix = `${counterLines++} `;
3238
}
33-
} else {
34-
console.log(line);
39+
} else if (showNumbers) {
40+
prefix = `${counterLines++} `;
3541
}
42+
console.log(`${prefix}${line}`);
3643
});
3744
} catch (error) {
38-
console.log(`Could not read: ${path}`);
45+
console.error(`Could not read: ${path}`);
46+
process.exit(1);
3947
}
4048
}

implement-shell-tools/ls/ls.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const argv = process.argv.slice(2);
88
const filePaths = argv.filter((arg) => !arg.startsWith("-"));
99
const showHiddenFiles = argv.includes("-a");
1010

11+
if (filePaths.length > 1) {
12+
console.error("Error: This version only supports one directory path a time.");
13+
process.exit(1);
14+
}
1115
// if no folder provide we use the current one
1216
const target = filePaths[0] || ".";
1317
// read the file.

implement-shell-tools/wc/wc.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ import fs from "node:fs";
33

44
const argv = process.argv.slice(2);
55

6-
const showWords = argv.includes("-w");
7-
const showLines = argv.includes("-l");
8-
const showBytes = argv.includes("-c");
9-
const showCharacters = argv.includes("-m");
6+
let showWords = argv.includes("-w");
7+
let showLines = argv.includes("-l");
8+
let showBytes = argv.includes("-c");
9+
let showCharacters = argv.includes("-m");
1010

1111
// filter flags, and getting the string of filename
1212
const filePaths = argv.filter((arg) => !arg.startsWith("-"));
13-
14-
const noFlags = !showLines && !showCharacters && !showWords && !showCharacters;
15-
if (!filePaths) {
13+
// if no flags enable all.
14+
if (!showLines && !showCharacters && !showWords && !showBytes) {
15+
showLines = true;
16+
showCharacters = true;
17+
showWords = true;
18+
showBytes = true;
19+
}
20+
// fix bug .length ===0 will be true if nothing provided, instead !filePath will return empty array which result true.
21+
if (filePaths.length === 0) {
1622
console.error("PLease provide a file path");
1723
process.exit(1);
1824
}
@@ -31,10 +37,10 @@ filePaths.forEach((filePath) => {
3137

3238
let output = "";
3339

34-
if (showLines || noFlags) output += `${lines} `;
35-
if (showWords || noFlags) output += `${words} `;
36-
if (showBytes || noFlags) output += `${bytes} `;
37-
if (showCharacters || noFlags) output += `${characters} `;
40+
if (showLines) output += `${lines} `;
41+
if (showWords) output += `${words} `;
42+
if (showBytes) output += `${bytes} `;
43+
if (showCharacters) output += `${characters} `;
3844

3945
console.log(`${output} ${filePath}`);
4046
});

0 commit comments

Comments
 (0)