Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Build Your Own CLI App in 5 Minutes (ClackJS + Node + NPM Tutorial)

Posted on Aug 5 Today, I’m going to show you how to build your own command line application and deploy it as a NPM package. I will be using npm, Node.js, and Clack to build this.You can even try it out for yourself right now by running:npx cli-trivia-gameHere's a sneak peek of the trivia game (full .gif of play-through + answers at the bottom):1) Navigate to your command line and run:npm init -yThis is going to initialize a new project, and the -y flag automatically answers “yes” to all the prompts. This generates a package.json file that has all the default settings without having to manually provide details.2) Next, you will run:npm -install @clack/promptsOnce you do that, you’ll have a framework that lets you create things like spinners, multi-selection, and self-validated cancel logic all with just a few lines of code. 3) Let’s leverage Clack’s built-in setup logic. To create an intro, I’m going to call intro here and pass in this string that let’s the player know they’re about to play in a trivia quiz that tests their CLI knowledge.4) Since this is a trivia quiz, let’s ask some questions. First, I’ll create a class called Question that requires a question string, an array of potential answers, and the index of the correct answer. 5) Then, I’m going to create 10 question objects using this class template.6) Now that we’ve got our questions, how do we display them on the command line? We’re going to use Clack’s select component to do this. All I have to do is pass in the question, initial value, and options, and voila.7) To make it feel a little more sophisticated, I’m going to add a spinner after the user answers just so they have time to process the UI and makes the program feel intelligent. 8) And then to tie up, I’m going to declare a global variable called totalCorrect which will increment when the player answers a question correctly. 9) At the end, I’ll add some logic to display different outros based on how many questions the player got correct.And the best part - we’re going to Publish this as an npm package so that every developer in the world can download on their machines if they choose to. It’s actually pretty easy.1) Go to package.json and add “bin”:”./index.js”2) Run npm login3) Run npm publish4) Then run npx cli-trivia-game3 Min Youtube version (if you prefer that) 👇Done! But before you start this project for yourself, take into consideration that Clack is really lacking in the documentation. Anything fairly complex, you won’t benefit from the pre-made components of clack/prompts. Here's a list of other CLI frameworks you can use instead.Check out the full code for the project here:https://github.com/theRubberDuckiee/cli-trivia-gameJust like that, you have made and published your first CLI application! Congrats :)And, if you'd like to know the answers to all 10 questions in my trivia game, here you go:Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse The Jared Wilcurt - Jul 30 James Hubert - Jul 31 Matt Angelosanto - Jul 19 Željko Šević - Jun 30 Once suspended, Therubberduckiee will not be able to comment or publish posts until their suspension is removed. Once unsuspended, therubberduckiee will be able to comment and publish posts again. Once unpublished, all posts by therubberduckiee will become hidden and only accessible to themselves. If therubberduckiee is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Jessica Wang. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag therubberduckiee: therubberduckiee consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging therubberduckiee will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Build Your Own CLI App in 5 Minutes (ClackJS + Node + NPM Tutorial)

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×