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

program
.name("check-for-cat")
.description("Implement my own version of cat")
.argument("<paths...>", "The file paths to process")
.option("-n", "Number lines")
.option("-b", "Number non-blank lines");

program.parse();

const paths = program.args;
const showN = program.opts().n;
const showB = program.opts().b;

let lineNumber = 1;

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

const endsWithNewline = content.endsWith("\n");
let lines = content.split("\n");
if (endsWithNewline) {
lines.pop();
}

lines = lines.map((line) => {
if (showB) {

if (line === "") {
return line;
}
const numbered = String(lineNumber).padStart(6, " ") + "\t" + line;
lineNumber++;
return numbered;
} else if (showN) {

const numbered = String(lineNumber).padStart(6, " ") + "\t" + line;
lineNumber++;
return numbered;
} else {

return line;
}
});

let output = lines.join("\n");
if (endsWithNewline) {
output += "\n";
}
process.stdout.write(output);
}
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"
}
}
46 changes: 46 additions & 0 deletions implement-shell-tools/ls/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { program } from "commander";
import { promises as fs } from "node:fs";

program
.name("check-for-ls")
.description("Implement my own version of ls")
.argument("[paths...]", "The file paths to process")
.option("-1, --one", "This lists one file per line")
.option("-a", "This shows all files");

program.parse();

const showOneLine = program.opts().one;
const showAllFiles = program.opts().a;
const paths = program.args;

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

for (const target of filePath) {
const info = await fs.stat(target);

let showFiles;
if (info.isDirectory()) {
showFiles = await fs.readdir(target);

if (showAllFiles) {
showFiles = [".", "..", ...showFiles];
} else {
showFiles = showFiles.filter((name) => !name.startsWith("."));
}
showFiles.sort();
} else {
showFiles = [target];
}

if (showOneLine) {
for (const file of showFiles) {
console.log(file);
}
} else {
console.log(showFiles.join(" "));
}
}
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/private-test/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/private-test/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/private-test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { program } from "commander";
import { promises as fs } from "node:fs";
import process from "node:process";

program
.name("count-containing-words")
.description("Counts words in a file that contain a particular character")
.option("-c, --char <char>", "The character to search for", "e")
.argument("<path>", "The file path to process");

program.parse();

const argv = program.args;
if (argv.length != 1) {
console.error(
`Expected exactly 1 argument (a path) to be passed but got ${argv.length}.`,
);
process.exit(1);
}
const path = argv[0];
const char = program.opts().char;

const content = await fs.readFile(path, "utf-8");
const countOfWordsContainingChar = content
.split(" ")
.filter((word) => word.includes(char)).length;
console.log(countOfWordsContainingChar);
2 changes: 2 additions & 0 deletions implement-shell-tools/private-test/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello baby gorl
i like you
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.

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

program
.name("check-for-wc")
.description("Implement my own version of wc")
.argument("<paths...>", "The file paths to process")
.option("-l", "Counts the total number of lines")
.option("-c", "Counts the total number of characters")
.option("-w", "Counts the total number of words");

program.parse();

const paths = program.args;
const showLines = program.opts().l;
const showWords = program.opts().w;
const showChar = program.opts().c;

const noFlagsGiven = !showLines && !showWords && !showChar;

const columns = [];

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

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

const wordCount = content
.split(/\s+/)
.filter((word) => word.length > 0).length;

const charCount = Buffer.byteLength(content, "utf-8");

columns.push({
path: path,
lines: lineCount,
words: wordCount,
char: charCount,
});
}
if (columns.length > 1) {
let totalLines = 0;
let totalWords = 0;
let totalChar = 0;

for (const result of columns) {
totalLines += result.lines;
totalWords += result.words;
totalChar += result.char;
}

columns.push({
path: "total",
lines: totalLines,
words: totalWords,
char: totalChar,
});
}

for (const result of columns) {
let line = "";

if (noFlagsGiven || showLines) {
line += String(result.lines).padStart(6, " ");
}
if (noFlagsGiven || showWords) {
line += String(result.words).padStart(6, " ");
}
if (noFlagsGiven || showChar) {
line += String(result.char).padStart(6, " ");
}

line += " " + result.path;

console.log(line);
}
Loading