From 8076ca5c303308376121b62efa1c553d883a8c1e Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 30 Nov 2026 12:00:14 +0000 Subject: [PATCH 01/10] add default setup for cat --- implement-shell-tools/cat/cat.js | 7 ++++++ implement-shell-tools/cat/package-lock.json | 25 +++++++++++++++++++++ implement-shell-tools/cat/package.json | 15 +++++++++++++ 3 files changed, 47 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 000000000..65e2194fa --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,7 @@ +import { program } from "commander"; + +program.action(() => { + console.log("hello world!"); +}); + +program.parse(); diff --git a/implement-shell-tools/cat/package-lock.json b/implement-shell-tools/cat/package-lock.json new file mode 100644 index 000000000..c75cf85fd --- /dev/null +++ b/implement-shell-tools/cat/package-lock.json @@ -0,0 +1,25 @@ +{ + "name": "cat", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "cat", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "commander": "^15.1.0" + } + }, + "node_modules/commander": { + "version": "15.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-15.1.0.tgz", + "integrity": "sha512-cgnQ88TRrWcC7pVq46RzJVRMktu3W2RcbnoidnfBsl1bmDCBwJS/udcIZpKoCrbAYYaCqsz1yZCVIgbIaU3ddQ==", + "license": "MIT", + "engines": { + "node": ">=22.12.0" + } + } + } +} diff --git a/implement-shell-tools/cat/package.json b/implement-shell-tools/cat/package.json new file mode 100644 index 000000000..8916cb6e2 --- /dev/null +++ b/implement-shell-tools/cat/package.json @@ -0,0 +1,15 @@ +{ + "name": "cat", + "version": "1.0.0", + "description": "JS implementation of the `cat` command line tool.", + "license": "ISC", + "author": "", + "type": "module", + "main": "cat.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "dependencies": { + "commander": "^15.1.0" + } +} From 470fcdea32d9741dd781c961952c062d812d7e57 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 30 Nov 2026 12:35:26 +0000 Subject: [PATCH 02/10] add cat files output --- implement-shell-tools/cat/cat.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index 65e2194fa..ae14edfc5 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -1,7 +1,14 @@ import { program } from "commander"; +import { promises as fs } from "node:fs"; -program.action(() => { - console.log("hello world!"); -}); +program + .argument("", "List of files to concatenate") + .name("cat") + .description("Concatenate FILE(s) to standard output."); program.parse(); + +for (const file of program.args) { + const data = await fs.readFile(file, "utf8"); + console.log(data); +} From 5d0af43ca1439c8dca34627e4d780c3eb8821302 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 30 Nov 2026 12:50:15 +0000 Subject: [PATCH 03/10] add -b and -n implementation --- implement-shell-tools/cat/cat.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index ae14edfc5..f01fdf583 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -4,11 +4,31 @@ import { promises as fs } from "node:fs"; program .argument("", "List of files to concatenate") .name("cat") - .description("Concatenate FILE(s) to standard output."); + .description("Concatenate FILE(s) to standard output.") + .option("-n", "Number all input lines") + .option("-b", "Number non-blank input lines"); program.parse(); +const options = program.opts(); +const { n, b } = options; + +let catAllData = ""; + for (const file of program.args) { - const data = await fs.readFile(file, "utf8"); - console.log(data); + let data = await fs.readFile(file, "utf8"); + catAllData += data; +} + +let dataArray = catAllData.split("\n"); + +if (b) { + dataArray = dataArray.map((line, index) => + line ? `${index + 1}\t${line}` : line, + ); +} else if (n) { + dataArray = dataArray.map((line, index) => `${index + 1}\t${line}`); } + +dataArray = dataArray.join("\n"); +process.stdout.write(dataArray); From adc770b32d44a3d9cce9dfb864cd6e8288caeea7 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 30 Nov 2026 12:54:45 +0000 Subject: [PATCH 04/10] fix numbering of non empty lines --- implement-shell-tools/cat/cat.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index f01fdf583..c6d94e0fe 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -23,8 +23,9 @@ for (const file of program.args) { let dataArray = catAllData.split("\n"); if (b) { - dataArray = dataArray.map((line, index) => - line ? `${index + 1}\t${line}` : line, + let lineNumber = 1; + dataArray = dataArray.map((line) => + line ? `${lineNumber++}\t${line}` : line, ); } else if (n) { dataArray = dataArray.map((line, index) => `${index + 1}\t${line}`); From 4224bcdd07328e15df1157bca4e4b7b0f776d6e1 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 30 Nov 2026 13:10:45 +0000 Subject: [PATCH 05/10] add ls initial setup --- implement-shell-tools/ls/ls.js | 17 +++++++++++++++ implement-shell-tools/ls/package-lock.json | 25 ++++++++++++++++++++++ implement-shell-tools/ls/package.json | 15 +++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 implement-shell-tools/ls/ls.js create mode 100644 implement-shell-tools/ls/package-lock.json create mode 100644 implement-shell-tools/ls/package.json diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..978457d67 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,17 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .argument("[path]", "Path to list", ".") + .name("ls") + .description( + "List information about the FILEs (the current directory by default).", + ) + .option("-1, --one", "List one file per line") + .option("-a, --all", "Do not ignore entries starting with ."); + +program.parse(); + +const path = program.args.length ? program.args : "."; +const { one, all } = program.opts(); +console.log(path, one, all); diff --git a/implement-shell-tools/ls/package-lock.json b/implement-shell-tools/ls/package-lock.json new file mode 100644 index 000000000..16d106dd8 --- /dev/null +++ b/implement-shell-tools/ls/package-lock.json @@ -0,0 +1,25 @@ +{ + "name": "ls", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "ls", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "commander": "^15.1.0" + } + }, + "node_modules/commander": { + "version": "15.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-15.1.0.tgz", + "integrity": "sha512-cgnQ88TRrWcC7pVq46RzJVRMktu3W2RcbnoidnfBsl1bmDCBwJS/udcIZpKoCrbAYYaCqsz1yZCVIgbIaU3ddQ==", + "license": "MIT", + "engines": { + "node": ">=22.12.0" + } + } + } +} diff --git a/implement-shell-tools/ls/package.json b/implement-shell-tools/ls/package.json new file mode 100644 index 000000000..5b98a4502 --- /dev/null +++ b/implement-shell-tools/ls/package.json @@ -0,0 +1,15 @@ +{ + "name": "ls", + "version": "1.0.0", + "description": "Implementation of the `ls` shell command", + "license": "ISC", + "author": "", + "type": "module", + "main": "ls.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "dependencies": { + "commander": "^15.1.0" + } +} From 720b1c55310450011b7f8374d217e7fe80c5eb41 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 30 Nov 2026 13:23:37 +0000 Subject: [PATCH 06/10] add implement flags and dir list --- implement-shell-tools/ls/ls.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js index 978457d67..4273abe53 100644 --- a/implement-shell-tools/ls/ls.js +++ b/implement-shell-tools/ls/ls.js @@ -2,7 +2,7 @@ import { program } from "commander"; import { promises as fs } from "node:fs"; program - .argument("[path]", "Path to list", ".") + .argument("[paths...]", "Paths to list files.", ".") .name("ls") .description( "List information about the FILEs (the current directory by default).", @@ -14,4 +14,18 @@ program.parse(); const path = program.args.length ? program.args : "."; const { one, all } = program.opts(); -console.log(path, one, all); + +async function main() { + let fileNames = await fs.readdir(path); + if (all) { + fileNames.unshift(".", ".."); + } else { + fileNames = fileNames.filter((name) => !name.startsWith(".")); + } + console.log(fileNames.join(one ? "\n" : " ")); +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); From 64446048d008875066d86d9c245429a39a68ebd3 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 30 Nov 2026 13:42:15 +0000 Subject: [PATCH 07/10] add multiple paths and sorting in ls --- implement-shell-tools/ls/ls.js | 56 +++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js index 4273abe53..5221a3f73 100644 --- a/implement-shell-tools/ls/ls.js +++ b/implement-shell-tools/ls/ls.js @@ -2,7 +2,7 @@ import { program } from "commander"; import { promises as fs } from "node:fs"; program - .argument("[paths...]", "Paths to list files.", ".") + .argument("[paths...]", "Paths to list files.", ["."]) .name("ls") .description( "List information about the FILEs (the current directory by default).", @@ -12,17 +12,57 @@ program program.parse(); -const path = program.args.length ? program.args : "."; +const paths = program.args.length ? program.args : ["."]; const { one, all } = program.opts(); +const sortNames = (a, b) => + a.localeCompare(b, undefined, { ignorePunctuation: true }); + async function main() { - let fileNames = await fs.readdir(path); - if (all) { - fileNames.unshift(".", ".."); - } else { - fileNames = fileNames.filter((name) => !name.startsWith(".")); + const files = []; + const dirs = []; + + for (const p of paths) { + const stats = await fs.stat(p); + if (stats.isDirectory()) { + dirs.push(p); + } else { + files.push(p); + } + } + + files.sort(sortNames); + dirs.sort(sortNames); + + const sep = one ? "\n" : " "; + + if (files.length > 0) { + console.log(files.join(sep)); + } + + for (let i = 0; i < dirs.length; i++) { + const dir = dirs[i]; + + if (files.length > 0 || i > 0) { + console.log(); + } + + if (paths.length > 1) { + console.log(`${dir}:`); + } + + let fileNames = await fs.readdir(dir); + if (all) { + fileNames.unshift(".", ".."); + } else { + fileNames = fileNames.filter((name) => !name.startsWith(".")); + } + fileNames.sort(sortNames); + + if (fileNames.length > 0) { + console.log(fileNames.join(sep)); + } } - console.log(fileNames.join(one ? "\n" : " ")); } main().catch((err) => { From 33f4f1bcd14c2e5470350389468e2512d6a792e4 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 30 Nov 2026 15:03:16 +0000 Subject: [PATCH 08/10] add wc implementation --- implement-shell-tools/wc/package-lock.json | 25 +++++++ implement-shell-tools/wc/package.json | 15 +++++ implement-shell-tools/wc/wc.js | 76 ++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 implement-shell-tools/wc/package-lock.json create mode 100644 implement-shell-tools/wc/package.json create mode 100644 implement-shell-tools/wc/wc.js diff --git a/implement-shell-tools/wc/package-lock.json b/implement-shell-tools/wc/package-lock.json new file mode 100644 index 000000000..a0de73d74 --- /dev/null +++ b/implement-shell-tools/wc/package-lock.json @@ -0,0 +1,25 @@ +{ + "name": "wc", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "wc", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "commander": "^15.1.0" + } + }, + "node_modules/commander": { + "version": "15.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-15.1.0.tgz", + "integrity": "sha512-cgnQ88TRrWcC7pVq46RzJVRMktu3W2RcbnoidnfBsl1bmDCBwJS/udcIZpKoCrbAYYaCqsz1yZCVIgbIaU3ddQ==", + "license": "MIT", + "engines": { + "node": ">=22.12.0" + } + } + } +} diff --git a/implement-shell-tools/wc/package.json b/implement-shell-tools/wc/package.json new file mode 100644 index 000000000..7ffa695a5 --- /dev/null +++ b/implement-shell-tools/wc/package.json @@ -0,0 +1,15 @@ +{ + "name": "wc", + "version": "1.0.0", + "description": "Implementation of the `wc` tool in NodeJS", + "license": "ISC", + "author": "", + "type": "module", + "main": "wc.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "dependencies": { + "commander": "^15.1.0" + } +} diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..431871e9b --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,76 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .argument("", "Files to count") + .name("wc") + .description("Print newline, word, and byte counts for each FILE.") + .option("-l, --lines", "Print the newline counts") + .option("-w, --words", "Print the word counts") + .option("-c, --bytes", "Print the byte counts"); + +program.parse(); + +const files = program.args; +const opts = program.opts(); + +const noFlags = !opts.lines && !opts.words && !opts.bytes; +const showLines = noFlags || Boolean(opts.lines); +const showWords = noFlags || Boolean(opts.words); +const showBytes = noFlags || Boolean(opts.bytes); + +function countBuffer(buffer) { + let lines = 0; + for (const byte of buffer) { + if (byte === 0x0a) lines++; + } + + const text = buffer.toString("utf8").trim(); + const words = text ? text.split(/\s+/).length : 0; + const bytes = buffer.length; + + return { lines, words, bytes }; +} + +function formatRow(counts, name, width) { + const cols = []; + if (showLines) cols.push(String(counts.lines).padStart(width, " ")); + if (showWords) cols.push(String(counts.words).padStart(width, " ")); + if (showBytes) cols.push(String(counts.bytes).padStart(width, " ")); + cols.push(name); + return cols.join(" "); +} + +async function main() { + const results = []; + const total = { lines: 0, words: 0, bytes: 0 }; + + for (const file of files) { + const buffer = await fs.readFile(file); + const counts = countBuffer(buffer); + results.push({ name: file, ...counts }); + total.lines += counts.lines; + total.words += counts.words; + total.bytes += counts.bytes; + } + + const columnCount = + Number(showLines) + Number(showWords) + Number(showBytes); + const width = + results.length === 1 && columnCount === 1 + ? 1 + : String(total.bytes).length; + + for (const result of results) { + console.log(formatRow(result, result.name, width)); + } + + if (results.length > 1) { + console.log(formatRow(total, "total", width)); + } +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); From 7fd7573fb9c6c4cdddcbd329d7f640ea727b6c62 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Mon, 30 Nov 2026 15:31:54 +0000 Subject: [PATCH 09/10] refactor cat implementation into main async function with error handling --- implement-shell-tools/cat/cat.js | 40 ++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index c6d94e0fe..27992c136 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -10,26 +10,32 @@ program program.parse(); -const options = program.opts(); -const { n, b } = options; +const files = program.args; +const { n, b } = program.opts(); -let catAllData = ""; +async function main() { + let catAllData = ""; -for (const file of program.args) { - let data = await fs.readFile(file, "utf8"); - catAllData += data; -} + for (const file of files) { + const data = await fs.readFile(file, "utf8"); + catAllData += data; + } + + let dataArray = catAllData.split("\n"); -let dataArray = catAllData.split("\n"); + if (b) { + let lineNumber = 1; + dataArray = dataArray.map((line) => + line ? `${lineNumber++}\t${line}` : line, + ); + } else if (n) { + dataArray = dataArray.map((line, index) => `${index + 1}\t${line}`); + } -if (b) { - let lineNumber = 1; - dataArray = dataArray.map((line) => - line ? `${lineNumber++}\t${line}` : line, - ); -} else if (n) { - dataArray = dataArray.map((line, index) => `${index + 1}\t${line}`); + process.stdout.write(dataArray.join("\n")); } -dataArray = dataArray.join("\n"); -process.stdout.write(dataArray); +main().catch((err) => { + console.error(err); + process.exit(1); +}); From fddd3114c2bc454a201dc7009315b97b358b00c8 Mon Sep 17 00:00:00 2001 From: Tomislav Dukez Date: Wed, 2 Dec 2026 12:50:49 +0000 Subject: [PATCH 10/10] fix cat for file finishing with an empty \n tag --- implement-shell-tools/cat/cat.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index 27992c136..f456dada3 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -21,7 +21,15 @@ async function main() { catAllData += data; } - let dataArray = catAllData.split("\n"); + const endsWithNewline = catAllData.endsWith("\n"); + let dataArray = catAllData === "" ? [] : catAllData.split("\n"); + + // A trailing newline produces a final empty element in the split array. + // Drop it so it isn't numbered as a phantom line; the newline is restored + // when the lines are joined back together. + if (endsWithNewline) { + dataArray.pop(); + } if (b) { let lineNumber = 1; @@ -32,7 +40,9 @@ async function main() { dataArray = dataArray.map((line, index) => `${index + 1}\t${line}`); } - process.stdout.write(dataArray.join("\n")); + process.stdout.write( + dataArray.join("\n") + (endsWithNewline ? "\n" : ""), + ); } main().catch((err) => {