Newton’s Apple is a Command Line Interface(CLI) tool to simplify component creation for developers working on ReactJS projects. Running a simple command, it will create a file with the name you specify and generate the boilerplate code for a typical React component.
And Node came to the rescue! I had only used Node, with Express, as a backend server before when creating Retrocade, so figuring out how to get it to work as a CLI took a little research. Lucky for me, there are some wonderful developers out there who took the time to write up some guides that really got me moving in the right direction:
- Building Command Line Tools with Node.js
- Writing Command-Line Applications in NodeJS
- Creating a Project Generator with Node
Turns out, it’s pretty much as easy as putting this at the top of your entry file:
Then, to give your app a command, simply add this to your package.json file:
With those simple instructions, that was pretty much all I needed to get going!
Next, I needed to figure out what tools I wanted to use in the project. I knew in the back of my mind that I wanted to keep it as lightweight as possible, but at the same time it just didn’t make sense not to take advantage of some great tools that other developers were providing to the community.
I saw that both of these were being in many projects similar to mine, so after reading through the docs and examples, I knew they would both be essential for what I wanted to do with Newton’s Apple.
With Commander, I could not only easily parse arguments passed back from the user, but also build a nice help guide at the same time. Commander also allows for very clean, easy to read code.
Inquirer, on the other hand, helps by allowing the application to “communicate” with the user by asking them questions. I wanted to allow the user to select from a list of lifecycle methods that they wanted to include and Inquirer delivers!
Simply passing a few lines of code:
Will provide you with a nice, easy list to select from when the command is passed:
The next problem I encountered was how to persist settings for a user. I did not want a user to have to select which lifecycle methods they wanted to include everytime they created a component. Also, I already had more advanced features I wanted to include in the future, so I needed a way to save options.
Enter ConfigStore - the perfect solution to my problem.
Now upon install, a small text file is created on the user’s computer that will save config options. Plus, ConfigStore offers a host of methods that allow for easy CRUD updates of those options.
The last problem I encountered was one I struggled with weather to write methods myself or use a library. When creating these files for components, the fs module built into Node was great for creating a file in the current directory, a directory that already existed, or even creating a directory and a file inside that directory. But once I started trying to pass a long path that didn’t exist, I quickly ran into issues.
While I certainly could have called Node’s fs functions recursively and written in error checking myself, it seemed silly when such a great library exists. This is actually a direct replacement for the default fs as you can call the fs methods from this library as well. And it’s as easy as:
Overall, I am very happy with the current progress of Newton’s Apple. I only released version 1.0.0 a couple of weeks ago and I already have over 600 downloads and 9 stars on Github, so I feel confident that it’s a tool people want to use. I still have big plans to add Redux boilerplate, custom methods, and default file structure support so I believe it will continue to grow.