generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Sheffield | 25-SDC-Nov | Hassan Osman | Sprint 3 | Implement Shell Tools #248
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
HassanOHOsman
wants to merge
80
commits into
CodeYourFuture:main
Choose a base branch
from
HassanOHOsman:answers/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
80 commits
Select commit
Hold shift + click to select a range
df4ff07
create cat.js file to implement cat logic in it
HassanOHOsman 2ee4991
import 2 built in NodeJS modules
HassanOHOsman 0931aea
checks if our program receieves 1 argument otherwise throw an error m…
HassanOHOsman f8392fa
storing the one argument in our program into a a variable called path
HassanOHOsman 23b18aa
Read file at the path passed as argument in the programme
HassanOHOsman 136af7b
rename content var in a more meaningful way
HassanOHOsman 0f50542
log out/display a file content without adding a new line
HassanOHOsman 2289a8d
installed commander library and updated the type property in JSON file
HassanOHOsman b236e17
improting commander library into js file
HassanOHOsman 4f65e6c
setting up basic info about our program and most importantly a flag/o…
HassanOHOsman 72dab99
update the program info to include argument
HassanOHOsman b6de478
commader to interpret the command line arguments based on -n option
HassanOHOsman 68982c6
read all the flags/options provided.
HassanOHOsman eaf8d3b
cleaning code and getting rid of useless bits
HassanOHOsman 815dd88
splitting the contnet of a file by newline and storing it in a var na…
HassanOHOsman 73f9e36
build logic for number of lines options
HassanOHOsman bff2e3b
adding a condition as to when the -n option used
HassanOHOsman ee72ac4
getting rid of numbered trailing empty lines
HassanOHOsman 2ba43de
define -b option in the porgram info
HassanOHOsman 583c011
update logic for -n & -b options
HassanOHOsman 13d12fe
update & correct logic for -n & -b options
HassanOHOsman 8ba6202
join the array elements by new line and output the formed text to end…
HassanOHOsman 64ff212
add a scenario as to when the program doesn't contain any -n or -b flags
HassanOHOsman bbe5b0c
reworking on code so that it handles outputting content from multiple…
HassanOHOsman ddf594b
work on logic to hadle multiple files
HassanOHOsman ee0d711
update the argument in the program info to handle multiple paths
HassanOHOsman d123b19
create ls file to store all the js program logic
HassanOHOsman cd93e63
run teminal commands to add package.json since i'll be using import t…
HassanOHOsman 584666e
import built in and third part modules/libraries into the ls.js file
HassanOHOsman ae56d9b
uodate the type value to "module" so that i could use "import" keyword
HassanOHOsman 49b4e4b
Initialize program with basic info (name, description, argument & opt…
HassanOHOsman 8f7a7e1
Add argument parsing for file path
HassanOHOsman a67234a
sotre program.opts in meaningful var options
HassanOHOsman 385b875
path expect one argument /file path
HassanOHOsman d5ea768
directoryContent stores a value that reads the content (files & folde…
HassanOHOsman 5adee43
adding a for loop for when -1 option is added to an ls program
HassanOHOsman d84dd4d
update the -1 flag description in the program info part
HassanOHOsman bd01e5c
add spacing for when ls is used without the -1/--one option
HassanOHOsman 83940e0
add option f-a for handling all files - including hidden ones - in th…
HassanOHOsman 6943c01
logic to filter out hidden files form LS output if -a is not added to it
HassanOHOsman 0ec7440
update logic for when option -a not selected
HassanOHOsman 9a639b6
create "wc.js" file to house my programme
HassanOHOsman 1b2e8ec
initialised npm and installed "Commander" library
HassanOHOsman bebba1c
update the type property value to "module" in the JSON file
HassanOHOsman e2ee819
import built-in & third party dependancies into the wc.js file
HassanOHOsman b87db26
set the program basic info and the first option: -l
HassanOHOsman b291f4f
interpert the command line argument based on option and ensure that t…
HassanOHOsman 0e8d2b9
ensuring that excactly one argument passed onto our porgramme
HassanOHOsman f827bfd
the first (and only) user argument sotred in a variable path
HassanOHOsman 8139354
Add options parsing and directory reading
HassanOHOsman ee0f23e
improt "stat" function from the fs module
HassanOHOsman eb7c00b
use stat function to grap info about the file or directory and store …
HassanOHOsman ee3b4eb
add if condition to handle 2 cases: 1) path is file or path is dir
HassanOHOsman 62a1c7d
build a path for each file in a dir and process the file depending on…
HassanOHOsman d4644e1
create a function to count lines
HassanOHOsman b51271e
include word counting functionality in the function counter
HassanOHOsman e9dc04e
make the function counter characters too
HassanOHOsman fbb3e4e
add return values inside the function
HassanOHOsman 2af4968
add the function counter the inside if condition for the case of deal…
HassanOHOsman 87768a1
update if block to handle when -l option logic for when it's added to…
HassanOHOsman 4196751
remove the buggy line
HassanOHOsman 5cbefa1
update the function so that it returns the numebr of lines rather tha…
HassanOHOsman 03d9fb6
adding .trim() method to get rid of the spaces (not counting blank li…
HassanOHOsman 091692c
add a function for the logic for when the case of the path is "dir"
HassanOHOsman c6aa7ae
for dir case: create logic to handdle when -l option is added to the …
HassanOHOsman 524af1d
update the error in the else for dir
HassanOHOsman 46c5df5
update bugy lines of code
HassanOHOsman c58db9c
remove the validation for if we have multiple arguments as this clash…
HassanOHOsman eb67a84
update logic to handl having mutple paths (files) when dealing with a…
HassanOHOsman 8a546e1
add counters for total lines, words & characters for when the path is…
HassanOHOsman ec4de6a
for the case of a dire print the total coutner
HassanOHOsman 009c40b
add -w option in the program basic info
HassanOHOsman c32410a
flesh out logic to include the -w & -c options
HassanOHOsman 3cc8aaf
code now handles no flag cases for all scenarios (file or dir)
HassanOHOsman 397d4e0
update logic to match the wc command line in the way it displays output
HassanOHOsman e155672
house keeping
HassanOHOsman d2daa0e
remove redantant and repetitive code
HassanOHOsman 4fdb357
validating the path first
HassanOHOsman b2563c1
add try-catch code block
HassanOHOsman b5866c5
change
HassanOHOsman 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,53 @@ | ||
| import process from "node:process"; | ||
| import {promises as fs} from "node:fs"; | ||
| import {program} from "commander"; | ||
|
|
||
| program | ||
| .name("display-file-content") | ||
| .description("Output the content of a file to the terminal") | ||
| .argument("<path...>", "The file path to process") | ||
| .option("-n", "Number the output lines") | ||
| .option("-b","Number the non-blank output lines") | ||
|
|
||
| program.parse(); | ||
|
|
||
| const paths = program.args; | ||
|
|
||
| const options = program.opts(); | ||
|
|
||
| let lineNumber = 1; | ||
|
|
||
| for (const path of paths) { | ||
| const filesContent = await fs.readFile(path, "utf-8"); | ||
|
|
||
| const lines = filesContent.split("\n"); | ||
|
|
||
| if (lines[lines.length - 1] === "") { | ||
| lines.pop(); | ||
| } | ||
|
|
||
| for (let line of lines) { | ||
| if (options.n) { | ||
| process.stdout.write(`${lineNumber} ${line}\n`); | ||
| lineNumber++; | ||
| } else if (options.b) { | ||
| if (line != "") { | ||
| process.stdout.write(`${lineNumber} ${line}\n`); | ||
| lineNumber++; | ||
| } else { | ||
| process.stdout.write("\n"); | ||
| } | ||
| } else { | ||
| process.stdout.write(line + "\n"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
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": "cat", | ||
| "version": "1.0.0", | ||
| "description": "You should already be familiar with the `cat` command line tool.", | ||
| "main": "cat.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "type": "module", | ||
| "dependencies": { | ||
| "commander": "^14.0.2" | ||
| } | ||
| } |
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,48 @@ | ||
| import process from "node:process"; | ||
| import { promises as fs } from "node:fs"; | ||
| import { program } from "commander"; | ||
|
|
||
| program | ||
| .name("list-files-in-directory") | ||
| .description("List all files and directories in a directory") | ||
| .argument("<path>", "The file path to process") | ||
| .option("-1, --one", "Output one entry per line") | ||
| .option("-a", "List all files & directories, including hidden ones"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const path = program.args[0]; | ||
|
|
||
| const options = program.opts(); | ||
|
|
||
| if (!path) { | ||
| console.error("Error: No directory path was provided."); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let directoryContent; | ||
|
|
||
| try { | ||
| directoryContent = await fs.readdir(path); | ||
| } catch (err) { | ||
| console.error(`Error reading directory: ${err.message}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| let allContent = directoryContent; | ||
|
|
||
| if (!options.a) { | ||
| allContent = directoryContent.filter(name => !name.startsWith(".")); | ||
| } | ||
|
|
||
| for (const item of allContent) { | ||
| if (options.one) { | ||
| process.stdout.write(item + "\n"); | ||
| } else { | ||
| process.stdout.write(item + " "); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
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": "ls", | ||
| "version": "1.0.0", | ||
| "description": "You should already be familiar with the `ls` command line tool.", | ||
| "main": "ls.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "type": "module", | ||
| "dependencies": { | ||
| "commander": "^14.0.2" | ||
| } | ||
| } |
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": "wc", | ||
| "version": "1.0.0", | ||
| "description": "You should already be familiar with the `wc` command line tool.", | ||
| "main": "wc.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "keywords": [], | ||
| "author": "", | ||
| "license": "ISC", | ||
| "type": "module", | ||
| "dependencies": { | ||
| "commander": "^14.0.2" | ||
| } | ||
| } |
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,90 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
| import process from "node:process"; | ||
| import { stat } from "node:fs/promises"; | ||
|
|
||
| program | ||
| .name("count-containing-lines-words-characters") | ||
| .description("Counts lines, words or characters in a file (or all files) inside a directory") | ||
| .option("-l, --line", "The number of lines in each file") | ||
| .option("-w, --word", "The number of words in each file") | ||
| .option("-c, --character", "The number of characters in each file") | ||
| .argument("<path...>", "The file path to process"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| const argv = program.args; | ||
|
|
||
| const options = program.opts(); | ||
|
|
||
|
|
||
| function counter(item) { | ||
| const lines = item.trim().split("\n").length; | ||
| const words = item.split(/\s+/).filter(Boolean).length; | ||
| const characters = item.length; | ||
| return { lines, words, characters }; | ||
| } | ||
|
|
||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalCharacters = 0; | ||
| let fileCount = 0; | ||
|
|
||
| for (const path of argv) { | ||
| const pathInfo = await stat(path); | ||
|
|
||
| if (pathInfo.isFile()) { | ||
| const content = await fs.readFile(path, "utf-8"); | ||
| const stats = counter(content); | ||
| if (options.line) { | ||
| console.log(`${stats.lines} ${path}`); | ||
| } else if (options.word) { | ||
| console.log(`${stats.words} ${path}`); | ||
| } else if (options.character) { | ||
| console.log(`${stats.characters} ${path}`); | ||
| } else { | ||
| console.log(`${stats.lines} ${stats.words} ${stats.characters} ${path}`); | ||
| } | ||
|
|
||
| totalLines += stats.lines; | ||
| totalWords += stats.words; | ||
| totalCharacters += stats.characters; | ||
| fileCount++; | ||
|
|
||
| } else if (pathInfo.isDirectory()) { | ||
| const files = await fs.readdir(path); | ||
| for (const file of files) { | ||
| const filePath = `${path}/${file}`; | ||
| const fileContent = await fs.readFile(filePath, "utf-8"); | ||
| const stats = counter(fileContent); | ||
|
|
||
| if (options.line) { | ||
| console.log(`${stats.lines} ${filePath}`); | ||
| } else if (options.word) { | ||
| console.log(`${stats.words} ${filePath}`); | ||
| } else if (options.character) { | ||
| console.log(`${stats.characters} ${filePath}`); | ||
| } else { | ||
| console.log(`${stats.lines} ${stats.words} ${stats.characters} ${filePath}`); | ||
| } | ||
|
|
||
| totalLines += stats.lines; | ||
| totalWords += stats.words; | ||
| totalCharacters += stats.characters; | ||
| fileCount++; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| if (fileCount > 1) { | ||
| if (options.line) { | ||
| console.log(`${totalLines} total`); | ||
| } else if (options.word) { | ||
| console.log(`${totalWords} total`); | ||
| } else if (options.character) { | ||
| console.log(`${totalCharacters} total`); | ||
| } else { | ||
| console.log(`${totalLines} ${totalWords} ${totalCharacters} 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would happen if a directory was not provided - are we able to handle this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Matt, apologies for the very delayed response!
Thanks for bringing this to my attention.
I've just added validation to handle such a case.
Please review and let me know.