LEARNNode CLI Automation

Name CLI Refactor Code

You need to make sure that your CLI code is understandable. If it isn't then the developers may find some difficulty in understanding it. So to make the code simpler, we use refactoring.

In my name CLI, I have written most of my code inside the index.js file. In my opinion, the file is looking a bit messy as I write more and more code inside it. Let's take a look at how we can refactor this code.

Code Refactoring

I am going to create a data.js file inside the utils directory. Then I am going to cut all of the data that was inside the index.js file and paste it inside this data.js file. If you are also following along with your name CLI then make sure you also cut and paste all the require statements inside the data.jsfile.

The next thing I have to do in order to make everything functional again is to export all the data from this data.js file. Since I have created variables for bio, social, and ad, I am only going to export these variables inside an object like this:

module.exports = {
bio,
social,
ad
}

Now I am going to require this data.js file inside the index.js file and use it. You can see in the image below how I am using the data in the index.js.

using data.js

Wrapping Up

And that is it. I have created a separate data file and using it inside my CLI's main entry point. You can also refactor more code if you like. Just make sure you don't over complicate everything.

Posted by Saad (It's a work in progress: Needs copy editing review by Awais.)