From 268ea9863308f6d5f6861fa90fd749054887b3ea Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Tue, 25 Nov 2025 18:40:52 +0000 Subject: [PATCH 1/5] cat implementation --- implement-shell-tools/cat/cat.js | 40 +++++++ implement-shell-tools/cat/package-lock.json | 118 ++++++++++++++++++++ implement-shell-tools/cat/package.json | 21 ++++ 3 files changed, 179 insertions(+) create mode 100644 implement-shell-tools/cat/cat.js create mode 100644 implement-shell-tools/cat/package-lock.json create mode 100644 implement-shell-tools/cat/package.json diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 00000000..75853869 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,40 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .name("cat command") + .description("create cat command") + .option("-n", "Number all lines") + .option("-b", "Number non-empty lines") + .argument("", "File paths"); + +program.parse(); +const paths = program.args; +const number = program.opts().n; +const nonEmptyLine = program.opts().b; + +let lineNumber = 1; + +for (const path of paths) { + try { + const read = await fs.readFile(path, "utf-8"); + const lines = read.split("\n"); + for (let i of lines) { + if (number) { + console.log(`${String(lineNumber).padStart(6, " ")} ${i}`); + lineNumber++; + } else if (nonEmptyLine) { + if (i.trim() !== "") { + console.log(`${String(lineNumber).padStart(6, " ")} ${i}`); + lineNumber++; + } else { + console.log(i); + } + } else { + console.log(i); + } + } + } catch (err) { + console.error(`File could not read: ${path}`); + } +} diff --git a/implement-shell-tools/cat/package-lock.json b/implement-shell-tools/cat/package-lock.json new file mode 100644 index 00000000..6f80a2a7 --- /dev/null +++ b/implement-shell-tools/cat/package-lock.json @@ -0,0 +1,118 @@ +{ + "name": "cat", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "cat", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "commander": "^14.0.2", + "glob": "^13.0.0", + "lru-cache": "^11.2.2", + "minimatch": "^10.1.1", + "minipass": "^7.1.2", + "path-scurry": "^2.0.1" + }, + "devDependencies": {} + }, + "node_modules/@isaacs/balanced-match": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/@isaacs/balanced-match/-/balanced-match-4.0.1.tgz", + "integrity": "sha512-yzMTt9lEb8Gv7zRioUilSglI0c0smZ9k5D65677DLWLtWJaXIS3CqcGyUFByYKlnUj6TkjLVs54fBl6+TiGQDQ==", + "license": "MIT", + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/@isaacs/brace-expansion": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/@isaacs/brace-expansion/-/brace-expansion-5.0.0.tgz", + "integrity": "sha512-ZT55BDLV0yv0RBm2czMiZ+SqCGO7AvmOM3G/w2xhVPH+te0aKgFjmBvGlL1dH+ql2tgGO3MVrbb3jCKyvpgnxA==", + "license": "MIT", + "dependencies": { + "@isaacs/balanced-match": "^4.0.1" + }, + "engines": { + "node": "20 || >=22" + } + }, + "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" + } + }, + "node_modules/glob": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-13.0.0.tgz", + "integrity": "sha512-tvZgpqk6fz4BaNZ66ZsRaZnbHvP/jG3uKJvAZOwEVUL4RTA5nJeeLYfyN9/VA8NX/V3IBG+hkeuGpKjvELkVhA==", + "license": "BlueOak-1.0.0", + "dependencies": { + "minimatch": "^10.1.1", + "minipass": "^7.1.2", + "path-scurry": "^2.0.0" + }, + "engines": { + "node": "20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/lru-cache": { + "version": "11.2.2", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.2.2.tgz", + "integrity": "sha512-F9ODfyqML2coTIsQpSkRHnLSZMtkU8Q+mSfcaIyKwy58u+8k5nvAYeiNhsyMARvzNcXJ9QfWVrcPsC9e9rAxtg==", + "license": "ISC", + "engines": { + "node": "20 || >=22" + } + }, + "node_modules/minimatch": { + "version": "10.1.1", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.1.1.tgz", + "integrity": "sha512-enIvLvRAFZYXJzkCYG5RKmPfrFArdLv+R+lbQ53BmIMLIry74bjKzX6iHAm8WYamJkhSSEabrWN5D97XnKObjQ==", + "license": "BlueOak-1.0.0", + "dependencies": { + "@isaacs/brace-expansion": "^5.0.0" + }, + "engines": { + "node": "20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, + "node_modules/minipass": { + "version": "7.1.2", + "resolved": "https://registry.npmjs.org/minipass/-/minipass-7.1.2.tgz", + "integrity": "sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw==", + "license": "ISC", + "engines": { + "node": ">=16 || 14 >=14.17" + } + }, + "node_modules/path-scurry": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-2.0.1.tgz", + "integrity": "sha512-oWyT4gICAu+kaA7QWk/jvCHWarMKNs6pXOGWKDTr7cw4IGcUbW+PeTfbaQiLGheFRpjo6O9J0PmyMfQPjH71oA==", + "license": "BlueOak-1.0.0", + "dependencies": { + "lru-cache": "^11.0.0", + "minipass": "^7.1.2" + }, + "engines": { + "node": "20 || >=22" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + } + } +} diff --git a/implement-shell-tools/cat/package.json b/implement-shell-tools/cat/package.json new file mode 100644 index 00000000..ba939f50 --- /dev/null +++ b/implement-shell-tools/cat/package.json @@ -0,0 +1,21 @@ +{ + "name": "cat", + "version": "1.0.0", + "description": "You should already be familiar with the `cat` command line tool.", + "main": "cat.js", + "type": "module", + "dependencies": { + "commander": "^14.0.2", + "glob": "^13.0.0", + "lru-cache": "^11.2.2", + "minimatch": "^10.1.1", + "minipass": "^7.1.2", + "path-scurry": "^2.0.1" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} From 07dd2185dbed92bc67c66e5fe96c05d8ce8b68af Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Mon, 1 Dec 2025 22:17:21 +0000 Subject: [PATCH 2/5] implement ls command --- implement-shell-tools/cat/package-lock.json | 118 -------------------- implement-shell-tools/cat/package.json | 21 ---- implement-shell-tools/ls/ls.js | 43 +++++++ implement-shell-tools/package-lock.json | 25 +++++ implement-shell-tools/package.json | 16 +++ 5 files changed, 84 insertions(+), 139 deletions(-) delete mode 100644 implement-shell-tools/cat/package-lock.json delete mode 100644 implement-shell-tools/cat/package.json create mode 100644 implement-shell-tools/ls/ls.js create mode 100644 implement-shell-tools/package-lock.json create mode 100644 implement-shell-tools/package.json diff --git a/implement-shell-tools/cat/package-lock.json b/implement-shell-tools/cat/package-lock.json deleted file mode 100644 index 6f80a2a7..00000000 --- a/implement-shell-tools/cat/package-lock.json +++ /dev/null @@ -1,118 +0,0 @@ -{ - "name": "cat", - "version": "1.0.0", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "cat", - "version": "1.0.0", - "license": "ISC", - "dependencies": { - "commander": "^14.0.2", - "glob": "^13.0.0", - "lru-cache": "^11.2.2", - "minimatch": "^10.1.1", - "minipass": "^7.1.2", - "path-scurry": "^2.0.1" - }, - "devDependencies": {} - }, - "node_modules/@isaacs/balanced-match": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/@isaacs/balanced-match/-/balanced-match-4.0.1.tgz", - "integrity": "sha512-yzMTt9lEb8Gv7zRioUilSglI0c0smZ9k5D65677DLWLtWJaXIS3CqcGyUFByYKlnUj6TkjLVs54fBl6+TiGQDQ==", - "license": "MIT", - "engines": { - "node": "20 || >=22" - } - }, - "node_modules/@isaacs/brace-expansion": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/@isaacs/brace-expansion/-/brace-expansion-5.0.0.tgz", - "integrity": "sha512-ZT55BDLV0yv0RBm2czMiZ+SqCGO7AvmOM3G/w2xhVPH+te0aKgFjmBvGlL1dH+ql2tgGO3MVrbb3jCKyvpgnxA==", - "license": "MIT", - "dependencies": { - "@isaacs/balanced-match": "^4.0.1" - }, - "engines": { - "node": "20 || >=22" - } - }, - "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" - } - }, - "node_modules/glob": { - "version": "13.0.0", - "resolved": "https://registry.npmjs.org/glob/-/glob-13.0.0.tgz", - "integrity": "sha512-tvZgpqk6fz4BaNZ66ZsRaZnbHvP/jG3uKJvAZOwEVUL4RTA5nJeeLYfyN9/VA8NX/V3IBG+hkeuGpKjvELkVhA==", - "license": "BlueOak-1.0.0", - "dependencies": { - "minimatch": "^10.1.1", - "minipass": "^7.1.2", - "path-scurry": "^2.0.0" - }, - "engines": { - "node": "20 || >=22" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - }, - "node_modules/lru-cache": { - "version": "11.2.2", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-11.2.2.tgz", - "integrity": "sha512-F9ODfyqML2coTIsQpSkRHnLSZMtkU8Q+mSfcaIyKwy58u+8k5nvAYeiNhsyMARvzNcXJ9QfWVrcPsC9e9rAxtg==", - "license": "ISC", - "engines": { - "node": "20 || >=22" - } - }, - "node_modules/minimatch": { - "version": "10.1.1", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.1.1.tgz", - "integrity": "sha512-enIvLvRAFZYXJzkCYG5RKmPfrFArdLv+R+lbQ53BmIMLIry74bjKzX6iHAm8WYamJkhSSEabrWN5D97XnKObjQ==", - "license": "BlueOak-1.0.0", - "dependencies": { - "@isaacs/brace-expansion": "^5.0.0" - }, - "engines": { - "node": "20 || >=22" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - }, - "node_modules/minipass": { - "version": "7.1.2", - "resolved": "https://registry.npmjs.org/minipass/-/minipass-7.1.2.tgz", - "integrity": "sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw==", - "license": "ISC", - "engines": { - "node": ">=16 || 14 >=14.17" - } - }, - "node_modules/path-scurry": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-2.0.1.tgz", - "integrity": "sha512-oWyT4gICAu+kaA7QWk/jvCHWarMKNs6pXOGWKDTr7cw4IGcUbW+PeTfbaQiLGheFRpjo6O9J0PmyMfQPjH71oA==", - "license": "BlueOak-1.0.0", - "dependencies": { - "lru-cache": "^11.0.0", - "minipass": "^7.1.2" - }, - "engines": { - "node": "20 || >=22" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - } - } -} diff --git a/implement-shell-tools/cat/package.json b/implement-shell-tools/cat/package.json deleted file mode 100644 index ba939f50..00000000 --- a/implement-shell-tools/cat/package.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "cat", - "version": "1.0.0", - "description": "You should already be familiar with the `cat` command line tool.", - "main": "cat.js", - "type": "module", - "dependencies": { - "commander": "^14.0.2", - "glob": "^13.0.0", - "lru-cache": "^11.2.2", - "minimatch": "^10.1.1", - "minipass": "^7.1.2", - "path-scurry": "^2.0.1" - }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [], - "author": "", - "license": "ISC" -} diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 00000000..634fac12 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,43 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .name("ls command") + .description("create ls command") + .option("-1", "One entry per line") + .option("-a", "Show all files including hidden") + .argument("[paths...]", "File paths"); + +program.parse(); + +const opts = program.opts(); +const paths = program.args.length > 0 ? program.args : ["."]; +const onePerLine = opts["1"]; +const showAll = opts.a; + +for (const targetPath of paths) { + try { + let files = await fs.readdir(targetPath); + + if (!showAll) { + files = files.filter((f) => !f.startsWith(".")); + } + + + if (paths.length > 1) { + console.log(`${targetPath}:`); + } + + if (onePerLine) { + for (const f of files) console.log(f); + } else { + console.log(files.join(" ")); + } + + if (paths.length > 1) { + console.log(); + } + } catch (err) { + console.error(`ls: cannot access '${targetPath}': ${err.message}`); + } +} diff --git a/implement-shell-tools/package-lock.json b/implement-shell-tools/package-lock.json new file mode 100644 index 00000000..38a22987 --- /dev/null +++ b/implement-shell-tools/package-lock.json @@ -0,0 +1,25 @@ +{ + "name": "implement-shell-tools", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "implement-shell-tools", + "version": "1.0.0", + "license": "ISC", + "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 00000000..e49468b7 --- /dev/null +++ b/implement-shell-tools/package.json @@ -0,0 +1,16 @@ +{ + "name": "implement-shell-tools", + "version": "1.0.0", + "description": "Your task is to re-implement shell tools you have used.", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "commander": "^14.0.2" + } +} From 90398dda812d0bc92de6647c110fb99bced9e782 Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Mon, 1 Dec 2025 22:17:54 +0000 Subject: [PATCH 3/5] implement wc command --- implement-shell-tools/wc/wc.js | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 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..0d0248b1 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,53 @@ + +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .option("-l", "Print only the line count") + .option("-w", "Print only the word count") + .option("-c", "Print only the byte count") + .argument("", "One or more file paths"); + +program.parse(); + +const opts = program.opts(); +const paths = program.args; + +async function wcFile(path) { + const content = await fs.readFile(path, "utf8"); + + const lines = content.split("\n").length; + const words = content.trim().split(/\s+/).filter(Boolean).length; + const bytes = Buffer.byteLength(content, "utf8"); + + return { lines, words, bytes }; +} + +function formatOutput(counts, filename, opts) { + if (opts.l) return `${counts.lines} ${filename}`; + if (opts.w) return `${counts.words} ${filename}`; + if (opts.c) return `${counts.bytes} ${filename}`; + + return `${counts.lines} ${counts.words} ${counts.bytes} ${filename}`; +} + +async function main() { + let total = { lines: 0, words: 0, bytes: 0 }; + let multipleFiles = paths.length > 1; + + for (const path of paths) { + const counts = await wcFile(path); + + total.lines += counts.lines; + total.words += counts.words; + total.bytes += counts.bytes; + + console.log(formatOutput(counts, path, opts)); + } + + if (multipleFiles) { + console.log(formatOutput(total, "total", opts)); + } +} + +main(); From a90ceffd046b02dd9f86f42d921d47c5a5710a95 Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Tue, 20 Jan 2026 12:15:04 +0000 Subject: [PATCH 4/5] Refactor: remove repeated code and nested ifs --- implement-shell-tools/cat/cat.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index 75853869..8b694390 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -14,22 +14,20 @@ const number = program.opts().n; const nonEmptyLine = program.opts().b; let lineNumber = 1; - + for (const path of paths) { try { const read = await fs.readFile(path, "utf-8"); const lines = read.split("\n"); + + function printNumLine(line) { + console.log(`${String(lineNumber).padStart(6, " ")} ${line}`); + lineNumber++; + } + for (let i of lines) { - if (number) { - console.log(`${String(lineNumber).padStart(6, " ")} ${i}`); - lineNumber++; - } else if (nonEmptyLine) { - if (i.trim() !== "") { - console.log(`${String(lineNumber).padStart(6, " ")} ${i}`); - lineNumber++; - } else { - console.log(i); - } + if (number || (nonEmptyLine && i.trim() !== "")) { + printNumLine(i); } else { console.log(i); } From 72e28df07d7fd2112dfb4cabbb806ee57782b8ca Mon Sep 17 00:00:00 2001 From: Fatma Degirmenci Date: Tue, 20 Jan 2026 12:26:56 +0000 Subject: [PATCH 5/5] Refactor: align numbers in columns (padStart()) --- implement-shell-tools/wc/wc.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js index 0d0248b1..9df12a1f 100644 --- a/implement-shell-tools/wc/wc.js +++ b/implement-shell-tools/wc/wc.js @@ -1,4 +1,3 @@ - import { program } from "commander"; import { promises as fs } from "node:fs"; @@ -28,7 +27,9 @@ function formatOutput(counts, filename, opts) { if (opts.w) return `${counts.words} ${filename}`; if (opts.c) return `${counts.bytes} ${filename}`; - return `${counts.lines} ${counts.words} ${counts.bytes} ${filename}`; + return `${String(counts.lines).padStart(8)} ${String(counts.words).padStart( + 8 + )} ${String(counts.bytes).padStart(8)} ${filename}`; } async function main() {