javascriptnode.jsvorpal.js

calling another function during vorpal action


I have just started using Vorpal.js to help build a CLI tool which will ultimately preform a HTTP request against an API endpoint.

I have it asking questions and validating data input, but once I have the input I am having rouble calling another method which contracts the endpoint url from the requested answers.

Here is my code:

    function apiEndpoint (env) {
    if (env === 'INT') {
        return API_ENDPOINT_INT;
    } else if (env === 'TEST') {
        return API_ENDPOINT_TEST;
    } else if (env === 'LIVE') {
        return API_ENDPOINT_LIVE
    }
}

function constructRequestURL (params) {
    let API_ENDPOINT = apiEndpoint(params.env);

    let requestURL = `${API_GATEWAY}${API_ENDPOINT}?asset_uri=${params.asset_uri}&${site_name}&${asset_type}&${event_type}`

    return requestURL;
}

vorpal
  .command('render')
  .option('--dave')
  .action(function (args, cb) {
      const self = this;
      this.prompt(questions)
      .then (function (result) {
        //   self.log(`Okay, here are you answers ${JSON.stringify(result)}`);
          let requestURL = constructRequestURL(result);
          self.log(`Request URL: ${requestURL}`);
          cb();
      })
  })


vorpal
  .delimiter('AMP ReRenderer$')
  .show();

This commented line works

//   self.log(`Okay, here are you answers ${JSON.stringify(result)}`);

How ever when calling the constructUrl function nothing happens and the cli tool simply exists.

I guess it might be scope issue?


Solution

  • No. It's probably silently failing. Always ensure you have a catch on your promises.

    vorpal
      .command('render')
      .option('--dave')
      .action(function (args, cb) {
          const self = this;
          this.prompt(questions)
          .then (function (result) {
            //   self.log(`Okay, here are you answers ${JSON.stringify(result)}`);
              let requestURL = constructRequestURL(result);
              self.log(`Request URL: ${requestURL}`);
              cb();
          }).catch(err => {
            self.log(err);
            cb();
          });
      })