Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create your own npm starter-kit?

Starte of the art: Popular node.js frameworks come with their own starter-kits that act as project templates, such as React comes with the starter-kit . When user want to create a new react application, they can use the short hand npx create-react-app my-app. ... to automagically create the whole project scaffold with all baseline project files, directories, and package dependencies. When using the starter-kit, npx resolve template variables inside the starter-kit files i.e. if package.json contains an entry {{my-application-name}}, npx will as for user input to replace the template variable with e.g. "my-first-app". React is just one example for such a starter-kit.

Problem statement: I would like to build my own starter-kit for my own node.js framework. At the first glance, it seems that one just follow the convention to name my package starting with "create-" and use the template-{{vars}} at the right places. I gave it a try and started copy&paste&replace, however npm install did not work, since unresolved template variable result in build errors.

Does somebody know a tutorial how to create your own npx starter-kit? Do somebody know how one can build and publish a new starter kit?

like image 559
dinkelaker Avatar asked Aug 27 '18 14:08

dinkelaker


1 Answers

yes, you can build your own npx starter by using the CLI helper packages on the npm registery

Steps for how to build your own npx starter...?

  1. you can use args or commander to get the cli inputs ( node argv withing the process object ) and parse it into object so you can keep track of what the user want

  2. based on the the previous parsed object from the CLI now you can gather more information by using inquirer where you can ask yes/no questions or choose one of many options.

  3. after the user answer all the questions you now can write the logic for creating the required starter based on the inputs of the user

  4. you can use execa to perform process command such as git init , git clone or any command that might comes to your own mind

  5. you can use listr to prettify your own task lists with a beutiful spinner

  6. you can use chalk to print the line in the CLI with colors and make it easier to read make the message green if it's success or red when it's failed

for more explanation on how it works together you can see this video

like image 144
Muhammed Paqer Avatar answered Oct 16 '22 23:10

Muhammed Paqer