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


program
.name("cat")
.description("Prints file content with optional line numbers")
.argument("<file>", "file to read")
.option("-n, --number", "number all lines")
.option("-b, --number-nonblank", "number non-blank lines only")
.parse();

const options = program.opts();
const filePath = program.args[0];

try {
const content = await fs.readFile(filePath, "utf-8");
const lines = content.split("\n");

lines.forEach((line, index) => {
const lineNumber = index + 1;
if (options.number) {

console.log(`${lineNumber.toString().padStart(4)} ${line}`);
} else if (options.numberNonblank) {

if (line.trim() === "") {
console.log(" " + line);
} else {
console.log(`${lineNumber.toString().padStart(4)} ${line}`);
}
} else {

console.log(line);
}
});
} catch (err) {
console.error(`Error reading file "${filePath}":`, err.message);
process.exit(1);
}



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

program
.name ("ls")
.description("List the file directory")
.option("-1","List one file per line")
.option("-a","Include hidden file")
.argument("[dir]", "Directory to list",".")
.parse();

const options = program.opts();
const dir = program.args[0] || ".";

try {
const items = await fs.readdir(dir, { withFileTypes: true });

let fileNames = items.map(item => item.name);

// Filter out hidden files unless -a
if (!options.a) {
fileNames = fileNames.filter(name => !name.startsWith("."));
}

// Print output
if (options["1"]) {
// One file per line
fileNames.forEach(name => console.log(name));
} else {
// Default: space-separated (like regular `ls`)
console.log(fileNames.join(" "));
}
} catch (err) {
console.error(`Error reading directory "${dir}":`, err.message);
process.exit(1);
}

38 changes: 38 additions & 0 deletions implement-shell-tools/wc/wc.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";

program
.name("wc")
.description("Custom wc CLI - count lines, words, bytes")
.argument("<file>", "File to process")
.option("-l", "Print the number of lines")
.option("-w", "Print the number of words")
.option("-c", "Print the number of bytes")
.parse();

const options = program.opts();
const filePath = program.args[0];

try {
const data = await fs.readFile(filePath,"utf-8");



const lineCount = data.split("\n").length;
const wordCount = data.trim().split(/\s+/).length;
const byteCount = data.length;

const showAll = !options.l && !options.w && !options.c;


const output = [];
if (options.l || showAll) output.push(lineCount.toString().padStart(8));
if (options.w || showAll) output.push(wordCount.toString().padStart(8));
if (options.c || showAll) output.push(byteCount.toString().padStart(8));
output.push(filePath);

console.log(output.join(" "));
} catch (err) {
console.error(`Error reading file "${filePath}":`, err.message);
process.exit(1);
}
21 changes: 21 additions & 0 deletions 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 package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"commander": "^14.0.0"
}
}