Skip to content
51 changes: 51 additions & 0 deletions implement-shell-tools/cat/cat.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { promises as fs } from "node:fs";
import { program } from "commander";

program
.name("cat")
.description("my own cat program")
.option("-n", "number all lines")
.option("-b", "number non-empty lines")
.argument("<paths...>", "The file path to process");
program.parse();

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

let lineNumber = 1;
let hadError = false;
for (const path of paths) {
try {
const content = await fs.readFile(path, "utf-8");
if (options.n || options.b) {
const lines = content.split("\n");
Comment thread
LonMcGregor marked this conversation as resolved.
if (lines[lines.length - 1] === "") {
lines.pop();
}

if (options.b) {
for (const line of lines) {
if (line.trim() !== "") {
process.stdout.write(` ${lineNumber} ${line}\n`);
lineNumber++;
} else {
process.stdout.write("\n");
}
}
} else if (options.n) {
for (const line of lines) {
process.stdout.write(`${String(lineNumber).padStart(6)}\t ${line}\n`);
lineNumber++;
}
}
} else {
process.stdout.write(content);
}
} catch (error) {
console.error(error.message);
hadError = true;
}
}
if (hadError) {
process.exitCode = 1;
}
28 changes: 28 additions & 0 deletions implement-shell-tools/ls/ls.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { promises as fs } from "node:fs";
import { program } from "commander";

program
.name("ls ")
.description("ls implementation")
.argument("[path]", "The path to process") //zero or one path
.option("-1, --one-per-line", "one file per line")
Comment thread
LonMcGregor marked this conversation as resolved.
.option("-a", "show hidden files");
program.parse();

const path = program.args[0] || ".";
const options = program.opts();
try {
const files = await fs.readdir(path);
const visibleFiles = files.filter(
(file) => options.a || !file.startsWith("."),
);
if(options.onePerLine)
{
console.log(visibleFiles.join("\n"))
}else{
console.log(visibleFiles.join(" "))
}

} catch (error) {
console.error(error.message);
}
20 changes: 20 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.

5 changes: 5 additions & 0 deletions implement-shell-tools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"commander": "^15.0.0"
}
}
60 changes: 60 additions & 0 deletions implement-shell-tools/wc/wc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { program } from "commander";

import { promises as fs } from "node:fs";

program
.name("wc")
.description("wc implementation")
.argument("<paths...>", "the file path to process")
.option("-l", "count lines")
.option("-w", "count words")
.option("-c", "count characters");

program.parse();

const paths = program.args;
const options = program.opts();
const noFlag = !options.l && !options.w && !options.c;

const total = {};
let hadError = false;
for (const path of paths) {
try {
const content = await fs.readFile(path, "utf-8");

const linesCounter = content.split("\n").length - 1;
const trimmedContent = content.trim();
const wordsCounter =
trimmedContent === "" ? 0 : trimmedContent.split(/\s+/).length;
const characterCounter = content.length;

const results = [];
if (options.l || noFlag) {
results.push(linesCounter);
total["lineCounter"] = (total["lineCounter"] ?? 0) + linesCounter;
}
if (options.w || noFlag) {
results.push(wordsCounter);
total["wordsCounter"] = (total["wordsCounter"] ?? 0) + wordsCounter;
}
if (options.c || noFlag) {
results.push(characterCounter);
total["characterCounter"] =
(total["characterCounter"] ?? 0) + characterCounter;
}

console.log(results.map(value=> String(value).padStart(4)).join(" ") +" "+ path)

} catch (error) {
console.error(error.message);
hadError = true;
}
}
if (paths.length > 1) {
Comment thread
LonMcGregor marked this conversation as resolved.

console.log(Object.values(total).map(value=> String(value).padStart(4)).join(" "),"total")

}
if (hadError) {
process.exitCode = 1;
}
Loading