All Projects → sw-yx → cli-cheatsheet

sw-yx / cli-cheatsheet

Licence: MIT License
helpful libraries for *building* CLIs. Not a list of CLIs.

this list is now archived - look inside https://github.com/sw-yx/brain if you would like an updated list! thank you.

cli-cheatsheet

helpful libraries and resources for building Node.js CLIs. Not a list of CLIs.

I put together a 90 minute Egghead.io Course and Workshop on how to put these tools together, if you like video learning. I also make some money if you watch those videos if you want to help :)

CLI Design Thinking

  • 12 Factor CLI Apps (Blogpost, Talk): Jeff Dickey's list of requirements for UX.

  • Heroku CLI Style Guide (Guide, Talk, Talk): Heroku's CLI Style Guide.

  • CLI Guidelines (Guide, Source): An open-source guide to help you write better command-line programs, taking traditional UNIX principles and updating them for the modern day.

  • The Poetics of CLI Command Names: Command naming anti-patterns and excellent names

  • CLI State Machines (Gist): My little thoughts on state management

  • Add a dry run mode for expensive commands like gatsby dry-run

  • The Art of Command Line

Frameworks

  • Commander: Built by TJ, used in create-react-app, vue-cli, and many others. Key feature: pluggability. Vorpal is another framework inspired by Commander and is seeking maintainers
  • Oclif: Built by Heroku, used in Heroku and Salesforce CLI's. Key feature: pluggability.
  • Sade: Built by lukeed, used in tsdx. Key feature: lightweight?
  • Gluegun: Built by Infinite Red, used in Ignite and AWS Amplify. Key feature: templating/filesystem
  • Ink: Built by Vadim & Sindre. Key Feature: React Components and Yoga Layout. See also import-jsx
  • Scritch: Built by Jamie, used at Eventbrite. Key Feature: compose multiple scripts regardless of language into one CLI.
  • Yargs: Built by bcoe, used by webpack-cli, mocha, react-native, nyc, and 14,343 other modules.
  • arg: Built by Vercel, used by vercel, ncc, micro, serve, and many others. Key Feature: tiny
  • cac: Built by Egoist, used by create-nuxt-app and many others.
  • Pastel for React-ink - used in Twilio https://www.twilio.com/blog/building-conference-cli-in-react

New: see CLUI https://blog.repl.it/clui

Inspiration from other languages - the http://charm.sh/ universe.

Utility Libraries

Performance Optimization

For performance logging, you can try process.htime() instead of console.time() (altho the latter uses the former under the hood)

Update Management/Nagging

Context/Config

Context from Git/CI

Context from filesystem/PATH

Config validation

Loading config from json, rc file, etc*

don't need but nice to know: https://www.npmjs.com/package/read-package-json

Beware the race condition when you check if file exists and then open it. the file could be deleted in betwee. instead, just open the file and handle the error if it doesnt exist.

Loading/Storing config from a persistent store

⚠️ Be aware of the XDG spec. Sindre's libraries use env-paths to get paths compliant with this.

🌟Input

Stdin Parsing

Argument Parsing

⚠️ Your framework may come with parsing built in

Input/Stdin/Argument Processing

make sure to also normalize inputted stuff before you compare

🌟Processing

Command execution

Remember Windows users for cross-platform nodejs (Guide). e.g. dont execute pkg.json or shell commands with single quotes instead of escaped double quotes

Sometimes processes can spawn processes. This is troublesome for watch/reload features. Kill them all with tree-kill.

You'll probably also use these in conjunction with port monitors (e.g. the process you're working with opens a port, like CRA for Netlify Dev):

If child_processes are going to be a key part of your CLI, be sure to read the docs and this guide to be aware of the API.

You may also want to poll for conditions to be true to execute async processes:

Spinners/Loaders

Templating

Temp folders

🌟Output

Files

Icons & Coloring

Note that you may want to offer the option to FORCE_COLOR in CI logging.

PrettyPrinting

there are also react based renderers

Boxing

Tables

Banners

Debug Logging

You often also find specialized logger tools for each domain, like @dazn/lambda-powertools-logger for AWS Lambda, but at their core they all support the core pattern of Log Levels DEBUG, INFO, WARN and ERROR.

Plugin/Release Management

⚠️ None of these are offline-first. Keen on finding one that respects offline first.

Dependency installs and Upgrading Scaffolds

import { spawn, hasYarn } from 'yarn-or-npm'
// optionalDir for installing?
const prefixFlag = hasYarn() ? '--cwd' : '--prefix'
spawn.sync(['install', prefixFlag, optionalDir], { stdio: 'inherit' })

Error Stacks/Error Handling

Serving Files

Misc

New: see CLUI https://blog.repl.it/clui

Other useful lists

Beginner Tutorials

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].