Let's Build a Thing: NPM Dependency Checker - Part 2
Let’s Build: NPM Dependency Checker - Part 2
Series start along with dev notes: Let’s Build: NPM Dependency Checker
Previous part: Let’s Build: NPM Dependency Checker - Part 2
GitHub: NPM Dependency Checker
Let’s get rocking on our new and awesome upcoming application. I’ve created a new branch and am ready to get this baby rolling.
New Project: We need to setup a new project with Mix. Ultimately, we’re going to create a command-line application that you can just call in your terminal.
After you’ve navigated to where you want your project to live, you can run the below command to setup the project with Mix. Note that “ndc” is what I’m calling the app and it’s what we’ll eventually call on the command line to execute it.
We now have a new folder “ndc” that contains our project. In the /lib/ folder is where our main coding will be done. You’ll notice Mix added the main module in there for us (Ndc in the ndc.ex file).
Since we’re going to package this application up for command-line fun, we need to tell Mix what our main module will be. Mix will use ESCRIPT to make this happen.
Mix Modification: Open up mix.exs in your main folder and add the following line to the project function:
The file should look something like this when you’re done:
Add MAIN function: Now that we’ve told Mix where to find the main function to kick things off, we need to make that main function. For now, let’s put a stub in place. Go to the /lib/ folder and open up ndc.exs. Inside of the Ndc module, add the main function.
Your code will end up looking like this:
In case you’re wondering, the main function is passed in any args from the command line. Sweet!
Argument Stubs: Elixir comes with argument parsing built in! It’s called OptionsParser. It’s up to us to handle the options (or lack thereof). For now, we’re going to put in some stubs. As our application grows, we’ll fix em up.
Also we’re going to modify the min function to handle option parsing and to react based on whatever is passed.
Here is the code for ndc.ex:
Test It Out!
Let’s make sure this app is working. First, we need to create an executable with Mix:
Once completed, you’ll find an executable in the main project directory. Let’s run it a couple time to see the output!
Next up we’ll do some core coding for our app.
Let’s Build: NPM Dependency Checker - Part 3