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
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/implement-shell-tools/wc/wc.mjs"
}
]
}
48 changes: 48 additions & 0 deletions implement-shell-tools/cat/cat.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { program } from "commander";
import { promises as fs } from "node:fs";
import process from "node:process";


program
.name("Display the contents of a file")
.description("Should effectively display the contents of a file as cat does even with extra flags")
.option("-n", "The character numbers all the lines of output.")
.option("-b", "The character numbers only the nonempty lines of the output")
.argument("<files...>");

program.parse();

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

if (files.length === 0) {
console.error(`Expected at least 1 argument (a path) to be passed but got ${files.length}.`);
process.exit(1);
}

let lineNum = 1;

for (const file of files) {
const content = await fs.readFile(file, "utf-8");
const lines = content.split("\n");

for (const line of lines) {
if (options.b) {
if (line.trim() !== "") {
console.log(`${(lineNum).toString().padStart(6)} ${line}`);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is looks like there is some repetition of code across your different output options. Can you think of a way to clean this up?

lineNum++;
}
else {
console.log(line);
}
}
else if (options.n) {
console.log(`${(lineNum).toString().padStart(6)} ${line}`);
lineNum++;
}
else {
console.log(line);
}
}

}
20 changes: 20 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": "^14.0.0"
}
}
38 changes: 38 additions & 0 deletions implement-shell-tools/ls/ls.mjs
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";
import process from "node:process";

program
.name("ls")
.description("This is a program to display the childern of a directory.")
.option("-1, --one", "This is to list all the children of a directory, one per line.")
.option("-a", "The flag to list the children of a directory one per line in long format.")
.argument("<filepath>")

program.parse(process.argv);

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

if (args.length != 1) {
console.error(`Expected exactly 1 argument (a path) to be passed but got ${args.length}.`);
process.exit(1);
}

const content = await fs.readdir(args[0], "utf-8");

const results = content.join(' ')
// const regex = '/\n/'

const filtered = content.filter((word) => word.charAt(0) !== '.')

if (opts.one) {
console.log(filtered.join('\n'))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you run the original ls command with both -a1 options? How does this compare to your implementation?

}
else if (opts.a) {
console.log(content.join(' '));
}
else {
console.log(filtered.join(' '))
}

20 changes: 20 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.

5 changes: 5 additions & 0 deletions implement-shell-tools/ls/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"commander": "^14.0.0"
}
}
20 changes: 20 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.

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

program
.name("wc")
.description("A program to implement word, character and line counter.")
.option("-l", "Counts the lines in a file")
.option("-w", "Counts the words in a file")
.option("-c", "Counts the characters in a file")
.argument("<filepath...>");

program.parse(process.argv);

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

if (args.length === 0) {
console.error(`Expected at least one argument but received ${args.length}`);
process.exit(1);
}

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

for (const file of args) {
const content = await fs.readFile(file, "utf-8");
const linesArr = content.split('\n');
const lines = linesArr[linesArr.length - 1] === '' ? linesArr.length - 1 : linesArr.length;
const words = content.split(/\s+/).filter(Boolean).length;
const chars = content.length;

totalLines += lines;
totalWords += words;
totalChars += chars;

let output = '';
if (opts.l) output += `${lines} `;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is quite a bit of duplication in your printing here and where you do total output. Can you think of a way to make this code cleaner?

if (opts.w) output += `${words} `;
if (opts.c) output += `${chars} `;
if (!opts.l && !opts.w && !opts.c) output += `${lines} ${words} ${chars} `;
output += file;
console.log(output);
}

if (args.length > 1) {
let totalOutput = '';
if (opts.l) totalOutput += `${totalLines} `;
if (opts.w) totalOutput += `${totalWords} `;
if (opts.c) totalOutput += `${totalChars} `;
if (!opts.l && !opts.w && !opts.c) totalOutput += `${totalLines} ${totalWords} ${totalChars} `;
totalOutput += 'total';
console.log(totalOutput);
}
Loading