diff --git a/implement-shell-tools/cat/cat.mjs b/implement-shell-tools/cat/cat.mjs new file mode 100644 index 000000000..2bdabad59 --- /dev/null +++ b/implement-shell-tools/cat/cat.mjs @@ -0,0 +1,51 @@ +import { promises as fs } from "node:fs"; +import { program } from "commander"; + +program + .name("cat") + .description("my own cat program") + .option("-n", "number all lines") + .option("-b", "number non-empty lines") + .argument("", "The file path to process"); +program.parse(); + +const paths = program.args; +const options = program.opts(); + +let lineNumber = 1; + +for (const path of paths) { + try { + const content = await fs.readFile(path, "utf-8"); + if (options.b) { + const lines = content.split("\n"); + + if (lines[lines.length - 1] === "") { + lines.pop(); + } + for (const line of lines) { + if (line.trim() !== "") { + process.stdout.write(` ${lineNumber} ${line}\n`); + lineNumber++; + } else { + process.stdout.write("\n"); + } + } + } else if (options.n) { + const lines = content.split("\n"); + + if (lines[lines.length - 1] === "") { + lines.pop(); + } + + for (const line of lines) { + process.stdout.write(` ${lineNumber} ${line}\n`); + lineNumber++; + } + } else { + process.stdout.write(content); + } + } catch (error) { + console.error(error.message); + } +} diff --git a/implement-shell-tools/ls/ls.mjs b/implement-shell-tools/ls/ls.mjs new file mode 100644 index 000000000..5bbaafdc8 --- /dev/null +++ b/implement-shell-tools/ls/ls.mjs @@ -0,0 +1,24 @@ +import { promises as fs } from "node:fs"; +import { program } from "commander"; + +program + .name("ls ") + .description("ls implementation") + .argument("[path]", "The path to process") //zero or one path + .option("-1, --one-per-line", "one file per line") + .option("-a", "show hidden files"); +program.parse(); + +const path = program.args[0] || "."; +const options = program.opts(); +try { + const files = await fs.readdir(path); + + for (const file of files) { + if (options.a || !file.startsWith(".")) { + console.log(file); + } + } +} catch (error) { + console.error(error.message); +} diff --git a/implement-shell-tools/package-lock.json b/implement-shell-tools/package-lock.json new file mode 100644 index 000000000..928714cdf --- /dev/null +++ b/implement-shell-tools/package-lock.json @@ -0,0 +1,20 @@ +{ + "name": "implement-shell-tools", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "commander": "^15.0.0" + } + }, + "node_modules/commander": { + "version": "15.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-15.0.0.tgz", + "integrity": "sha512-z67u4ZhzCL/Tydu1lJARtEZYWbWaN7oYLHbsuzocr6y4N6WZAagG3RQ4FW61V1/0+jImpj293XfrcYnd1qxtPg==", + "engines": { + "node": ">=22.12.0" + } + } + } +} diff --git a/implement-shell-tools/package.json b/implement-shell-tools/package.json new file mode 100644 index 000000000..6bd6aa2a7 --- /dev/null +++ b/implement-shell-tools/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "commander": "^15.0.0" + } +} diff --git a/implement-shell-tools/wc/wc.mjs b/implement-shell-tools/wc/wc.mjs new file mode 100644 index 000000000..eb8a67f8c --- /dev/null +++ b/implement-shell-tools/wc/wc.mjs @@ -0,0 +1,59 @@ +import { program } from "commander"; + +import { promises as fs } from "node:fs"; + +program + .name("wc") + .description("wc implementation") + .argument("", "the file path to process") + .option("-l", "count lines") + .option("-w", "count words") + .option("-c", "count characters"); + +program.parse(); + +const paths = program.args; +const options = program.opts(); +const noFlag = !options.l && !options.w && !options.c; + +const total = {}; +let hadError = false; +for (const path of paths) { + try { + const content = await fs.readFile(path, "utf-8"); + + const linesCounter = content.split("\n").length - 1; + const trimmedContent = content.trim(); + const wordsCounter = + trimmedContent === "" ? 0 : trimmedContent.split(/\s+/).length; + const characterCounter = content.length; + + const results = []; + if (options.l || noFlag) { + results.push(linesCounter); + total["lineCounter"] = (total["lineCounter"] ?? 0) + linesCounter; + } + if (options.w || noFlag) { + results.push(wordsCounter); + total["wordsCounter"] = (total["wordsCounter"] ?? 0) + wordsCounter; + } + if (options.c || noFlag) { + results.push(characterCounter); + total["characterCounter"] = + (total["characterCounter"] ?? 0) + characterCounter; + } + + console.log(results.join(" ") + " " + path); + + } catch (error) { + console.error(error.message); + hadError = true; + } +} +if (paths.length > 1) { + + console.log(Object.values(total).join(" "),"total") +} +if (hadError) { + process.exitCode = 1; +}