Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
../../implement-cowsay/.venv/
39 changes: 39 additions & 0 deletions implement-shell-tools/cat/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { promises as fs } from "node:fs";
import { glob } from "glob";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I run this code I get an error Cannot find package 'glob'. Do you see this also?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment but I don't see any error !

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();
3 changes: 3 additions & 0 deletions implement-shell-tools/cat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
29 changes: 29 additions & 0 deletions implement-shell-tools/ls/ls.js
Original file line number Diff line number Diff line change
@@ -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();
3 changes: 3 additions & 0 deletions implement-shell-tools/ls/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
3 changes: 3 additions & 0 deletions implement-shell-tools/wc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
66 changes: 66 additions & 0 deletions implement-shell-tools/wc/wc.js
Original file line number Diff line number Diff line change
@@ -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();