diff --git a/implement-shell-tools/Package.json b/implement-shell-tools/Package.json new file mode 100644 index 000000000..dac6949b0 --- /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 000000000..289a4c037 --- /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/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..c3d4c0822 --- /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(" ")) + } + diff --git a/implement-shell-tools/package-lock.json b/implement-shell-tools/package-lock.json new file mode 100644 index 000000000..6f585d453 --- /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" + } + } + } +} diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..eef911ad2 --- /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