How to write your own command line interface?

Cüneyt
5bayt
Published in
4 min readMay 19, 2020

This is about how you create your custom command line interface, generate projects and push them into github repository.

Why do we need that?

If you’re building a lot web applications or you are working in a corporate company that means you have your initial project with your own custom design and architecture. No developer would want to write the login mechanism for an application over and over again. This is going to be an example of creating a project with your own command line interface.

The important question here is “What do you need?”.

This is going to be a good starting for creating own cli and you can go on with your custom architecture.

Application is a simple node.js application with an index file in it and some modules that i have seperated for custom configurations. The libraries that i have used are below;

I named my cli as “oceanic” you would remember if you have watched the tv series “Lost ” :)

This is my directory structure.

How to make your command global to use everywhere?

To your package.json file.

After you have added the directory which will work under “bin” you need to install your package globally with;

npm install -g

And add your index.js file. This is accually were that i have used figlet package.

Then you can use your comand like;

Second phase is about github authentication. I have referenced the article very useful below;

As in the article i added a file called github.js that provides authentication under my lib folder. There is another library here that configstore to store the github authentication token.

This is a method also in my index.js file.

This is also github.js file where the methods i call from index.js file, under my lib. Also referenced from the article above.

After i atuhenticated for github i can create a project as my wishes. Then i added a method that is “run” to my index.js file. It includes the steps during the project being create.

That run methods needs also another two files project.js and repo.js. One hand project.js executes an npm commad on the other hand repo creates a repository and adds all files to your github repository. To build a project my project.js file is below to create for example an angular project;

Here in my project.js file there is an antoher file inquirer for asking some questions and get answers. askProjectDetails() method in my inquirer.js file is;

If you follow the steps on my run method i have created the application on my root directory then i returned with project information in my method again. Then i will create the repository with the method in my github.js file.

That returns the url of my repository then i need to add all my files created and push to my remote repository.

You can check your github repositories :)

That is the flow;

You can add much more configurative options good luck with that :)

Thanks for reading.

--

--