Skip to content

dayflowerio/nodejs-cli-howto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 

Repository files navigation

Node.js CLI HOWTO

Docs for creating an NPM distributable Node.js CLI following best practices

Requirements

  • nvm
  • gh - Github CLI
  • yarn

Initializing repo

  1. Setup repo and basic folder structure.
$ nvm install --lts
$ nvm use --lts
$ gh repo new
$ cd name-of-new-repo
$ mkdir bin
$ mkdir -p src/lib
$ mkdir -p src/commands
$ cat "#!/usr/bin/env node\n\nprocess.on('SIGINT', () => {\n  console.log('Process interrupted. Exiting gracefully.')\n  process.exit(0)\n})\n" > ./bin/cli.js
$ chmod a+x ./bin/cli.js
$ touch src/command.js
$ yarn init
$ yarn add -D eslint jest babel-jest @babel/core @babel/cli @babel/preset-env semver @stylistic/eslint-plugin eslint-plugin-jest standard @create-jest @jest/globals ts-node typescript @tsconfig/node20
$ yarn add @inquirer/prompts chalk commander dotenv ora configstore
$ yarn eslint --init
$ yarn create-jest
$ yarn tsc --init
$ yarn add .
$ yarn commit -m 'added CLI scaffolding'
$ yarn link
  1. Add bin clause to package.json
...
  "bin" {
    "command-name": "./bin/cli.js"
  },
...

Folder structure

.env
.gitignore
README.md
bin/
  cli.js
package.json
sample.env
src/
  command.js
  lib/
  commands/
yarn.lock

Useful packages

  • semver - Semantic version management
  • inquirer - CLI interactivity
  • chalk - CLI terminal colorization
  • commander - CLI framework
  • dotenv - Read .env files
  • ora - CLI terminal loading indicators
  • configstore - Configuration state management

Resources

About

Docs for creating an NPM distributable Node.js CLI following best practices

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published