Skip to content

Anastasiia Hlushkova | Oct2025-1 | Module-Tools | Sprint-3 | Implement-shell-tools - #5

Open
ksbn wants to merge 9 commits into
mainfrom
implement-shell-tools
Open

Anastasiia Hlushkova | Oct2025-1 | Module-Tools | Sprint-3 | Implement-shell-tools#5
ksbn wants to merge 9 commits into
mainfrom
implement-shell-tools

Conversation

@ksbn

@ksbn ksbn commented Apr 6, 2026

Copy link
Copy Markdown
Owner

Implement shell tools: cat, ls, wc

Description:
This PR adds implementations of the following shell tools in Node.js:

cat – supports no flags initially, with -n (number lines) and -b (number non-empty lines) flags.
ls – supports -1 (one entry per line) and -a (show hidden files) flags.
wc – supports counting lines (-l), words (-w), and bytes (-c) for single and multiple files.

All implementations have been tested locally using the sample-files provided in the exercises. The output matches the behavior of the corresponding Unix commands.

Notes:

.DS_Store is ignored via .gitignore.
package.json is included for ES module support in Node.js.

This PR closes the implement-shell-tools task and is ready for review.

@johncowie johncowie Apr 11, 2026

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 a subtle difference in behaviour for the line numbers, when using -n across multiple files:

$ cat -n sample-files/*.txt
    
     1	Once upon a time...
     1	There was a house made of gingerbread.
     1	It looked delicious.
     2	I was tempted to take a bite of it.
     3	But this seemed like a bad idea...
     4
     5	There's more to come, though...


$ node cat.js -n sample-files/*.txt

1	Once upon a time...
2	There was a house made of gingerbread.
3	It looked delicious.
4	I was tempted to take a bite of it.
5	But this seemed like a bad idea...
6
7	There's more to come, though...

I'm not too concerned about the formatting, but when running 'cat -n' it gives you the line numbers relative to each file, whereas in your implementation you are joining the file contents together first, and then adding in the line numbers

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I fixed this by moving the lineNumber variable inside the file loop so it resets to 1 for each file. I also took the opportunity to match cat's formatting by right-aligning line numbers in a 6-character wide field using padStart(6). Thanks for reviewing!

@johncowie johncowie Apr 11, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Couple of things:

First when showing hidden files, ls also shows the routes to the current and parent directory (. and ..) i.e.:

$ ls -1 -a sample-files
.
..
.hidden.txt
1.txt
2.txt
3.txt
dir

$ node ls.js -1 -a sample-files
.
..
.hidden.txt
1.txt
2.txt
3.txt
dir

Secondly, it doesn't look like you support the -1 argument. For ls the difference between supplying and not is:

$ ls
README.md	ls.js		sample-files

$ ls -1
README.md
ls.js
sample-files

whereas yours always returns the filenames one per line.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Thanks for the feedback!

I've fixed both issues:

Added support for the -1 flag — now the output is printed in a single column only when this flag is provided, and defaults to a tab-separated format otherwise.
Updated the -a behavior to include . and .., matching the standard ls output.

Now the results align with the expected behavior for both -a and -1 options.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It looks like there's a bug in the line counting:

$ wc -l sample-files/*
       1 sample-files/1.txt
       1 sample-files/2.txt
       5 sample-files/3.txt
       7 total

$ node wc.js -l sample-files/*
2 sample-files/1.txt
2 sample-files/2.txt
6 sample-files/3.txt

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's because when you do data.split('\n') you also get an empty string afterwards because there's a '\n' at the end of the last line e.g. "Once upon a time...\n" => ['Once upon a time...', '' ].

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

wc also shows the option when flags are supplied, showing the total for the aspects selected, which yours isn't doing e.g.:

$ wc -l sample-files/*
       1 sample-files/1.txt
       1 sample-files/2.txt
       5 sample-files/3.txt
       7 total

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Thanks for pointing this out!

You're absolutely right — the issue was caused by splitting on '\n', which adds an extra empty string when the file ends with a newline. I’ve fixed the line counting to handle this case correctly.

Also implemented the total count output when flags are used, so now it matches the behavior of wc.

Appreciate the detailed review!

@johncowie

Copy link
Copy Markdown

Overall the code is pretty well structured and the tools mostly work. In each case there are some differences in behaviour between yours and the shell tools though.

For each of these, having some unit tests would help you too. If you can separate out the argument parsing and file loading from the core logic, so you have a function that takes the file contents and the options and returns the output, then it should be relatively easy to write tests that verify the core logic of the tool.

If you like we could find some time to pair on this.

@johncowie johncowie left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It'd be good to at least fix the bug in wc, and add support for the -1 formatting option in the ls implementation. I'm less concerned about the other things I raised but if you want to fix those too that'd be cool :)

@ksbn

ksbn commented Apr 14, 2026

Copy link
Copy Markdown
Owner Author

It'd be good to at least fix the bug in wc, and add support for the -1 formatting option in the ls implementation. I'm less concerned about the other things I raised but if you want to fix those too that'd be cool :)

Thanks for the suggestions!

I’ve fixed the bug in wc (the extra line caused by splitting on '\n'), so the counts now match the expected behavior.

Also added support for the -1 formatting option in the ls implementation.

Appreciate the feedback 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants