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
38 changes: 38 additions & 0 deletions implement-shell-tools/cat/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {program} from "commander";
import {promises as fs} from "node:fs";
import process from "node:process";

program
.name ("cat")
.description ("A JS clone for unix command cat")
.argument("<files...>","One or more files to reach and print")
.option("-n", "number all output lines")
.action(async (files,options)=> {
for (const filePath of files){
try {
const content = await fs.readFile(filePath,"utf8");
if (options.n)
{
const lines = content.trimEnd().split("\n");
lines.forEach((line,index) => {
console.log(`${index+1} \t ${line}`);
});
}
else
{
process.stdout.write(content);
}


}
catch(error)
{
console.error("No such file or directory");
process.exit(1);
}

}

});

program.parse(process.argv);
21 changes: 21 additions & 0 deletions implement-shell-tools/cat/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions implement-shell-tools/cat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type":"module" ,
"dependencies": {
"commander": "^15.0.0"
}
}
27 changes: 27 additions & 0 deletions implement-shell-tools/ls/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {program} from "commander";
import process from "node:process";
import {promises as fs } from "node:fs";


program
.name ("list command clone")
.description("List all files & folders within current folder")
.argument("[directory]","Directory to list",".")
.option("-1","list file line by line")
.action(async (directory,options) =>
{
try
{
let files = await fs.readdir(directory);
console.log(files.join(" "));
}

catch (error)
{
console.error('No such file or directory');
process.exit(1);
}

});

program.parse(process.argv);
21 changes: 21 additions & 0 deletions implement-shell-tools/ls/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions implement-shell-tools/ls/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "module",
"dependencies": {
"commander": "^15.0.0"
}
}
21 changes: 21 additions & 0 deletions implement-shell-tools/wc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions implement-shell-tools/wc/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "module",
"dependencies": {
"commander": "^15.0.0"
}
}

60 changes: 60 additions & 0 deletions implement-shell-tools/wc/wc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import process from "node:process";
import {program} from "commander";
import {promises as fs} from "node:fs";

program
.name("WC clone")
.description("Counting each words in a given file")
.argument("<files...>","one or more files to count the words")
.action(async (files)=>{
let totalLines = 0;
let totalWords = 0;
let totalBytes = 0;

for (const file of files){
try {
const content = await fs.readFile(file,"utf-8");
const size = await fs.readFile(file);

const lineCount = content.split("\n").length-1;

const wordCount = content.trim() === "" ? 0 : content.trim().split(/\s+/).length;

const byteCount = size.length;

totalLines += lineCount;
totalWords += wordCount;
totalBytes += byteCount;

printCounts({lineCount,wordCount,byteCount,label:file,});
}

catch (err){
console.error("No such file or directory");
process.exit(1);
}
}

if (files.length>1)
{
printCounts({lineCount:totalLines,
wordCount:totalWords,
byteCount:totalBytes,
label:"Total",
})
}

});

function printCounts({lineCount,wordCount,byteCount,label})
{
let output = "";

output += lineCount.toString().padStart(7, " ");
output += wordCount.toString().padStart(7," ");
output += byteCount.toString().padStart(7," ");

console.log(`${output} ${label}`);
}

program.parse(process.argv);
Loading