From f929943a25bb80f53d095a79d9275d2ea281f085 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Fri, 21 Nov 2025 14:18:54 +0000 Subject: [PATCH 1/8] Create basic mycat.js --- implement-shell-tools/cat/myCat.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 implement-shell-tools/cat/myCat.js diff --git a/implement-shell-tools/cat/myCat.js b/implement-shell-tools/cat/myCat.js new file mode 100644 index 000000000..fad4671e9 --- /dev/null +++ b/implement-shell-tools/cat/myCat.js @@ -0,0 +1,9 @@ +import fs from "node:fs"; + +const args = process.argv.slice(2); +const files = args; + +for (const filename of files) { + const content = fs.readFileSync(filename, "utf-8"); + process.stdout.write(content); +} From 2691eee439e243fe01de0d3711506fbd6526fa79 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Sat, 22 Nov 2025 14:37:50 +0000 Subject: [PATCH 2/8] Changed basic and addedn flan -n and -b --- implement-shell-tools/cat/myCat.js | 40 ++++++++++++++++++++++--- implement-shell-tools/package-lock.json | 21 +++++++++++++ implement-shell-tools/package.json | 6 ++++ 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 implement-shell-tools/package-lock.json create mode 100644 implement-shell-tools/package.json diff --git a/implement-shell-tools/cat/myCat.js b/implement-shell-tools/cat/myCat.js index fad4671e9..d9876d763 100644 --- a/implement-shell-tools/cat/myCat.js +++ b/implement-shell-tools/cat/myCat.js @@ -1,9 +1,41 @@ -import fs from "node:fs"; +import { program } from "commander"; +import { promises as fs } from "node:fs"; +import process from "node:process"; -const args = process.argv.slice(2); +program + .name("myCat") + .description("Simple file viewer") + .option("-n", "Number all output lines") + .option("-b", "Number non-blank output lines") + .argument("", "One or more file paths to show"); + +program.parse(); + +const args = program.args; const files = args; +const opts = program.opts(); +let lineNumber = 1; for (const filename of files) { - const content = fs.readFileSync(filename, "utf-8"); - process.stdout.write(content); + const content = await fs.readFile(filename, "utf-8"); + + if (opts.n) { + const lines = content.split("\n"); + + for (const line of lines) { + console.log(lineNumber + " " + line); + lineNumber++; + } + } else if (opts.b) { + const lines = content.split("\n"); + + for (const line of lines) { + if (line.trim() !== "") { + console.log(lineNumber + " " + line); + lineNumber++; + } + } + } else { + console.log(content); + } } diff --git a/implement-shell-tools/package-lock.json b/implement-shell-tools/package-lock.json new file mode 100644 index 000000000..09b3f387a --- /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.2" + } + }, + "node_modules/commander": { + "version": "14.0.2", + "resolved": "https://registry.npmjs.org/commander/-/commander-14.0.2.tgz", + "integrity": "sha512-TywoWNNRbhoD0BXs1P3ZEScW8W5iKrnbithIl0YH+uCmBd0QpPOA8yc82DS3BIE5Ma6FnBVUsJ7wVUDz4dvOWQ==", + "license": "MIT", + "engines": { + "node": ">=20" + } + } + } +} diff --git a/implement-shell-tools/package.json b/implement-shell-tools/package.json new file mode 100644 index 000000000..76dcd3f7a --- /dev/null +++ b/implement-shell-tools/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "dependencies": { + "commander": "^14.0.2" + } +} From 176b4d5f287905b1808354caaf87432fc95f216d Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Tue, 25 Nov 2025 11:50:59 +0000 Subject: [PATCH 3/8] Ls --- implement-shell-tools/ls/myLs.js | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 implement-shell-tools/ls/myLs.js diff --git a/implement-shell-tools/ls/myLs.js b/implement-shell-tools/ls/myLs.js new file mode 100644 index 000000000..afc59c385 --- /dev/null +++ b/implement-shell-tools/ls/myLs.js @@ -0,0 +1,36 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; +import process from "node:process"; + +program + .name("myLs") + .description("my ls clone") + .option("-1", "one entry per line") + .option("-a", "show hidden files") + .argument("[paths...]", "file or directory paths"); + +program.parse(); + +const opts = program.opts(); +let paths = program.args; + +if (paths.length === 0) { + paths = ["."]; +} + +for (const path of paths) { + const entries = await fs.readdir(path); + + for (const file of entries) { + if (!opts.a && file.startsWith(".")) { + continue; + } + + if (opts["1"]) { + console.log(file); + } else { + console.log(file + " "); + } + } + +} From 9f8b68c8ddbdeb59b1133ecb15496a54854a7a41 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Tue, 25 Nov 2025 13:21:29 +0000 Subject: [PATCH 4/8] My Wc --- implement-shell-tools/wc/MyWc.js | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 implement-shell-tools/wc/MyWc.js diff --git a/implement-shell-tools/wc/MyWc.js b/implement-shell-tools/wc/MyWc.js new file mode 100644 index 000000000..a84fe7654 --- /dev/null +++ b/implement-shell-tools/wc/MyWc.js @@ -0,0 +1,68 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; +import process from "node:process"; + +program + .name("myLs") + .description("my ls clone") + .option("-l", "line count") + .option("-w", "words count") + .option("-c", "character count") + .option("-s", "character count without spaces") + .argument("[paths...]", "file or directory paths"); + +program.parse(); + +const opts = program.opts(); +let files = program.args; + +if (files.length === 0) { + files = ["."]; +} + +let totalLines = 0; +let totalWords = 0; + +if (opts.l) { + for (const file of files) { + const content = await fs.readFile(file, "utf-8"); + console.log(content); + const lineCount = content.split("\n").length; + + totalLines += lineCount; + } + console.log("Lines:", totalLines); +} +if (opts.w) { + for (const file of files) { + const content = await fs.readFile(file, "utf-8"); + console.log(content); + + const wordCount = content.trim().split(/\s+/).length; + totalWords += wordCount; + } + console.log("Total words:", totalWords); +} +if (opts.c) { + let totalChars = 0; + for (const file of files) { + const content = await fs.readFile(file, "utf-8"); + + totalChars += content.trim().length; + // const charList = content.trim().split(/\s+/); + // for (const char of charList) { + // totalChars += char.length; + // } + } + console.log("Total characters:", totalChars); +} + +if (opts.s) { + let totalCharsNoSpaces = 0; + for (const file of files) { + const content = await fs.readFile(file, "utf-8"); + const withoutSpaces = content.replace(/\s/g, ""); + totalCharsNoSpaces += withoutSpaces.length; + } + console.log("Total characters without spaces:", totalCharsNoSpaces); +} From 66e8b67bcaac7eb94e12841804d6dc002fb618e6 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Tue, 25 Nov 2025 13:26:13 +0000 Subject: [PATCH 5/8] Edit myCat --- implement-shell-tools/cat/myCat.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/implement-shell-tools/cat/myCat.js b/implement-shell-tools/cat/myCat.js index d9876d763..6368db616 100644 --- a/implement-shell-tools/cat/myCat.js +++ b/implement-shell-tools/cat/myCat.js @@ -11,8 +11,7 @@ program program.parse(); -const args = program.args; -const files = args; +const files = program.args; const opts = program.opts(); let lineNumber = 1; From 830d1979f779e43467e9509228b346df5e07ff81 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Tue, 17 Feb 2026 15:54:00 +0000 Subject: [PATCH 6/8] Delete repetition lines --- .gitignore | 3 +++ implement-shell-tools/cat/myCat.js | 4 +--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e64..e7d034995 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ node_modules +.DS_Store +__pycache__/ +*.pyc \ No newline at end of file diff --git a/implement-shell-tools/cat/myCat.js b/implement-shell-tools/cat/myCat.js index 6368db616..70301b350 100644 --- a/implement-shell-tools/cat/myCat.js +++ b/implement-shell-tools/cat/myCat.js @@ -1,6 +1,5 @@ import { program } from "commander"; import { promises as fs } from "node:fs"; -import process from "node:process"; program .name("myCat") @@ -17,16 +16,15 @@ let lineNumber = 1; for (const filename of files) { const content = await fs.readFile(filename, "utf-8"); + const lines = content.split("\n"); if (opts.n) { - const lines = content.split("\n"); for (const line of lines) { console.log(lineNumber + " " + line); lineNumber++; } } else if (opts.b) { - const lines = content.split("\n"); for (const line of lines) { if (line.trim() !== "") { From 1160111a8992cfe2a831f73d2cb1fb398b807ff9 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Tue, 17 Feb 2026 16:06:02 +0000 Subject: [PATCH 7/8] Change param --- implement-shell-tools/ls/myLs.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/implement-shell-tools/ls/myLs.js b/implement-shell-tools/ls/myLs.js index afc59c385..05e0777db 100644 --- a/implement-shell-tools/ls/myLs.js +++ b/implement-shell-tools/ls/myLs.js @@ -1,11 +1,10 @@ import { program } from "commander"; import { promises as fs } from "node:fs"; -import process from "node:process"; program .name("myLs") .description("my ls clone") - .option("-1", "one entry per line") + .option("-1, --one-per-line", "one entry per line") .option("-a", "show hidden files") .argument("[paths...]", "file or directory paths"); @@ -19,14 +18,14 @@ if (paths.length === 0) { } for (const path of paths) { - const entries = await fs.readdir(path); + const directoryItems = await fs.readdir(path); - for (const file of entries) { + for (const file of directoryItems) { if (!opts.a && file.startsWith(".")) { continue; } - if (opts["1"]) { + if (opts.onePerLine) { console.log(file); } else { console.log(file + " "); From b6fdd207a9c0933979dfb7a70a439eb2088f889e Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Tue, 17 Feb 2026 16:18:49 +0000 Subject: [PATCH 8/8] Edit and removed unnecessary lines. --- implement-shell-tools/wc/MyWc.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/implement-shell-tools/wc/MyWc.js b/implement-shell-tools/wc/MyWc.js index a84fe7654..981c2b089 100644 --- a/implement-shell-tools/wc/MyWc.js +++ b/implement-shell-tools/wc/MyWc.js @@ -1,10 +1,9 @@ import { program } from "commander"; import { promises as fs } from "node:fs"; -import process from "node:process"; program - .name("myLs") - .description("my ls clone") + .name("myWc") + .description("my wc clone") .option("-l", "line count") .option("-w", "words count") .option("-c", "character count") @@ -26,7 +25,6 @@ let totalWords = 0; if (opts.l) { for (const file of files) { const content = await fs.readFile(file, "utf-8"); - console.log(content); const lineCount = content.split("\n").length; totalLines += lineCount; @@ -36,7 +34,6 @@ if (opts.l) { if (opts.w) { for (const file of files) { const content = await fs.readFile(file, "utf-8"); - console.log(content); const wordCount = content.trim().split(/\s+/).length; totalWords += wordCount; @@ -49,10 +46,7 @@ if (opts.c) { const content = await fs.readFile(file, "utf-8"); totalChars += content.trim().length; - // const charList = content.trim().split(/\s+/); - // for (const char of charList) { - // totalChars += char.length; - // } + } console.log("Total characters:", totalChars); }