generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Glasgow | 25-SDC-July | Prati Amalden | Sprint 3 | Implement shell tools #130
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
PratiAmalden
wants to merge
8
commits into
CodeYourFuture:main
Choose a base branch
from
PratiAmalden: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 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0482554
add package.json
PratiAmalden 8020365
implement cat
PratiAmalden 85bb502
implement ls
PratiAmalden e785ce0
implement wc
PratiAmalden 9139868
fix cat error handler
PratiAmalden 287fd3a
add try/catch for ls
PratiAmalden d8c098f
fix cat
PratiAmalden f6c367c
Exit with code 1 if wc fails to read a file
PratiAmalden 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,45 @@ | ||
| import { program } from "commander"; | ||
| import {promises as fs} from "node:fs"; | ||
|
|
||
| program | ||
| .name("cat") | ||
| .description("read, display, and concatenate text files.") | ||
| .option("-n", " Number all output lines.") | ||
| .option("-b", " Number non-blank output lines.") | ||
| .arguments("<paths...>"); // allow more file paths | ||
|
|
||
| program.parse(); | ||
|
|
||
| const options = program.opts(); | ||
| const paths = program.args; | ||
|
|
||
| for(const path of paths){ | ||
| let content; | ||
| try { | ||
| content = await fs.readFile(path, "utf-8") | ||
| } catch(err) { | ||
| console.error(`Error reading file "${path}": ${err.message} `); | ||
| continue; | ||
| } | ||
|
|
||
| // split file into lines | ||
| let lines = content.replace(/\n$/, "").split("\n"); | ||
|
|
||
| let lineNum = 1; | ||
|
|
||
| for (const line of lines){ | ||
| if(options.b){ | ||
| if(line.trim() !== ""){ | ||
| console.log(`${lineNum.toString().padStart(5)} ${line}`) | ||
| lineNum++; | ||
| } else { | ||
| console.log(""); | ||
| } | ||
| } else if(options.n){ | ||
illicitonion marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| console.log(`${lineNum.toString().padStart(5)} ${line}`) | ||
| lineNum++; | ||
| } else{ | ||
| console.log(`${line}`) | ||
| } | ||
| } | ||
| } | ||
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,29 @@ | ||
| import {program} from "commander"; | ||
| import {promises as fs} from "node:fs"; | ||
|
|
||
| program | ||
| .name("ls") | ||
| .description("List all the files in a directory") | ||
| .option("-a, --all", "Include hidden files") | ||
| .option("-1", "One entry per line") | ||
| .argument("[dir]", "directory to list", "."); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const options = program.opts(); | ||
| const dir = program.args[0] || "."; | ||
|
|
||
| const entries = await fs.readdir(dir, { withFileTypes: true }); | ||
|
|
||
| const visibleNames = []; | ||
|
|
||
| for(const entry of entries){ | ||
| if(!options.all && entry.name.startsWith(".")) continue; | ||
| visibleNames.push(entry.name); | ||
| } | ||
|
|
||
| if(options["1"]){ | ||
| console.log(visibleNames.join("\n")); | ||
| } else{ | ||
| console.log(visibleNames.join(" ")); | ||
| } |
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,16 @@ | ||
| { | ||
| "name": "implement-shell-tools", | ||
| "version": "1.0.0", | ||
| "description": "Your task is to re-implement shell tools you have used.", | ||
| "main": "index.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "dependencies": { | ||
| "commander": "^14.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,65 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("wc") | ||
| .description("Display numbers of line, words, and bytes in each file") | ||
| .option("-l", "Number of lines") | ||
| .option("-w", "Number of words") | ||
| .option("-c", "Number of bytes") | ||
| .argument("<path...>"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const options = program.opts(); | ||
| const paths = program.args; | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalBytes = 0; | ||
|
|
||
| for(const path of paths){ | ||
| let content; | ||
| try{ | ||
| content = await fs.readFile(path, "utf-8"); | ||
| } catch (err){ | ||
| console.error(`Error reading file "${path}":`, err.message); | ||
illicitonion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| continue; | ||
| } | ||
|
|
||
| const lines = content.replace(/\n$/, "").split("\n"); | ||
|
|
||
| const words = content.trim().split(/\s+/); // handles multiple spaces | ||
| const { size } = await fs.stat(path); | ||
|
|
||
| const lineCount = lines.length; | ||
| const wordCount = words.length; | ||
| const byteCount = size; | ||
|
|
||
| totalLines += lineCount; | ||
| totalWords += wordCount; | ||
| totalBytes += byteCount; | ||
|
|
||
| if(options.l) { | ||
| console.log(`\t${lineCount} ${path}`); | ||
| } else if(options.w) { | ||
| console.log(`\t${wordCount} ${path}`); | ||
| } else if(options.c) { | ||
| console.log(`\t${byteCount} ${path}`) | ||
| } else { | ||
| console.log(`\t${lineCount}\t${wordCount}\t${size} ${path}`); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| if (paths.length > 1) { | ||
| if (options.l) { | ||
| console.log(`\t${totalLines} total`); | ||
| } else if (options.w) { | ||
| console.log(`\t${totalWords} total`); | ||
| } else if (options.c) { | ||
| console.log(`\t${totalBytes} total`); | ||
| } else { | ||
| console.log(`\t${totalLines}\t${totalWords}\t${totalBytes} total`); | ||
| } | ||
| } | ||
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.