London | 26-SDC-MAR | Jamal Laqdiem | Sprint 3 | Implement Shell Tools#437
London | 26-SDC-MAR | Jamal Laqdiem | Sprint 3 | Implement Shell Tools#437jamallaqdiem wants to merge 4 commits intoCodeYourFuture:mainfrom
Conversation
implement-shell-tools/cat/cat.js
Outdated
| } | ||
| }); | ||
| } catch (error) { | ||
| console.log(`Could not read: ${path}`); |
There was a problem hiding this comment.
If there's an error where will this message get printed? stdout or stderr? And what exit code would the process exit with?
Do these match our expectations from shell tools?
There was a problem hiding this comment.
Thanks for reviewing, I made the changes from console.log that wil send txt to stdout To console.error that willsend to stderr and display the error in the screen.
Added pocess.exit(1) to make sure that in case of failure the code should exit with a non zero code.
implement-shell-tools/cat/cat.js
Outdated
| console.log(`${counterLines++} ${line}`); | ||
| } else if (showNonBlankNumbers) { | ||
| // increment and show numbers only if the line is not empty. | ||
| if (line.trim() !== "") { | ||
| console.log(`${counterLines++} ${line}`); | ||
| } else { | ||
| // print empty lines | ||
| console.log(line); | ||
| } | ||
| } else { | ||
| console.log(line); |
There was a problem hiding this comment.
The three branches here look quite similar and repetitive. In general, if you have multiple similar branches, it's more clear to extract the differences into variables, and then run the same code, i.e. so you'd only have one call to console.log which looks more like console.log(`${prefix}${line}\n`) where prefix may be set differently based on options (including potentially an empty string).
This way it's easier for someone reading the code to see what's the same / different in each case, and also avoids the hazard that someone updates one of the branches but forgets to update the other ones.
There was a problem hiding this comment.
Thanks for reviewing, I followed your correct instructions , as it's much clearer and readable using a variable.
| const showNonBlankNumbers = argv.includes("-b"); | ||
|
|
||
| //filter the - from the array argv as it's a flag. | ||
| const filePaths = argv.filter((arg) => !arg.startsWith("-")); |
There was a problem hiding this comment.
What would happen if someone passed a -q flag here? What should happen?
There was a problem hiding this comment.
Thanks for reviewing, you are right to point that out, my logic will let the user confused thinking that the -q flag exists and worked, I implemented in place a flag check and ensured print an error and exit with not 0 code.
| const showHiddenFiles = argv.includes("-a"); | ||
|
|
||
| // if no folder provide we use the current one | ||
| const target = filePaths[0] || "."; |
There was a problem hiding this comment.
What would happen if someone specified multiple paths, e.g. ls /some/file /some/other/file? What does your implementation do?
The README.md only requires that your programme works with simple paths, but I would recommend implementing support for multiple. But if you don't implement that, you generally want to give an error to the user if they supply input you don't expect, rather than just ignoring it.
There was a problem hiding this comment.
Thanks for reviewing, I should at least display an stderr and exit .
| filePaths.forEach((filePath) => { | ||
| const content = fs.readFileSync(filePath, "utf-8"); | ||
|
|
||
| const lines = content.split("\n").length - 1; |
There was a problem hiding this comment.
Why do you have to - 1 here?
There was a problem hiding this comment.
Thanks for reviewing, I used -1 because my vscode automatically add a new line when save, so in this case split method will add an empty element, however in other environments this logic my fail if no trialling new line added .
There was a problem hiding this comment.
We may add a conditional check using .pop method in case of empty string at the end
implement-shell-tools/wc/wc.js
Outdated
|
|
||
| let output = ""; | ||
|
|
||
| if (showLines || noFlags) output += `${lines} `; |
There was a problem hiding this comment.
Can you think of a way to achieve this same result, but without needing a noFlags variable at all?
If our program was bigger and more complicated, in most of the program we don't want to have to think about what the exact flags interface was. Ideally we can just look at showLines to decide whether we should show lines, without needing to worry which exactly flags led to us needing to show lines.
There was a problem hiding this comment.
Thanks for reviewing, I implemented a clear way to switch all flags to true if no flag was picked.
Fixed a bug.
Learners, PR Template
Self checklist
Changelist
shell tools exercises.