From b6e54aa666b0208436b36cdb3e0ef2a94493b038 Mon Sep 17 00:00:00 2001 From: Rihanna P <114884466+RihannaP@users.noreply.github.com> Date: Wed, 22 Oct 2025 00:10:56 +0100 Subject: [PATCH 1/4] Implement cat command --- implement-shell-tools/Package.json | 6 +++++ implement-shell-tools/cat/cat.js | 36 +++++++++++++++++++++++++ implement-shell-tools/package-lock.json | 21 +++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 implement-shell-tools/Package.json create mode 100644 implement-shell-tools/cat/cat.js create mode 100644 implement-shell-tools/package-lock.json diff --git a/implement-shell-tools/Package.json b/implement-shell-tools/Package.json new file mode 100644 index 00000000..dac6949b --- /dev/null +++ b/implement-shell-tools/Package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "dependencies": { + "commander": "^14.0.1" + } +} diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 00000000..289a4c03 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,36 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; +import process from "node:process"; + +program + .name("cat command") + .description("Implementing 'cat' command") + .option("-n", "The line numbers") + .option("-b", "The line numbers only for non-empty lines") + .argument("", "The file paths to process"); + +program.parse(); + +const paths = program.args; +let lineNumber = 1; +const displayLineNumber = program.opts().n; +const displayNonEmptyLineNumber = program.opts().b; + +for(const path of paths){ +const content = await fs.readFile(path, "utf-8"); +const lines = content.split("\n"); + for(const line of lines){ + if(displayLineNumber){ + console.log(`${String(lineNumber).padStart(6, ' ')} ${line}`); + lineNumber++; + } else if (displayNonEmptyLineNumber) { + if (line.trim()) { + console.log(`${String(lineNumber).padStart(6, ' ')} ${line}`); + lineNumber++; + } else { + console.log(line); + + } + } + } +} \ No newline at end of file diff --git a/implement-shell-tools/package-lock.json b/implement-shell-tools/package-lock.json new file mode 100644 index 00000000..6f585d45 --- /dev/null +++ b/implement-shell-tools/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "implement-shell-tools", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "commander": "^14.0.1" + } + }, + "node_modules/commander": { + "version": "14.0.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-14.0.1.tgz", + "integrity": "sha512-2JkV3gUZUVrbNA+1sjBOYLsMZ5cEEl8GTFP2a4AVz5hvasAMCQ1D2l2le/cX+pV4N6ZU17zjUahLpIXRrnWL8A==", + "license": "MIT", + "engines": { + "node": ">=20" + } + } + } +} From e00a37e8d90670f38cab73292c6608735c86e987 Mon Sep 17 00:00:00 2001 From: Rihanna P <114884466+RihannaP@users.noreply.github.com> Date: Wed, 22 Oct 2025 00:49:32 +0100 Subject: [PATCH 2/4] Implement ls command --- implement-shell-tools/ls/ls.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 implement-shell-tools/ls/ls.js diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 00000000..f8b8eacb --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,29 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .name("ls command") + .description("Implementing 'ls' command") + .option("-1", "list one file per line") + .option("-a", "include hidden files") + .argument("[directory]", "Directory to list"); + +program.parse(); + +const directory = program.args[0] || "."; //current directory as a defult if no directory provided +const allFiles = program.opts().a; +const listPerLine = program.opts()["1"]; + + + +const files = await fs.readdir(directory); +const visibleFiles = allFiles ? files : files.filter(file => !file.startsWith(".")); + +if (listPerLine){ + for(const file of visibleFiles){ + console.log(file) + } +}else{ + console.log(visibleFiles.join(" ")) + } + From 05cf39109618b5ae1aa5ab2b9b15ecd33e0f97c5 Mon Sep 17 00:00:00 2001 From: Rihanna P <114884466+RihannaP@users.noreply.github.com> Date: Wed, 22 Oct 2025 01:13:56 +0100 Subject: [PATCH 3/4] Implement wc command --- implement-shell-tools/wc/wc.js | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 implement-shell-tools/wc/wc.js diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 00000000..eef911ad --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,57 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; +import process from "node:process"; + +program + .name("wc command") + .description("Implementing 'wc' command") + .option("-l", "show line count") + .option("-w", "show word count") + .option("-c", "show character count") + .argument("", "files to read"); + +program.parse(); + +const paths = program.args; +const options = program.opts(); + + +function formatCounts(lines, words, chars, options) { + let result = ""; + + if (options.l || options.w || options.c) { + if (options.l) result += `${lines}L `; + if (options.w) result += `${words}W `; + if (options.c) result += `${chars}Char `; + } else { + result += `${lines}L ${words}W ${chars}Char `; + } + + return result; +} + +let totalLines = 0; +let totalWords = 0; +let totalChars = 0; + +for (const path of paths) { + const content = await fs.readFile(path, "utf-8"); + + const lineCount = (content.match(/\n/g) || []).length; + const wordCount = content.trim().split(/\s+/).length; + const charCount = content.length; + + totalLines += lineCount; + totalWords += wordCount; + totalChars += charCount; + + const output = formatCounts(lineCount, wordCount, charCount, options); + + console.log(`${output}${path}`); +} + +if (paths.length > 1) { + const totalOutput = formatCounts(totalLines, totalWords, totalChars, options); + + console.log(`${totalOutput}total`); +} \ No newline at end of file From cbcc184c6b9c66c81865d180239c2b9562d682b7 Mon Sep 17 00:00:00 2001 From: Rihanna P <114884466+RihannaP@users.noreply.github.com> Date: Wed, 29 Oct 2025 14:26:12 +0000 Subject: [PATCH 4/4] fix minor bugs --- implement-shell-tools/ls/ls.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js index f8b8eacb..c3d4c082 100644 --- a/implement-shell-tools/ls/ls.js +++ b/implement-shell-tools/ls/ls.js @@ -20,10 +20,10 @@ const files = await fs.readdir(directory); const visibleFiles = allFiles ? files : files.filter(file => !file.startsWith(".")); if (listPerLine){ - for(const file of visibleFiles){ - console.log(file) + for (const file of visibleFiles){ + console.log(file) } -}else{ +} else { console.log(visibleFiles.join(" ")) }