diff --git a/implement-shell-tools/cat/cat.mjs b/implement-shell-tools/cat/cat.mjs new file mode 100644 index 000000000..703e82763 --- /dev/null +++ b/implement-shell-tools/cat/cat.mjs @@ -0,0 +1,32 @@ +import process from "node:process"; +import { promises as fs } from "node:fs"; + +let args = process.argv.slice(2); +const flags = args.filter((arg) => arg.startsWith("-")); +const filePaths = args.filter((args) => !args.startsWith("-")); +let lineNumber = 1; + +for (const filePath of filePaths) { + const content = await fs.readFile(filePath, "utf-8"); + + if (flags.length == 0) { + process.stdout.write(content); + continue; + } + + const lines = content.split("\n"); + + for (const line of lines) { + if (flags.includes("-b")) { + if (line === "") { + console.log(); + } else { + console.log(`${lineNumber}\t${line}`); + lineNumber++; + } + } else if (flags.includes("-n")) { + console.log(`${lineNumber}\t${line}`); + lineNumber++; + } + } +} diff --git a/implement-shell-tools/ls/ls.mjs b/implement-shell-tools/ls/ls.mjs new file mode 100644 index 000000000..a0f2a5593 --- /dev/null +++ b/implement-shell-tools/ls/ls.mjs @@ -0,0 +1,22 @@ +import process from "node:process"; +import { promises as fs } from "node:fs"; + +const args = process.argv.slice(2); +const flags = args.filter((arg) => arg.startsWith("-")); +const path = args.filter((arg) => !arg.startsWith("-")); +let targetDir = path[0] || "."; +let files = await fs.readdir(targetDir); + +if (flags.includes("-a")) { + files.unshift(".", ".."); +} else { + files = files.filter((fileName) => !fileName.startsWith(".")); +} + +if (flags.includes("-1")) { + for (const file of files) { + console.log(file); + } +} else { + console.log(files.join(" ")); +} diff --git a/implement-shell-tools/wc/sample-files/4.txt b/implement-shell-tools/wc/sample-files/4.txt new file mode 100644 index 000000000..e69de29bb diff --git a/implement-shell-tools/wc/wc.mjs b/implement-shell-tools/wc/wc.mjs new file mode 100644 index 000000000..a63068dc9 --- /dev/null +++ b/implement-shell-tools/wc/wc.mjs @@ -0,0 +1,48 @@ +import process from "node:process"; +import { promises as fs } from "node:fs"; + +let filePaths = []; +let flag = null; +let numberOfWords; + +if (process.argv[2].startsWith("-")) { + flag = process.argv[2]; + filePaths = process.argv.slice(3); +} else { + filePaths = process.argv.slice(2); +} +for (const filePath of filePaths) { + const content = await fs.readFile(filePath, "utf-8"); + + if (flag === "-w") { + console.log(getWordCount(content)); + } else if (flag === "-l") { + console.log(getLineCount(content)); + } else if (flag === "-c") { + console.log(getByteCount(content), filePaths); + } else { + console.log( + getWordCount(content), + getLineCount(content), + getByteCount(content), + filePaths, + ); + } +} + +function getWordCount(text) { + const trimmed = text.trim(); + if (trimmed.length == 0) { + return 0; + } + return trimmed.split(/\s+/).length; +} + +function getLineCount(text) { + if (text.length == 0) return 0; + return text.split("\n").length - 1; +} + +function getByteCount(text) { + return Buffer.byteLength(text, "utf-8"); +}