diff --git a/.gitignore b/.gitignore index 3c3629e6..c94a1fe6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +../../implement-cowsay/.venv/ diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 00000000..4b680dfc --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -0,0 +1,39 @@ +import { promises as fs } from "node:fs"; +import { glob } from "glob"; +async function readFile() { + let fileNamePattern; + let flag; + if (process.argv[2].startsWith("-")) { + flag = process.argv[2]; + fileNamePattern = process.argv[3]; + } else { + fileNamePattern = process.argv[2]; + } + + const filesNameArray = await glob(fileNamePattern); + filesNameArray.sort(); + let lineNumber = 1; + + for (const file of filesNameArray) { + const fileContent = await fs.readFile(file, "utf-8"); + if (flag == null) { + console.log(fileContent); + } else { + const linesOfText = fileContent.split(/\r?\n/); + if (linesOfText[linesOfText.length - 1].trim() === "") { + linesOfText.pop(); + } + + for (const line of linesOfText) { + if (line.trim() === "" && flag === "-b") { + console.log(line); + } else { + console.log(`${lineNumber} ${line}`); + lineNumber++; + } + } + } + } +} + +readFile(); diff --git a/implement-shell-tools/cat/package.json b/implement-shell-tools/cat/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/implement-shell-tools/cat/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 00000000..785f2a7c --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -0,0 +1,29 @@ +import { promises as fs } from "node:fs"; +async function listDir() { + let finalFileList; + const flag = process.argv[2]; + const argvArray = process.argv.slice(2); + + const flags = argvArray.filter((item) => item.startsWith("-")); + + const Path = + argvArray.filter((item) => !item.startsWith("-")) || process.cwd(); + + const isSingleColumn = flags.includes("-1"); + + const fileListArray = await fs.readdir(Path[0] || process.cwd()); + if (flags.includes("-a")) { + finalFileList = fileListArray; + } else { + finalFileList = fileListArray.filter((file) => !file.startsWith(".")); + } + + if (isSingleColumn) { + for (const item of finalFileList) { + console.log(item); + } + } else { + console.log(finalFileList.join(" ")); + } +} +listDir(); diff --git a/implement-shell-tools/ls/package.json b/implement-shell-tools/ls/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/implement-shell-tools/ls/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/implement-shell-tools/wc/package.json b/implement-shell-tools/wc/package.json new file mode 100644 index 00000000..3dbc1ca5 --- /dev/null +++ b/implement-shell-tools/wc/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 00000000..4216b71e --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -0,0 +1,66 @@ +import { promises as fs } from "node:fs"; +async function wcJsImplement() { + + let totalLines=0,totalWords=0,TotalBytes=0; + let words,bytes; + const commandLineArray=process.argv.slice(2); + const flags = commandLineArray.filter((item) => item.startsWith("-")); + + if(flags.length !==0 ) + { + commandLineArray.shift(); + } + + function printOutput(lines,words,bytes,lable){ + switch (flags[0]) { + case "-l": + console.log(`${String(lines).padStart(4)} ${lable}`); + break; + case "-w": + console.log(`${String(words).padStart(4)} ${lable}`); + break; + case "-c": + console.log(`${String(bytes).padStart(4)} ${lable}`); + break; + default: + console.log( + `${String(lines).padStart(4)}${String(words).padStart( + 4 + )}${String(bytes).padStart(4)} ${lable}` + ); + } + } + + + for(const file of commandLineArray){ + const buffer = await fs.readFile(file); + const fileContent=buffer.toString("utf-8"); + bytes=buffer.length; + TotalBytes += bytes; + + const lines=fileContent.split(/\r?\n/); + let linesCount,wordsCount=0; + //Calculate count of lines + if(lines[lines.length-1].trim()===""){ + lines.pop(); + linesCount=lines.length; + totalLines += linesCount; + } + //calculate count of words + for(const line of lines){ + words=line.trim().split(/\s+/); + if (words[0].trim() !== ""){ + wordsCount += words.length; + } + } + totalWords += wordsCount; + + printOutput(linesCount,wordsCount,bytes,file); + } + + if(commandLineArray.length>1){ + printOutput(totalLines, totalWords, TotalBytes, "total"); + } + +} +wcJsImplement(); \ No newline at end of file