Progress on the CLI — Sept 23, 2020

Alexander Hugh
3 min readSep 23, 2020

So…this has been a week. First week back to classes and it’s quite overwhelming I have to say. However, we didn’t come here to hear me grovel. Lets get into what I’ve learnt and what I have tried with my CLI.

So for those who didn’t read my last post (you should, it introduces me to you wonderful readers) I’m in DPS909 and my first project is to create a CLI tool that will check for dead links. I chose to make it in JavaScript with node.js and I struggled initially as I had no clue, not even the faintest on how to make one. As it turns out, actually making the cli is quite easy! There’s quite a few tutorials you can find such as this one written by the people at Twilio, or this one on Sitepoint. However, I used this one written by the folks at Okta to get me started.

I won’t go through the entire process since I’m no professional and I’m afraid that I’ll provide bad information. However, in brief, the steps I did was:

1: make a directory with the name of your CLI and inside it run the good ol’ npm init
2: create a bin folder with an index.js inside with this “shebang” at the top: #!/usr/bin/env node
3:change the main value in your package.json into bin/index and add a bin key below license.
4: open the terminal and inside your project root enter node . which will run your index.js inside the terminal! Awesome!
5: To be able to use this CLI from anywhere on your machine (as I would like to for this project) enter in the terminal npm install -g to install your CLI globally! Even more Awesome!

Then you have a CLI ready to use whenever! I very, VERY, VERY, briefly went over the steps involved and you shouldn’t treat this as a tutorial, please look at the links above the list to see how to actually do it, especially the one by Okta which is the one I used.

The CLI in action!
The CLI so far in action!

After I got a CLI that actually works I set out and tried to make it colorful with this great node module called Chalk! It’s super simple to use and the documentation for it can all be found here! Then I added the capability to accept arguments for my CLI with another module called Yargs. This allowed me to have not only arguments but also a -help that comes built in. Once again, look at the Okta tutorial for further information.

As of now I’m attempting to figure out how to use fs.readFile() to get a file name the user would pass in as an argument and then colour said links! I know I’ll need to use a regex to grab the individual links from the file which I’ll have to do some hefty research because my knowledge in regex is very lacking. Then, it’s a matter of debugging, beautifying and publishing!

And that’s all for this progress report! Hope you enjoyed this and if you have any ideas or criticisms on what I can do or am doing, please leave a comment and I will respond as soon as I can.

Regards
-Alexander Hugh

--

--

Alexander Hugh

Student in software development at Seneca college, Ontario Canada. Primarily going to talk about programming stuff but may talk about other hobbies here.