Unlock the Power of CLI Tools with Node.js
What is a CLI Tool?
A CLI tool is a command-line interface that allows you to run tasks or operations directly from your terminal or command line prompt. With Node.js, you can create custom CLI tools tailored to your specific needs.
Create Your First CLI Tool
Let’s dive into creating a simple CLI tool called todos-cli. This tool will enable users to view their to-do list, add items, and mark tasks as done.
Step 1: Set Up the Project
Create a new directory for your project and initialize a Node.js project using npm init. Install the required packages, including commander, chalk, and conf.
npm init -y
npm install commander chalk confStep 2: Create the CLI Tool
Create a file called index.js and require the necessary packages. Use commander to declare commands, options, and actions. For example, let’s create a list command to display the to-do list.
const program = require('commander');
program
 .version('1.0.0')
 .description('A CLI tool for managing your to-do list');
program
 .command('list')
 .description('Display your to-do list')
 .action(() => {
    // implement the logic to retrieve and display the to-do list
  });
program.parse(process.argv);Step 3: Implement the List Command
Create a separate file for the list command and implement the logic to retrieve and display the to-do list using conf. Use chalk to add some color to your output.
const conf = require('conf');
const chalk = require('chalk');
const list = () => {
  const todoList = conf.get('todo-list');
  console.log(chalk.blue('Your to-do list:'));
  console.log(todoList.map((task, index) => `${index + 1}. ${task}`).join('\n'));
};
module.exports = list;Step 4: Add More Commands
Add an add command to allow users to add new tasks to their list. Implement the logic to store the new task using conf. Create a mark-done command to mark tasks as done, with optional parameters to specify which tasks to mark.
program
 .command('add ')
 .description('Add a new task to your to-do list')
 .action((task) => {
    const todoList = conf.get('todo-list') || [];
    todoList.push(task);
    conf.set('todo-list', todoList);
  });
program
 .command('mark-done [taskNumbers...]')
 .description('Mark tasks as done')
 .action((taskNumbers) => {
    const todoList = conf.get('todo-list');
    taskNumbers.forEach((taskNumber) => {
      todoList[taskNumber - 1].done = true;
    });
    conf.set('todo-list', todoList);
  });Step 5: Test and Publish
Test your CLI tool by running it globally using npm install -g. Once satisfied, publish your tool to npm using npm publish.
npm install -g.
npm publishUpdating Your CLI Tool
To update your CLI tool, use npm version to increment the version number and npm publish to push the changes to npm.
npm version minor
npm publishThe Possibilities are Endless
With Node.js and CLI tools, the possibilities are endless. From automating workflows to simplifying tasks, the power is in your hands. So, what are you waiting for? Create something awesome today!