- The Commander.js is a NPM module to develop commands, sub-commands and options to run on the system terminal.
- You can find the commander.js NPM module in following link:-
https://www.npmjs.com/package/commander
- In this demo, “We will learn to create command and options using Commander.js”.
- We will be building a command named devise and subcommand info with 2 options –machineName and –machineType. The following screenshot shows the devise command usage.
- To develop devise command we have created a project name called devise.The following screenshot shows devise project structure.
- The commander NPM module can be installed using npm install commander –save command.The following terminal shows the installation of commander module locally:-
- The index.js file contains the code definition of command, subcommand and options. The subcommand info has been defined using action() method.The machineName and machineType options are defined using option() method.We have used 2 methods getMachineName() and getMachineType() which return the name of the system and type OS used.
#!/usr/bin/env node //Node.js Command development using Commander.js var commander = require("commander"), process = require("process"), os = require("os"); //Returns name of the host machine var getMachineName = function(){ return os.hostname(); }; //Returns type of OS var getMachineType = function(){ return os.platform(); }; //Defining command devise info --machineName --machineType commander.command("info") .description('returns the details of the host machine.') .option("--machineName","return machine name") .option("--machineType","return machine type") .action(function(cmd){ var resultObject = {}; if(cmd.machineName){ resultObject.machineName = getMachineName(); } if(cmd.machineType){ resultObject.machineType = getMachineType(); } return resultObject; }); commander.parse(process.argv);
- The package.json contains the special property bin which contains a key-value pair . The key represents the command name and value contains the entry point of execution.
{ "name": "devise", "version": "1.0.6", "description": "Command line tool for host machine details", "bin": { "devise": "./index.js" }, "keywords": "command, cli", "repository": { "type": "git", "url": "https://github.com/saan1984/devise.git" }, "author": "Sandeep kumar patel <sandeep@tutorialsavvy.com>", "license": "ISC", "preferGlobal": true, "dependencies": { "commander": "^2.9.0" } }
- Now we can publish this module to NPM(Node Package Manager) registry using npm publish command. THe following screenshot shows the terminal for publishing NOM module.
- Once the Devise module is published successfully we can find it in NPM repository.The following URL points to the devise command that we published in previous step:-
https://www.npmjs.com/package/devise
- The following screenshot shows the devise module in NPM registry:-
- Now we can install our newly created devise module as other NPM module.The following screenshot shows the installation of devise module globally using npm install devise –global command in terminal
- Now we can run the devise command with subcommands and options.The following screenshot shows the output in terminal with devise command in execution which logged the machine name and type:-
- The demo code can be found in the following URL:-
https://github.com/saan1984/devise