javascriptnode.jsvorpal.js

Is it possible to create a Vorpal application which the command's output goes directly to the terminal, instead of it's own shell?


When you create Vorpal application, at least from what I see in the docs, it creates it's own shell. First, you enter that shell, and then you start executing commands. Like this:

user@computer: quotr
quotr$
quotr$ snapshot
You triggered `snapshot`.
quotr$

In the above example, the snapshot command is being executed "inside" the Vorpal shell. It's output doesn't go directly to the terminal.

What I'm looking for is something like this:

user@computer: quotr snapshot
You triggered `snapshot`.
user@computer:

How can I achieve that with Vorpal?


Solution

  • Yes, you can do this with the vorpal.parse method, which will parse the arguments passed into Vorpal and execute them.

    var vorpal = require('vorpal')();
    
    vorpal
      .command('snapshot')
      .action(function (args, cb) {
        this.log('You triggered `snapshot`.');
        cb();
      });
    
    vorpal.parse(process.argv);
    

    You can have the app exit after completing the command simply by omitting vorpal.show(). Because there is no prompt, Node realizes there is nothing left to do, and the process will exit naturally.