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 new file mode 100644 index 000000000..70301b350 --- /dev/null +++ b/implement-shell-tools/cat/myCat.js @@ -0,0 +1,38 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +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 files = program.args; +const opts = program.opts(); +let lineNumber = 1; + +for (const filename of files) { + const content = await fs.readFile(filename, "utf-8"); + const lines = content.split("\n"); + + if (opts.n) { + + for (const line of lines) { + console.log(lineNumber + " " + line); + lineNumber++; + } + } else if (opts.b) { + + for (const line of lines) { + if (line.trim() !== "") { + console.log(lineNumber + " " + line); + lineNumber++; + } + } + } else { + console.log(content); + } +} diff --git a/implement-shell-tools/ls/myLs.js b/implement-shell-tools/ls/myLs.js new file mode 100644 index 000000000..05e0777db --- /dev/null +++ b/implement-shell-tools/ls/myLs.js @@ -0,0 +1,35 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .name("myLs") + .description("my ls clone") + .option("-1, --one-per-line", "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 directoryItems = await fs.readdir(path); + + for (const file of directoryItems) { + if (!opts.a && file.startsWith(".")) { + continue; + } + + if (opts.onePerLine) { + console.log(file); + } else { + console.log(file + " "); + } + } + +} 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" + } +} diff --git a/implement-shell-tools/wc/MyWc.js b/implement-shell-tools/wc/MyWc.js new file mode 100644 index 000000000..981c2b089 --- /dev/null +++ b/implement-shell-tools/wc/MyWc.js @@ -0,0 +1,62 @@ +import { program } from "commander"; +import { promises as fs } from "node:fs"; + +program + .name("myWc") + .description("my wc 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"); + 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"); + + 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; + + } + 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); +}