From 56cac6c1fb5fce8475a2f70b7bee1d988d941caf Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Mon, 17 Nov 2025 14:13:51 +0000 Subject: [PATCH 01/15] Implement basic cat with no flags --- implement-shell-tools/cat/cat.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 implement-shell-tools/cat/cat.js diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..3fa63cd2a --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,17 @@ +import process from "node:process" +import {promises as fs} from "node:fs" + +const argv = process.argv.slice(2) + +if (argv.length != 1) { + console.error( + `Expected exactly 1 argument (a path) to be passed but got ${argv.length}.` + ); + process.exit(1); +} + +const path = argv[0] + + +const content = await fs.readFile(path, "utf-8") +console.log(content) \ No newline at end of file From 2e40fa0a2ffa87709733575bf230365b98cdfa69 Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Mon, 17 Nov 2025 14:21:20 +0000 Subject: [PATCH 02/15] Add prep folder to gitignore --- implement-shell-tools/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 implement-shell-tools/.gitignore diff --git a/implement-shell-tools/.gitignore b/implement-shell-tools/.gitignore new file mode 100644 index 000000000..cbaafe27b --- /dev/null +++ b/implement-shell-tools/.gitignore @@ -0,0 +1 @@ +prep/ From a0538b4025842e160a8b899c094ba92112d41b43 Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Mon, 17 Nov 2025 14:25:41 +0000 Subject: [PATCH 03/15] Add package.json and ignore prep folder --- implement-shell-tools/.gitignore | 1 + implement-shell-tools/package.json | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 implement-shell-tools/package.json diff --git a/implement-shell-tools/.gitignore b/implement-shell-tools/.gitignore index cbaafe27b..538b43a81 100644 --- a/implement-shell-tools/.gitignore +++ b/implement-shell-tools/.gitignore @@ -1 +1,2 @@ prep/ +prep/ diff --git a/implement-shell-tools/package.json b/implement-shell-tools/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/implement-shell-tools/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} From aaea89c9af1488ccdc4e298f2bd40629c7b7c3bd Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Mon, 17 Nov 2025 14:27:37 +0000 Subject: [PATCH 04/15] Ignore prep folder --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3c3629e64..cc8b55682 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +prep/ From afdea4aa63ee5c28c11841a1ff9789ff4111d774 Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Mon, 17 Nov 2025 15:13:03 +0000 Subject: [PATCH 05/15] Add commander dependency --- package-lock.json | 20 ++++++++++++++++++++ package.json | 6 ++++++ 2 files changed, 26 insertions(+) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..ada06fac6 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,20 @@ +{ + "name": "Module-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==", + "engines": { + "node": ">=20" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 000000000..76dcd3f7a --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "dependencies": { + "commander": "^14.0.2" + } +} From c44207963c66dffd0e0e03f24f76fa7ec168e4fa Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Mon, 17 Nov 2025 15:13:46 +0000 Subject: [PATCH 06/15] Ignore node_modules --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index cc8b55682..1f38afd3f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules prep/ +node_modules/ From aa7b0bf8a5b9164622b73f2c7b4680a80ba2769a Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Tue, 18 Nov 2025 12:50:47 +0000 Subject: [PATCH 07/15] Add -n flag support to cat using commander --- implement-shell-tools/cat/cat.js | 47 +++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index 3fa63cd2a..a71c15814 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -1,17 +1,44 @@ -import process from "node:process" +import {program} from "commander"; import {promises as fs} from "node:fs" -const argv = process.argv.slice(2) -if (argv.length != 1) { - console.error( - `Expected exactly 1 argument (a path) to be passed but got ${argv.length}.` - ); - process.exit(1); -} +//here we Configure the program: what flags and arguments it accepts +// .option("short, long", "description") +//The < > brackets mean "required" + +program + .name("cat") + .description("concatenate and print files") + .option("-n, --number", "Number all output lines") + .argument("", "The file path to process"); + +// here Parse command line arguments (reads process.argv and interprets it) +program.parse(); -const path = argv[0] +// Get the parsed values +const path = program.args[0]; // The filename user provided +const hasNumberFlag = program.opts().number; // True if user used -n flag +//read the file const content = await fs.readFile(path, "utf-8") -console.log(content) \ No newline at end of file + +// Output with or without line numbers +if (hasNumberFlag) { + // Add line numbers to each line + const lines = content.split("\n"); // Split into array of lines + const numberedLines = lines.map((line, index) => { + const numberOfLine = index + 1; // Convert 0-based index to 1-based line number + return `${numberOfLine} ${line}`; // Format: " 1 Hello" + }); + console.log(numberedLines.join("\n")); // Join back with newlines +} else { + // Just print the file as-is + console.log(content); +} + + + + + + From bb6cc75f0e97c164efbd91bd2a5776f6d416cc5c Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Tue, 18 Nov 2025 14:50:29 +0000 Subject: [PATCH 08/15] Add -b flag using commander --- implement-shell-tools/cat/cat.js | 38 ++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index a71c15814..fe5ee9f53 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -1,6 +1,5 @@ -import {program} from "commander"; -import {promises as fs} from "node:fs" - +import { program } from "commander"; +import { promises as fs } from "node:fs"; //here we Configure the program: what flags and arguments it accepts // .option("short, long", "description") @@ -10,35 +9,46 @@ program .name("cat") .description("concatenate and print files") .option("-n, --number", "Number all output lines") + .option("-b, --numberNonBlank", "Number non-blank output lines") .argument("", "The file path to process"); // here Parse command line arguments (reads process.argv and interprets it) program.parse(); - // Get the parsed values const path = program.args[0]; // The filename user provided -const hasNumberFlag = program.opts().number; // True if user used -n flag +const hasNumberFlag = program.opts().number; // True if user used -n flag +const hasBFlag = program.opts().numberNonBlank; //read the file -const content = await fs.readFile(path, "utf-8") +const content = await fs.readFile(path, "utf-8"); // Output with or without line numbers if (hasNumberFlag) { + // Add line numbers to each line const lines = content.split("\n"); // Split into array of lines const numberedLines = lines.map((line, index) => { - const numberOfLine = index + 1; // Convert 0-based index to 1-based line number - return `${numberOfLine} ${line}`; // Format: " 1 Hello" + const lineNumber = index + 1; // Convert 0-based index to 1-based line number + return `${lineNumber} ${line}`; // Format: " 1 Hello" }); console.log(numberedLines.join("\n")); // Join back with newlines +} else if (hasBFlag) { + console.log("Using -b flag!"); + let lineNumber = 0; + const lines = content.split("\n"); + console.log("Total lines:", lines.length); + const numberedLines = lines.map((line) => { + console.log(`Line: "${line}", isEmpty: ${line.trim() === ""}`); + if (line.trim() === "") { + return line; + } else { + lineNumber = lineNumber + 1; + return ` ${lineNumber} ${line}` + } + }); +console.log(numberedLines.join("\n")); } else { // Just print the file as-is console.log(content); } - - - - - - From 6cedb45ba2a847a12418cf9bd6e6846964147cc8 Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Tue, 18 Nov 2025 18:57:28 +0000 Subject: [PATCH 09/15] Add multiple file support to cat --- implement-shell-tools/cat/cat.js | 72 ++++++++++++++++---------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index fe5ee9f53..af4867c60 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -10,45 +10,45 @@ program .description("concatenate and print files") .option("-n, --number", "Number all output lines") .option("-b, --numberNonBlank", "Number non-blank output lines") - .argument("", "The file path to process"); + .argument("", "The file paths to process"); // here Parse command line arguments (reads process.argv and interprets it) program.parse(); -// Get the parsed values -const path = program.args[0]; // The filename user provided -const hasNumberFlag = program.opts().number; // True if user used -n flag -const hasBFlag = program.opts().numberNonBlank; - -//read the file -const content = await fs.readFile(path, "utf-8"); - -// Output with or without line numbers -if (hasNumberFlag) { - - // Add line numbers to each line - const lines = content.split("\n"); // Split into array of lines - const numberedLines = lines.map((line, index) => { - const lineNumber = index + 1; // Convert 0-based index to 1-based line number - return `${lineNumber} ${line}`; // Format: " 1 Hello" - }); - console.log(numberedLines.join("\n")); // Join back with newlines -} else if (hasBFlag) { - console.log("Using -b flag!"); - let lineNumber = 0; - const lines = content.split("\n"); - console.log("Total lines:", lines.length); - const numberedLines = lines.map((line) => { - console.log(`Line: "${line}", isEmpty: ${line.trim() === ""}`); - if (line.trim() === "") { - return line; - } else { +// Shared counter across all files for -n and -b flags +let lineNumber = 0; + + +// Process each file +for (const path of program.args) { + const hasNumberFlag = program.opts().number; // True if user used -n flag + const hasBFlag = program.opts().numberNonBlank; + + //read the file for each argument + const content = await fs.readFile(path, "utf-8"); + // Output with or without line numbers + if (hasNumberFlag) { + // Add line numbers to each line + const lines = content.split("\n"); // Split into array of lines + const numberedLines = lines.map((line) => { lineNumber = lineNumber + 1; - return ` ${lineNumber} ${line}` - } - }); -console.log(numberedLines.join("\n")); -} else { - // Just print the file as-is - console.log(content); + return `${lineNumber} ${line}`; // Format: " 1 Hello" + }); + console.log(numberedLines.join("\n")); // Join back with newlines + } else if (hasBFlag) { + const lines = content.split("\n"); + + const numberedLines = lines.map((line) => { + if (line.trim() === "") { + return line; + } else { + lineNumber = lineNumber + 1; + return ` ${lineNumber} ${line}`; + } + }); + console.log(numberedLines.join("\n")); + } else { + // Just print the file as-is + console.log(content); + } } From 668b8cead6f0aaa0b525e1ac55a7f34eb83f1eaa Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Wed, 19 Nov 2025 11:10:07 +0000 Subject: [PATCH 10/15] Start basic wc implementation --- implement-shell-tools/wc/wc.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 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 000000000..ee05ac80f --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,30 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +//config the program +program + .name("wc") + .description( + "The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input" + ) + .argument("", "The file path to process"); + +//interpret the program +program.parse(); + +//use the parsed data +const path = program.args[0]; + +//read the file +const content = await fs.readFile(path, "utf-8"); + +//count lines +const lines = content.split("\n").length - 1; + +//count words split by any whitespace +const words = content.split(/\s+/).filter((word) => word.length > 0).length; + +//count character +const characters = content.length; + +console.log(`${lines} ${words} ${characters} ${path}`); From 661a148450fc50ececd4a7d0fcdbd78cbf91a03d Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Wed, 19 Nov 2025 11:52:47 +0000 Subject: [PATCH 11/15] Implement wc with multiple file support and totals --- implement-shell-tools/wc/wc.js | 39 +++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js index ee05ac80f..62ab9a760 100644 --- a/implement-shell-tools/wc/wc.js +++ b/implement-shell-tools/wc/wc.js @@ -7,24 +7,39 @@ program .description( "The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input" ) - .argument("", "The file path to process"); + .argument("", "The file paths to process"); //interpret the program program.parse(); -//use the parsed data -const path = program.args[0]; +//initialise totals +let totalLines = 0; +let totalWords = 0; +let totalCharacters = 0; -//read the file -const content = await fs.readFile(path, "utf-8"); +//process each file -//count lines -const lines = content.split("\n").length - 1; +for (const path of program.args) { + //read the file + const content = await fs.readFile(path, "utf-8"); -//count words split by any whitespace -const words = content.split(/\s+/).filter((word) => word.length > 0).length; + //count lines + const lines = content.split("\n").length - 1; -//count character -const characters = content.length; + //count words (split by any whitespace) + const words = content.split(/\s+/).filter((word) => word.length > 0).length; -console.log(`${lines} ${words} ${characters} ${path}`); + //count character + const characters = content.length; + + //Add to totals + totalLines += lines; + totalWords += words; + totalCharacters += characters; + + console.log(`${lines} ${words} ${characters} ${path}`); +} + +if (program.args.length > 1) { + console.log(`${totalLines} ${totalWords} ${totalCharacters} total`); +} From 2b10b68c040610db309ba9ab32bff088b9920636 Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Wed, 19 Nov 2025 13:00:37 +0000 Subject: [PATCH 12/15] Add flag support to wc (-l, -w, -c) --- implement-shell-tools/wc/wc.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js index 62ab9a760..d79a48eba 100644 --- a/implement-shell-tools/wc/wc.js +++ b/implement-shell-tools/wc/wc.js @@ -7,6 +7,9 @@ program .description( "The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input" ) + .option("-l, --lines", "count lines only") + .option("-w, --words", "count words only") + .option("-c, --bytes", "count bytes only") .argument("", "The file paths to process"); //interpret the program @@ -15,7 +18,25 @@ program.parse(); //initialise totals let totalLines = 0; let totalWords = 0; -let totalCharacters = 0; +let totalBytes = 0; + +//check for flags +const hasLineFlag = program.opts().lines; +const hasWordFlag = program.opts().words; +const hasBytesFlag = program.opts().bytes; + +// create output format function to avoid repetition +function formatOutput(lines, words, bytes, path) { + if (hasLineFlag) { + console.log(`${lines} ${path}`); + } else if (hasWordFlag) { + console.log(`${words} ${path}`); + } else if (hasBytesFlag) { + console.log(`${bytes} ${path}`); + } else { + console.log(`${lines} ${words} ${bytes} ${path}`); + } +} //process each file @@ -29,17 +50,17 @@ for (const path of program.args) { //count words (split by any whitespace) const words = content.split(/\s+/).filter((word) => word.length > 0).length; - //count character - const characters = content.length; + //count bytes correctly especially important for non-ASCII characters + const bytes = Buffer.byteLength(content, "utf-8"); //Add to totals totalLines += lines; totalWords += words; - totalCharacters += characters; + totalBytes += bytes; - console.log(`${lines} ${words} ${characters} ${path}`); + formatOutput(lines, words, bytes, path); } if (program.args.length > 1) { - console.log(`${totalLines} ${totalWords} ${totalCharacters} total`); + formatOutput(totalLines, totalWords, totalBytes, "total"); } From dec157ee358e6b14c0dba9b65c8acf211cec50b7 Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Wed, 19 Nov 2025 14:03:56 +0000 Subject: [PATCH 13/15] Implement ls with -1 flag and directory argument support --- implement-shell-tools/ls/ls.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 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 000000000..407a69358 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,28 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +//config the program +program + .name("ls") + .description("list directory contents") + .option("-1, --one", "Force output to be one entry per line") + .argument("[directory]", "Directory to list", "."); // "." means current directory + +//interpret the program +program.parse(); + +// Get the directory argument (first argument in program.args array) +// If no argument provided, default to current directory "." +const directory = program.args[0] || "."; + +//read the directory to get array of filenames +const files = await fs.readdir(directory); + + + +//print each file on its own line +// Note: console.log(files) would print the entire array like: ['file1', 'file2'] +// Loop prints each individually on separate lines +for (const file of files) { + console.log(file) +} From b91a308a3339c3b1ffd9ce24c88bd274d2592f29 Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Wed, 19 Nov 2025 14:27:30 +0000 Subject: [PATCH 14/15] Add -a flag to ls to show hidden files --- implement-shell-tools/ls/ls.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js index 407a69358..25b2be937 100644 --- a/implement-shell-tools/ls/ls.js +++ b/implement-shell-tools/ls/ls.js @@ -6,6 +6,10 @@ program .name("ls") .description("list directory contents") .option("-1, --one", "Force output to be one entry per line") + .option( + "-a, --all", + "shows all the files including the hidden ones which start with a dot" + ) .argument("[directory]", "Directory to list", "."); // "." means current directory //interpret the program @@ -18,11 +22,22 @@ const directory = program.args[0] || "."; //read the directory to get array of filenames const files = await fs.readdir(directory); +//check for flags +const hasAflag = program.opts().all; + +// Filter the files array BEFORE looping +// If hasAflag is true, keep all files +// If hasAflag is false, remove files that start with "." + +const fileToShow = hasAflag +? files +: files.filter(file => !file.startsWith(".")) //print each file on its own line // Note: console.log(files) would print the entire array like: ['file1', 'file2'] // Loop prints each individually on separate lines -for (const file of files) { + +for (const file of fileToShow) { console.log(file) -} +} \ No newline at end of file From e52032471bb30bf5c9d9427ba556091abeb47162 Mon Sep 17 00:00:00 2001 From: zohrehKazemianpour <129424353+zohrehKazemianpour@users.noreply.github.com> Date: Sun, 4 Jan 2026 14:47:48 +0000 Subject: [PATCH 15/15] Refactor: address PR feedback for cat and ls tools --- .gitignore | 5 +++-- implement-shell-tools/cat/cat.js | 16 ++++++---------- implement-shell-tools/ls/ls.js | 16 ++++++++++------ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 1f38afd3f..9be2500d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ + +prep node_modules -prep/ -node_modules/ +.venv diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js index af4867c60..3579f5588 100644 --- a/implement-shell-tools/cat/cat.js +++ b/implement-shell-tools/cat/cat.js @@ -22,29 +22,24 @@ let lineNumber = 0; // Process each file for (const path of program.args) { const hasNumberFlag = program.opts().number; // True if user used -n flag - const hasBFlag = program.opts().numberNonBlank; + const shouldNumberNonBlank = program.opts().numberNonBlank; //read the file for each argument const content = await fs.readFile(path, "utf-8"); + const lines = content.split("\n"); // Split into array of lines + // Output with or without line numbers if (hasNumberFlag) { // Add line numbers to each line - const lines = content.split("\n"); // Split into array of lines const numberedLines = lines.map((line) => { lineNumber = lineNumber + 1; return `${lineNumber} ${line}`; // Format: " 1 Hello" }); console.log(numberedLines.join("\n")); // Join back with newlines - } else if (hasBFlag) { - const lines = content.split("\n"); + } else if (shouldNumberNonBlank) { const numberedLines = lines.map((line) => { - if (line.trim() === "") { - return line; - } else { - lineNumber = lineNumber + 1; - return ` ${lineNumber} ${line}`; - } + return line.trim() === "" ? line : `${++lineNumber} ${line}`; }); console.log(numberedLines.join("\n")); } else { @@ -52,3 +47,4 @@ for (const path of program.args) { console.log(content); } } + diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js index 25b2be937..173387284 100644 --- a/implement-shell-tools/ls/ls.js +++ b/implement-shell-tools/ls/ls.js @@ -24,6 +24,7 @@ const files = await fs.readdir(directory); //check for flags const hasAflag = program.opts().all; +const hasOneFlag = program.opts().one; // Filter the files array BEFORE looping // If hasAflag is true, keep all files @@ -34,10 +35,13 @@ const fileToShow = hasAflag : files.filter(file => !file.startsWith(".")) -//print each file on its own line -// Note: console.log(files) would print the entire array like: ['file1', 'file2'] -// Loop prints each individually on separate lines +if (hasOneFlag) { + + for (const file of fileToShow) { + console.log(file); + } +} else { + // print horizontally + console.log(fileToShow.join(" ")); +} -for (const file of fileToShow) { - console.log(file) -} \ No newline at end of file