diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..e860264f6 --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,40 @@ +import process from "node:process"; +import { promises as fs } from "node:fs"; + +const argv = process.argv.slice(2, process.argv.length); +const flags = []; +const paths = []; +for (let i = 0; i < argv.length; i++) { + if (argv[i][0] == "-") { + flags.push(argv[i]); + } else { + paths.push(argv[i]); + } +} + +displayFiles(paths, flags); + +async function displayFiles(paths, flags) { + let content = ''; + for (let i = 0; i < paths.length; i++) { + content += await fs.readFile(paths[i], "utf-8"); + } + + const lines = content.split("\n"); + if (lines[lines.length - 1] == '') { + lines.pop(); + } + let lineNumber = 1; + for (let i = 0; i < lines.length; i++) { + let output = lines[i]; + if ( + (flags.includes("-n") && !flags.includes("-b")) || + (flags.includes("-b") && lines[i] != "") + ) { + output = " " + (lineNumber).toString() + " " + lines[i]; + lineNumber++; + } + console.log(output); + } +} + diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..fe8550677 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,27 @@ +import process from "node:process"; +import { promises as fs } from "node:fs"; + +const argv = process.argv.slice(2, process.argv.length); +const currentDir = './'; +const flags = []; +let path = ''; +for (let i = 0; i < argv.length; i++) { + if (argv[i][0] == "-") { + flags.push(argv[i]); + } else { + path = argv[i]; + } +} +if (path == '') path = currentDir; + +const content = await fs.readdir(path); + +if (flags.includes("-l")) { + for (let i = 0; i < content.length; i++) { + let line = content[i]; + if (!flags.includes("-a") && line[0] == ".") continue; + console.log(line); + } +} else { + console.log(content.join(" ")); +} \ No newline at end of file diff --git a/implement-shell-tools/package.json b/implement-shell-tools/package.json new file mode 100644 index 000000000..a8171e913 --- /dev/null +++ b/implement-shell-tools/package.json @@ -0,0 +1,13 @@ +{ + "name": "implement-shell-tools", + "version": "1.0.0", + "type": "module", + "description": "Your task is to re-implement shell tools you have used.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC" +} \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..7602c9d2b --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,91 @@ +import process from "node:process"; +import { promises as fs } from "node:fs"; + +const argv = process.argv.slice(2, process.argv.length); +let flag = ''; +const paths = []; +let dir = ''; +let ending = ''; +for (let i = 0; i < argv.length; i++) { + if (argv[i][0] == "-") { + flag = argv[i]; + } else { + paths.push(argv[i]); + } +} + +if (paths.length > 0) { + dir = paths[0].slice(0, paths[0].indexOf("/")); + ending = paths[0].slice(paths[0].indexOf("/") + 1); +}; + +const files = await resolveQuery(paths, dir, ending); +await logFilesInfo(files, flag); + +async function resolveQuery(paths, dir, ending) { + let files = []; + if (paths.length > 1) { + files = await fs.readdir(dir); + } else { + files = [ending]; + } + return files; +} + +async function logFilesInfo(files, flag) { + let totalLines = 0; + let totalWords = 0; + let totalBytes = 0; + let totalOutput = ''; + for (const fileName of files) { + let fileOutput = ''; + let filePath = dir + "/" + fileName; + let file = await fs.readFile(filePath, "utf-8"); + let linesNum = countLinesInString(file); + let wordsNum = countWordsInString(file); + let bytes = file.length; + totalLines += linesNum; + totalWords += wordsNum; + totalBytes += bytes; + if (flag == "-l") { + fileOutput = linesNum + " " + filePath; + } else if (flag == "-w") { + fileOutput = wordsNum + " " + filePath; + } else if (flag == '-c') { + fileOutput = bytes + " " + filePath; + } else { + fileOutput = linesNum + " " + wordsNum + " " + bytes + " " + filePath; + } + console.log(fileOutput); + } + if (files.length > 1) { + if (flag == "-l") { + totalOutput = totalLines + " total"; + } else if (flag == "-w") { + totalOutput = totalWords + " total"; + } else if (flag == '-c') { + totalOutput = totalBytes + " total"; + } else { + totalOutput = totalLines + " " + totalWords + " " + totalBytes + " total"; + } + console.log(totalOutput); + } +} + +function countLinesInString(str) { + let lines = str.split('\n'); + let linesNum = lines.length; + if (lines[lines.length - 1] == '') { + linesNum--; + } + return linesNum; +} + +function countWordsInString(str) { + let words = str.replace(/\n/g, ' ').split(' '); + words = words.filter((word) => { + return (word != '') + }); + let wordsNum = words.length; + return wordsNum; +} \ No newline at end of file