'Calling one command in Yargs calls other commands as well

I am learning nodejs and yargs and tried to implement it in my code using command function.

I am trying to make a CLI based note taking app.

I have two files app.js and utils.js, I run app.js and utils.js is imported in app.js to use functions in it.

There is a problem that I cannot debug, when calling app.js with remove option it is automatically also calling add command even when it is not being called explicitly by remove command.

Input:

node app.js remove --title="hello"

Output:

{ _: [ 'remove' ], title: 'hello', '$0': 'app.js' }
Already exists!
Operation successful!

This is my app.js:

// import modules
const validator = require('validator');
const yargs = require('yargs');
// const chalk = require('chalk');
const utils = require('./utils.js');

// version

yargs.version('1.0.0');

const argv = yargs.argv;
console.log(argv);
const command = argv._[0];

// commands
yargs.command({
    command: 'add',
    describe: 'Add a new note',
    builder: {
        overwrite: {
            describe: 'Overwrite the existing file',
            demandOption: true,
            type: 'boolean'
        },
        title: {
            describe: 'Title of the note',
            demandOption: true,
            type: 'string'
        },
        body: {
            body: 'Body of the note',
            demandOption: true,
            type: 'string'
        }
    },
    handler: utils.addNote(argv.overwrite, argv.title, argv.body)
});

yargs.command({
    command: 'remove',
    describe: 'Remove a note by its title',
    builder: {
        title: {
            describe: 'Title to search for',
            demandOption: true,
            type: 'string'
        }
    },
    handler: utils.removeNote(argv.title)
});

// eof
yargs.parse()

This is my utils.js:

// import
const fs = require('fs');

// load notes
function loadNote() {
    try {
        const dataBuffer = fs.readFileSync('notes.json');
        const stringData = dataBuffer.toString();
        const dataJson = JSON.parse(stringData);
        return dataJson;
    } catch (e) {
        return [];
    }
}

// add note
function addNote(overwrite, title, body) {
    const newNote = {
        "title": title,
        "body": body
    };

    const dataJson = loadNote();
    if (overwrite) {
        fs.writeFileSync('notes.json', JSON.stringify([newNote]));
        console.log("Operation successful!");
    } else {
        let flag = true;
        dataJson.forEach(function (object) {
            if (object.title === title) {
                flag = false;
            }
        });
        if (flag) {
            dataJson.push(newNote);
            fs.writeFileSync('notes.json', JSON.stringify(dataJson));
            console.log("Operation successful!");
        } else {
            console.log("Already exists!");
        }
    }
}

// remove notes
function removeNote(title) {
    const dataJson = loadNote();
    dataJson.filter((object) => object.title !== title);
    fs.writeFileSync('notes.json', JSON.stringify(dataJson));
    console.log("Operation successful!");
}

// export
module.exports = {
    addNote: addNote,
    removeNote: removeNote,
};


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source