-
-
Notifications
You must be signed in to change notification settings - Fork 105
NW|26-JUI-SDC|Ahmad Hmedan|Sprint 3 |Implement-shell-tools-JS #599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AhmadHmedann
wants to merge
11
commits into
CodeYourFuture:main
Choose a base branch
from
AhmadHmedann:Implement-shell-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e2983ea
Implement cat CLI with -n and -b options
AhmadHmedann 0f7b2b0
Implement ls CLI with -a options
AhmadHmedann 3f651c8
Implement wc CLI with -a options
AhmadHmedann 481e074
fix: handle per-file errors and empty file word count
AhmadHmedann a759353
refactor: simplify wc implemention
AhmadHmedann a28e72e
fix: Set the process exit code to 1 if any file fails.
AhmadHmedann a33518b
fix: improve output formatting
AhmadHmedann d379ae6
refactor: remove dublicate code
AhmadHmedann d4752a4
feat: one per line implemention
AhmadHmedann 30b2b19
refactor:
AhmadHmedann 7f12983
Remove debug console log in ls.mjs
AhmadHmedann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { promises as fs } from "node:fs"; | ||
| import { program } from "commander"; | ||
|
|
||
| program | ||
| .name("cat") | ||
| .description("my own cat program") | ||
| .option("-n", "number all lines") | ||
| .option("-b", "number non-empty lines") | ||
| .argument("<paths...>", "The file path to process"); | ||
| program.parse(); | ||
|
|
||
| const paths = program.args; | ||
| const options = program.opts(); | ||
|
|
||
| let lineNumber = 1; | ||
| let hadError = false; | ||
| for (const path of paths) { | ||
| try { | ||
| const content = await fs.readFile(path, "utf-8"); | ||
| if (options.n || options.b) { | ||
| const lines = content.split("\n"); | ||
| if (lines[lines.length - 1] === "") { | ||
| lines.pop(); | ||
| } | ||
|
|
||
| if (options.b) { | ||
| for (const line of lines) { | ||
| if (line.trim() !== "") { | ||
| process.stdout.write(` ${lineNumber} ${line}\n`); | ||
| lineNumber++; | ||
| } else { | ||
| process.stdout.write("\n"); | ||
| } | ||
| } | ||
| } else if (options.n) { | ||
| for (const line of lines) { | ||
| process.stdout.write(`${String(lineNumber).padStart(6)}\t ${line}\n`); | ||
| lineNumber++; | ||
| } | ||
| } | ||
| } else { | ||
| process.stdout.write(content); | ||
| } | ||
| } catch (error) { | ||
| console.error(error.message); | ||
| hadError = true; | ||
| } | ||
| } | ||
| if (hadError) { | ||
| process.exitCode = 1; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { promises as fs } from "node:fs"; | ||
| import { program } from "commander"; | ||
|
|
||
| program | ||
| .name("ls ") | ||
| .description("ls implementation") | ||
| .argument("[path]", "The path to process") //zero or one path | ||
| .option("-1, --one-per-line", "one file per line") | ||
|
LonMcGregor marked this conversation as resolved.
|
||
| .option("-a", "show hidden files"); | ||
| program.parse(); | ||
|
|
||
| const path = program.args[0] || "."; | ||
| const options = program.opts(); | ||
| try { | ||
| const files = await fs.readdir(path); | ||
| const visibleFiles = files.filter( | ||
| (file) => options.a || !file.startsWith("."), | ||
| ); | ||
| if(options.onePerLine) | ||
| { | ||
| console.log(visibleFiles.join("\n")) | ||
| }else{ | ||
| console.log(visibleFiles.join(" ")) | ||
| } | ||
|
|
||
| } catch (error) { | ||
| console.error(error.message); | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "dependencies": { | ||
| "commander": "^15.0.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { program } from "commander"; | ||
|
|
||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("wc") | ||
| .description("wc implementation") | ||
| .argument("<paths...>", "the file path to process") | ||
| .option("-l", "count lines") | ||
| .option("-w", "count words") | ||
| .option("-c", "count characters"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const paths = program.args; | ||
| const options = program.opts(); | ||
| const noFlag = !options.l && !options.w && !options.c; | ||
|
|
||
| const total = {}; | ||
| let hadError = false; | ||
| for (const path of paths) { | ||
| try { | ||
| const content = await fs.readFile(path, "utf-8"); | ||
|
|
||
| const linesCounter = content.split("\n").length - 1; | ||
| const trimmedContent = content.trim(); | ||
| const wordsCounter = | ||
| trimmedContent === "" ? 0 : trimmedContent.split(/\s+/).length; | ||
| const characterCounter = content.length; | ||
|
|
||
| const results = []; | ||
| if (options.l || noFlag) { | ||
| results.push(linesCounter); | ||
| total["lineCounter"] = (total["lineCounter"] ?? 0) + linesCounter; | ||
| } | ||
| if (options.w || noFlag) { | ||
| results.push(wordsCounter); | ||
| total["wordsCounter"] = (total["wordsCounter"] ?? 0) + wordsCounter; | ||
| } | ||
| if (options.c || noFlag) { | ||
| results.push(characterCounter); | ||
| total["characterCounter"] = | ||
| (total["characterCounter"] ?? 0) + characterCounter; | ||
| } | ||
|
|
||
| console.log(results.map(value=> String(value).padStart(4)).join(" ") +" "+ path) | ||
|
|
||
| } catch (error) { | ||
| console.error(error.message); | ||
| hadError = true; | ||
| } | ||
| } | ||
| if (paths.length > 1) { | ||
|
LonMcGregor marked this conversation as resolved.
|
||
|
|
||
| console.log(Object.values(total).map(value=> String(value).padStart(4)).join(" "),"total") | ||
|
|
||
| } | ||
| if (hadError) { | ||
| process.exitCode = 1; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.