javascriptnode.jsvorpal.jsinquirervorpal

Close Vorpal instance programmatically


This regards the vorpal CLI for node.js, like so:

const vorpal = require('vorpal')();

The user can close vorpal with Ctrl-C, but how can I close vorpal programmatically?

For example, if a vorpal terminal session is opened, but no further stdin is received after 25 seconds, I want to programmatically close vorpal, here is what my code looks like:

  vorpal
  .delimiter(shortCWD + chalk.magenta(' / suman>'))
  .show();

  const to = setTimeout(function () {
    vorpal.close();  // >>>  I want to programmatically close vorpal...but vorpal.close() is not a function
    process.stdin.end();
    log.error('No stdin was received after 25 seconds..closing...');
    p.killAllImmediately();
    process.exit(0);
  }, 25000);   // if no stdin has been received after 25 seconds

  process.stdin
  .setEncoding('utf8')
  .resume()
  .on('data', function customOnData(data: string) {
    clearTimeout(to);
    if (String(data) === 'q') {
      log.warning('killing all active workers.');
      p.killAllActiveWorkers();
    }
  });

Solution

  • While Vorpal doesn't have a close function, you can run the native exit command programmatically using exec or execSync. You may also be interested in hide for just hiding Vorpal.

    Code:

    vorpal.exec("exit");