-
-
Notifications
You must be signed in to change notification settings - Fork 42
London | 25-SDC-July | Andrei Filippov | Sprint 3 | Implement shell tools #126
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 13 commits
6b47b71
fb5acb7
f094e2f
9907bec
47ca368
56ac17f
59f994c
49ce080
b9e33e1
c116a34
e9f9571
7fcbf79
fabd47a
c61cbdf
85d6a7f
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,60 @@ | ||
| import { promises as fs } from "node:fs"; | ||
| import { program } from "commander"; | ||
|
|
||
| program | ||
| .name("cat") | ||
| .description("Concatenate and print files") | ||
| .option("-n", "Number the output lines, starting at 1") | ||
| .option("-b", "Number the non-blank output lines, starting at 1") | ||
| .argument("<sample-files...>", "The file path to process") | ||
| .parse(); | ||
|
|
||
| const argv = program.args; | ||
|
|
||
| const opts = program.opts(); | ||
|
|
||
| const countLines = (data) => { | ||
| const lines = data.split("\n"); | ||
| if (lines[lines.length - 1] === "") { | ||
| lines.pop(); | ||
| } | ||
|
|
||
| let lineNum = 1; | ||
|
|
||
| for (const line of lines) { | ||
| if (opts.b) { | ||
| if (line.trim() === "") { | ||
| console.log(); | ||
| } else { | ||
| console.log(`${lineNum} ${line}`); | ||
| lineNum++; | ||
| } | ||
| } else if (opts.n) { | ||
| console.log(`${lineNum} ${line}`); | ||
| lineNum++; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| async function example(path) { | ||
| try { | ||
| const data = await fs.readFile(path, { encoding: "utf8" }); | ||
| if (opts["b"]) { | ||
| countLines(data); | ||
| } else if (opts["n"]) { | ||
| countLines(data); | ||
| } else { | ||
| console.log(data.trimEnd()); | ||
| } | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
| } | ||
|
|
||
| const handleInput = async () => { | ||
| for (const path of argv) { | ||
| await example(path); | ||
| } | ||
| }; | ||
|
|
||
| handleInput(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { promises as fs } from "node:fs"; | ||
| import process from "node:process"; | ||
| import path from "node:path"; | ||
| import { program } from "commander"; | ||
|
|
||
| program | ||
| .name("ls") | ||
| .description("Shows files in directory") | ||
| .option("-1", "list one file per line") | ||
| .option( | ||
| "-a", | ||
| "Used to list all files, including hidden files, in the current directory" | ||
| ) | ||
| .argument("[sample-files]", "The file path to process"); | ||
|
|
||
| program.parse(); | ||
|
|
||
| let pathToFile = ""; | ||
|
|
||
| const programArgv = program.args; | ||
|
|
||
| (async () => { | ||
| if (programArgv.length === 1) { | ||
| pathToFile = programArgv[0]; | ||
| try { | ||
| const stats = await fs.stat(pathToFile); | ||
| if (stats.isFile()) { | ||
| await listFiles("file"); | ||
| } else if (stats.isDirectory()) { | ||
|
|
||
| listFiles("directory"); | ||
| } else { | ||
| console.error("Not a file or directory."); | ||
| } | ||
| } catch (err) { | ||
| console.error("Invalid path:", err.message); | ||
| } | ||
| } else if (programArgv.length === 0) { | ||
| pathToFile = process.cwd(); | ||
| await listFiles("directory"); | ||
| } else { | ||
| console.error( | ||
| `Expected no more than 1 argument (sample-files) but got ${programArgv.length}.` | ||
| ); | ||
| } | ||
| })(); | ||
|
|
||
| const flag_1 = (files) => { | ||
| try { | ||
| files.forEach(function (file) { | ||
| console.log(file); | ||
|
|
||
| }); | ||
| } catch (err) { | ||
| console.error("Error reading directory:", err); | ||
| } | ||
| }; | ||
|
|
||
| const flag_a = (files) => { | ||
| try { | ||
| files.unshift(".."); | ||
| files.unshift("."); | ||
| return files; | ||
| } catch (err) { | ||
| console.error("Error reading directory:", err); | ||
|
||
| } | ||
| }; | ||
|
|
||
| async function listFiles(type) { | ||
| let output = [] | ||
| try { | ||
| let formattedPath = ""; | ||
| if (type == "directory") { | ||
| formattedPath = pathToFile; | ||
| } else if (type == "file") { | ||
| formattedPath = path.dirname(pathToFile); | ||
| } | ||
| const char = program.opts(); | ||
| const files = await fs.readdir(formattedPath); | ||
| const sortedOutput = files.sort((a, b) => a.localeCompare(b)); | ||
|
|
||
| if (char["a"]){ | ||
| output = flag_a(sortedOutput) | ||
| } else { | ||
| sortedOutput.forEach(function (file) { | ||
|
|
||
| if (file[0] != "."){ | ||
| output.push(file) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| if (char["1"]){ | ||
| flag_1(output) | ||
| } else { | ||
| console.log(output.join(" ")); | ||
| } | ||
| } catch (err) { | ||
| console.error("Error reading directory:", err); | ||
| } | ||
| } | ||
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": "^14.0.0" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { program } from "commander"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| program | ||
| .name("count-containing-words") | ||
| .description("Counts words in a file that contain a particular character") | ||
| .option( | ||
| "-l", | ||
| "The number of lines in each input file is written to the standard output." | ||
| ) | ||
| .option( | ||
| "-w", | ||
| "The number of words in each input file is written to the standard output." | ||
| ) | ||
| .option( | ||
| "-c", | ||
| "The number of bytes in each input file is written to the standard output." | ||
| ) | ||
| .argument("<path...>", "The file path to process") | ||
| .parse(); | ||
|
|
||
| const argv = program.args; | ||
|
|
||
| const opts = program.opts(); | ||
|
|
||
| const total = []; | ||
| const output = []; | ||
|
|
||
| const flag_c = (content) => { | ||
| output.push(Buffer.byteLength(content, "utf8")); | ||
| }; | ||
|
|
||
| const flag_w = (content) => { | ||
| output.push(content.match(/\b[\w']+\b/g).length); | ||
| }; | ||
|
|
||
| const flag_l = (content) => { | ||
| output.push(content.split("\n").length - 1); | ||
| }; | ||
|
|
||
| const countAndDisplay = async (path) => { | ||
| const content = await fs.readFile(path, "utf-8"); | ||
| output.splice(0); | ||
| if (opts["l"]) { | ||
| flag_l(content); | ||
| } | ||
| if (opts["w"]) { | ||
| flag_w(content); | ||
| } | ||
| if (opts["c"]) { | ||
| flag_c(content); | ||
| } | ||
| if (argv.length > 1) { | ||
| if (total.length == 0) { | ||
| total.push(...output); | ||
| } else { | ||
| for (let index = 0; index < output.length; index++) { | ||
| total[index] += output[index]; | ||
| } | ||
| } | ||
| } | ||
| console.log([...output, path].join(" ")); | ||
|
||
| }; | ||
|
|
||
| const handleInput = async () => { | ||
| if (Object.keys(opts).length == 0) { | ||
| ["l", "w", "c"].forEach((key) => (opts[key] = true)); | ||
| } | ||
| for (const path of argv) { | ||
| await countAndDisplay(path); | ||
| } | ||
| if (argv.length > 1) { | ||
| console.log(`${total.join(" ")} total`); | ||
| } | ||
| }; | ||
| handleInput(); | ||
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.
If I pass multiple files in, the line numbering resets to 1 for each file. Can you make your implementation work more like the original cat which uses a continuous numbering?
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.
Since I’m on macOS, the built-in cat also resets line numbers to 1 for each file. Its output looks like this: