-
-
Notifications
You must be signed in to change notification settings - Fork 105
London | 26-SDC-July | AYK| Sprint 3 | Implement shell tools #612
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import {program} from "commander"; | ||
| import {promises as fs} from "node:fs"; | ||
| import process from "node:process"; | ||
|
|
||
| program | ||
| .name ("cat") | ||
| .description ("A JS clone for unix command cat") | ||
| .argument("<files...>","One or more files to reach and print") | ||
| .option("-n", "number all output lines") | ||
| .action(async (files,options)=> { | ||
| for (const filePath of files){ | ||
| try { | ||
| const content = await fs.readFile(filePath,"utf8"); | ||
| if (options.n) | ||
| { | ||
| const lines = content.trimEnd().split("\n"); | ||
| lines.forEach((line,index) => { | ||
| console.log(`${index+1} \t ${line}`); | ||
| }); | ||
| } | ||
| else | ||
| { | ||
| process.stdout.write(content); | ||
| } | ||
|
|
||
|
|
||
| } | ||
| catch(error) | ||
| { | ||
| console.error("No such file or directory"); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| }); | ||
|
|
||
| program.parse(process.argv); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type":"module" , | ||
| "dependencies": { | ||
| "commander": "^15.0.0" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import {program} from "commander"; | ||
| import process from "node:process"; | ||
| import {promises as fs } from "node:fs"; | ||
|
|
||
|
|
||
| program | ||
| .name ("list command clone") | ||
| .description("List all files & folders within current folder") | ||
| .argument("[directory]","Directory to list",".") | ||
| .option("-1","list file line by line") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you correctly handling the -1 option? have you implemented everything the readme asked for? |
||
| .action(async (directory,options) => | ||
| { | ||
| try | ||
| { | ||
| let files = await fs.readdir(directory); | ||
| console.log(files.join(" ")); | ||
| } | ||
|
|
||
| catch (error) | ||
| { | ||
| console.error('No such file or directory'); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| program.parse(process.argv); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "module", | ||
| "dependencies": { | ||
| "commander": "^15.0.0" | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "module", | ||
| "dependencies": { | ||
| "commander": "^15.0.0" | ||
| } | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import process from "node:process"; | ||
| import {program} from "commander"; | ||
| import {promises as fs} from "node:fs"; | ||
|
|
||
| program | ||
| .name("WC clone") | ||
| .description("Counting each words in a given file") | ||
| .argument("<files...>","one or more files to count the words") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about arguments and options, have you implemented everything the readme asked for? |
||
| .action(async (files)=>{ | ||
| let totalLines = 0; | ||
| let totalWords = 0; | ||
| let totalBytes = 0; | ||
|
|
||
| for (const file of files){ | ||
| try { | ||
| const content = await fs.readFile(file,"utf-8"); | ||
| const size = await fs.readFile(file); | ||
|
|
||
| const lineCount = content.split("\n").length-1; | ||
|
|
||
| const wordCount = content.trim() === "" ? 0 : content.trim().split(/\s+/).length; | ||
|
|
||
| const byteCount = size.length; | ||
|
|
||
| totalLines += lineCount; | ||
| totalWords += wordCount; | ||
| totalBytes += byteCount; | ||
|
|
||
| printCounts({lineCount,wordCount,byteCount,label:file,}); | ||
| } | ||
|
|
||
| catch (err){ | ||
| console.error("No such file or directory"); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| if (files.length>1) | ||
| { | ||
| printCounts({lineCount:totalLines, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for passing an object then destructuring in printCounts? |
||
| wordCount:totalWords, | ||
| byteCount:totalBytes, | ||
| label:"Total", | ||
| }) | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| function printCounts({lineCount,wordCount,byteCount,label}) | ||
| { | ||
| let output = ""; | ||
|
|
||
| output += lineCount.toString().padStart(7, " "); | ||
| output += wordCount.toString().padStart(7," "); | ||
| output += byteCount.toString().padStart(7," "); | ||
|
|
||
| console.log(`${output} ${label}`); | ||
| } | ||
|
|
||
| program.parse(process.argv); | ||
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.
Good work so far, have you implemented everything the readme asked for?