FEATURED POSTS
PAGES
note: Series start along with dev notes: Let's Build: NPM Dependency Checker
Previous part: Let's Build: NPM Dependency Checker - Part 2
Project Setup
GitHub: NPM Dependency Checker branch: app-setup
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.
mix new ndc
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:
escript: [main_module: Ndc]
The file should look something like this when you're done:
def project do
[app: :ndc,
version: "0.1.0",
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
escript: [main_module: Ndc], ## <---- this is what you added
deps: deps()]
end
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:
defmodule Ndc do
def main(args) do
IO.puts "Daylight's Burning!"
end
end
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:
defmodule Ndc do
# Application Start
def main(args) do
args |> parse_args |> process
end
# No arguments passed via command line
def process([]) do
IO.puts "Charmed, I'm sure."
end
# Arguments were passed via command line
def process(options) do
IO.puts "NPM package: #{options[:pkg]}"
end
# Handle parsing of arguments
defp parse_args(args) do
{options, _, _} = OptionParser.parse(args,
switches: [pkg: :string]
)
options
end
end
Test It Out!
Let's make sure this app is working. First, we need to create an executable with Mix:
$ mix escript.build
Once completed, you'll find an executable in the main project directory. Let's run it a couple time to see the output!
$ ./ndc
## Charmed, I'm sure.
$ ./ndc --pkg=warcraft
## NPM package: warcraft
Core Coding
Next up we'll do some core coding for our app.