How to create your own npm starter-kit?
Asked Answered
A

1

10

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?

Arvie answered 27/8, 2018 at 14:57 Comment(3)
For reference, the relevant doc is npm-init npm init <initializer> -> npx create-<initializer>Maenad
I've found some useful insights by following a tutorial on how to build a cli with node jsMaenad
hi, did u find solution for your problem?Ogden
N
2

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

Neace answered 28/7, 2021 at 4:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.