10x Developer
  • Introduction
  • HTML
    • HTML DOM Jquery
    • HTML: Form
    • CSS
    • CSS Layout
    • CSS Flexbox
    • CSS Grid
    • SASS/LESS
    • CSS/LESS/SASS Cookbook
    • Bootstrap
  • JavaScript
    • JavaScript
    • ES6
    • TypeScript
    • JavaScript Testing
    • JavaScript Event Loop
    • DOM
    • Web APIs
    • JSON
    • RegEx
    • Functional Programming
    • JavaScript Lib
    • CoffeeScript
    • CoffeeScript to ES6 cheatsheet
  • Angular.js
    • Angular
    • Angular Cookbook
    • Angular Mistakes I Made
    • Angular 1.x
  • React.js
    • React.js
  • Node
    • Node.js
    • CLI command line tool
    • Electron / Atom
    • NW.js (node-webkit)
  • Serverless
    • AWS Lambda
    • Google Cloud Function
    • Actions on Google / Google Assistant
  • Full Stack Development
    • HTTP
    • Meteor
    • MongoDB
    • Digital Ocean
    • UI
    • Sketch
    • Web Dev Resources
  • Lang
    • Ruby
  • Know Your Tools
    • Chrome DevTools
    • Editor: VS Code
    • Editor: Vim
    • Editor: Sublime
    • Editor: Atom
    • Windows
    • Git
    • Linux / Bash
    • Mac
  • Cheatsheets
Powered by GitBook
On this page
  • How to create a package that can be used as cli (and package)?
  • Folder Structure
  • Useful Libs
  1. Node

CLI command line tool

PreviousNode.jsNextElectron / Atom

Last updated 6 years ago

How to create a package that can be used as cli (and package)?

Folder Structure

  • use the generator:

  • create a bin folder, create a file called: 'my-cli'

    #!/usr/bin/env node
    require(__dirname+'/../dist/my-cli')
  • Update your package.json:

"files": [
    "dist",
    "bin"
  ],
"main": "./dist/index.js",
"bin": {
    "my-cli": "./bin/my-cli"
},
  • So the flow is:

    • We tell package the bin file is in the bin folder

    • The bin file invokes the actual script in dist folder

    • We develop the script in lib folder and babel the source ('prepublish') to dist folder

    • To test locally on dev machine

      npm link
      # then start using the cli version
    • To publish internally, do

      npm version minor # update version
      npm pack # this will create the abc-1.0.0.tgz file
      
      # to install and use
      npm install -g abc-1.0.0.tgz

Reference:

Useful Libs

    • to run, do coffee make testName

interactive input

cli helper:

full blown cli tool:

isolated cli, similar to REPL:

shell commands, and make tool in js:

so you can have a make file like this:

color in cli:

A list of cli tools:

https://github.com/yeoman/generator-node
https://github.com/bevry/cson
https://docs.npmjs.com/files/package.json
http://javascriptplayground.com/blog/2015/03/node-command-line-tool/
https://docs.npmjs.com/misc/scripts
https://github.com/SBoudrias/Inquirer.js
https://www.npmjs.com/package/meow
https://github.com/tj/commander.js
https://github.com/dthree/vorpal
https://github.com/shelljs/shelljs
https://github.com/madrobby/zepto/blob/master/make
https://www.npmjs.com/package/chalk
https://github.com/sindresorhus/awesome-nodejs#command-line-apps