NodeJS Command Line Interface | Making a CLI with NodeJS and Express
Project Layout
Create a folder that will be root folder (suggested name node-cli). Inside that folder create three more folder bin, commands, data. now create all file according to project flow image at the top
bin/player.js
#!/usr/bin/env node // SUPER COMMANDS const program = require('commander'); program .version('1.1.1') .command("show", "Show all players") .command("set", "add or remove player") .parse(process.argv); // console.log("hello from player");
bin/player-set.js
// SUB COMMANDS // console.log("Add or remove players"); const Set = require('../commands/Set'); const set = new Set(); const program = require("commander"); program.command('add').description("Add player informations").action( set.addPlayer); program.command('remove').description("remove player informations").action(set.removePlayer); program.parse(process.argv);
bin/player-show.js
// SUB COMMANDS const program = require('commander'); const colors = require('colors'); const all_player = require('../data/player-data'); // const console.log(all_player);
commands/Set.js
const inquirer = require('inquirer'); const colors = require('colors'); const playerData = require('../data/player-data'); class Set { addPlayer = async () => { console.log("Adding player to the player set"); let newPlayer = await inquirer.prompt([ { type: 'input', name: "name", message: "Enter player name! ==> ".cyan }, { type: 'list', name: "game", message: "Enter the game of the player! ==> ".cyan, choices: ["Cricket", "Football"] // choices[, default, filter, loop] }, ]); playerData.push(newPlayer); playerData.forEach((pd_value, pd_index) => console.log(pd_index, pd_value)); } removePlayer = async () => { console.log(playerData.length); playerData.forEach((pd_value, pd_index) => console.log(pd_index, pd_value)); let indexNum = await inquirer.prompt([{ type: 'number', name: "player", message: "Enter the index number which you want to remove! ==> ".cyan }]); // console.log(indexNum.player); playerData.splice(indexNum.player, 1); console.log("-------------------"); playerData.forEach((pd_value, pd_index) => console.log(pd_index, pd_value)); } } module.exports = Set;
data/player-data.js
const all_player = [ { name: "Virat Kohli", game: "Cricket" }, { name: "Gayle", game: "Cricket" }, { name: "AB DE Villiers", game: "Cricket" }, { name: "Lionel Messi", game: "Football" }, { name: "Cristiano Ronaldo", game: "Football" }, { name: "Neymar JR", game: "Football" }, ]; module.exports = all_player;
package.json
{ "name": "player", "version": "1.0.0", "description": "", "main": "index.js", "bin": "./bin/player.js", "dependencies": { "colors": "^1.4.0", "commander": "^6.2.0", "inquirer": "^7.3.3" }, "devDependencies": {}, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
Open terminal or command prompt in root folder and run essential commands
npm install npm link sudo npm link
Now you are able to run commands of node.js cli
player --version player --help player show player set player set add player set remove
No comments: