Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
40 changes: 40 additions & 0 deletions implement-shell-tools/cat/myCat.js
Comment thread
LonMcGregor marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { program } from "commander";
import { promises as fs } from "node:fs";
import process from "node:process";
Comment thread
LonMcGregor marked this conversation as resolved.
Outdated

program
.name("myCat")
.description("Simple file viewer")
.option("-n", "Number all output lines")
.option("-b", "Number non-blank output lines")
.argument("<path...>", "One or more file paths to show");

program.parse();

const files = program.args;
const opts = program.opts();
let lineNumber = 1;

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

if (opts.n) {
const lines = content.split("\n");

for (const line of lines) {
console.log(lineNumber + " " + line);
lineNumber++;
}
} else if (opts.b) {
const lines = content.split("\n");
Comment thread
LonMcGregor marked this conversation as resolved.
Outdated

for (const line of lines) {
if (line.trim() !== "") {
console.log(lineNumber + " " + line);
lineNumber++;
}
}
} else {
console.log(content);
}
}
36 changes: 36 additions & 0 deletions implement-shell-tools/ls/myLs.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";
Comment thread
LonMcGregor marked this conversation as resolved.
Outdated

program
.name("myLs")
.description("my ls clone")
.option("-1", "one entry per line")
.option("-a", "show hidden files")
.argument("[paths...]", "file or directory paths");
Comment thread
LonMcGregor marked this conversation as resolved.

program.parse();

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

if (paths.length === 0) {
paths = ["."];
}

for (const path of paths) {
const entries = await fs.readdir(path);
Comment thread
LonMcGregor marked this conversation as resolved.
Outdated

for (const file of entries) {
if (!opts.a && file.startsWith(".")) {
continue;
}

if (opts["1"]) {
Comment thread
LonMcGregor marked this conversation as resolved.
Outdated
console.log(file);
} else {
console.log(file + " ");
}
}

}
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.

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.2"
}
}
68 changes: 68 additions & 0 deletions implement-shell-tools/wc/MyWc.js
Comment thread
LonMcGregor marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { program } from "commander";
import { promises as fs } from "node:fs";
import process from "node:process";
Comment thread
LonMcGregor marked this conversation as resolved.
Outdated

program
.name("myLs")
.description("my ls clone")
Comment thread
LonMcGregor marked this conversation as resolved.
Outdated
.option("-l", "line count")
.option("-w", "words count")
.option("-c", "character count")
.option("-s", "character count without spaces")
.argument("[paths...]", "file or directory paths");

program.parse();

const opts = program.opts();
let files = program.args;

if (files.length === 0) {
files = ["."];
}

let totalLines = 0;
let totalWords = 0;

if (opts.l) {
for (const file of files) {
const content = await fs.readFile(file, "utf-8");
console.log(content);
const lineCount = content.split("\n").length;

totalLines += lineCount;
}
console.log("Lines:", totalLines);
}
if (opts.w) {
for (const file of files) {
const content = await fs.readFile(file, "utf-8");
console.log(content);

const wordCount = content.trim().split(/\s+/).length;
totalWords += wordCount;
}
console.log("Total words:", totalWords);
}
if (opts.c) {
let totalChars = 0;
for (const file of files) {
const content = await fs.readFile(file, "utf-8");

totalChars += content.trim().length;
// const charList = content.trim().split(/\s+/);
Comment thread
LonMcGregor marked this conversation as resolved.
Outdated
// for (const char of charList) {
// totalChars += char.length;
// }
}
console.log("Total characters:", totalChars);
}

if (opts.s) {
let totalCharsNoSpaces = 0;
for (const file of files) {
const content = await fs.readFile(file, "utf-8");
const withoutSpaces = content.replace(/\s/g, "");
totalCharsNoSpaces += withoutSpaces.length;
}
console.log("Total characters without spaces:", totalCharsNoSpaces);
}