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

program
.name("cat command")
.description("Implementing 'cat' command")
.option("-n", "The line numbers")
.option("-b", "The line numbers only for non-empty lines")
.argument("<paths...>", "The file paths to process");

program.parse();

const paths = program.args;
let lineNumber = 1;
const displayLineNumber = program.opts().n;
const displayNonEmptyLineNumber = program.opts().b;

for(const path of paths){
const content = await fs.readFile(path, "utf-8");
const lines = content.split("\n");
for(const line of lines){
if(displayLineNumber){
console.log(`${String(lineNumber).padStart(6, ' ')} ${line}`);
lineNumber++;
} else if (displayNonEmptyLineNumber) {
if (line.trim()) {
console.log(`${String(lineNumber).padStart(6, ' ')} ${line}`);
lineNumber++;
} else {
console.log(line);

}
}
}
}
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 { program } from "commander";
import { promises as fs } from "node:fs";

program
.name("ls command")
.description("Implementing 'ls' command")
.option("-1", "list one file per line")
.option("-a", "include hidden files")
.argument("[directory]", "Directory to list");

program.parse();

const directory = program.args[0] || "."; //current directory as a defult if no directory provided
const allFiles = program.opts().a;
const listPerLine = program.opts()["1"];



const files = await fs.readdir(directory);
const visibleFiles = allFiles ? files : files.filter(file => !file.startsWith("."));

if (listPerLine){
for (const file of visibleFiles){
console.log(file)
}
} else {
console.log(visibleFiles.join(" "))
}

21 changes: 21 additions & 0 deletions implement-shell-tools/package-lock.json

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

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

program
.name("wc command")
.description("Implementing 'wc' command")
.option("-l", "show line count")
.option("-w", "show word count")
.option("-c", "show character count")
.argument("<paths...>", "files to read");

program.parse();

const paths = program.args;
const options = program.opts();


function formatCounts(lines, words, chars, options) {
let result = "";

if (options.l || options.w || options.c) {
if (options.l) result += `${lines}L `;
if (options.w) result += `${words}W `;
if (options.c) result += `${chars}Char `;
} else {
result += `${lines}L ${words}W ${chars}Char `;
}

return result;
}

let totalLines = 0;
let totalWords = 0;
let totalChars = 0;

for (const path of paths) {
const content = await fs.readFile(path, "utf-8");

const lineCount = (content.match(/\n/g) || []).length;
const wordCount = content.trim().split(/\s+/).length;
const charCount = content.length;

totalLines += lineCount;
totalWords += wordCount;
totalChars += charCount;

const output = formatCounts(lineCount, wordCount, charCount, options);

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

if (paths.length > 1) {
const totalOutput = formatCounts(totalLines, totalWords, totalChars, options);

console.log(`${totalOutput}total`);
}