0

I'm pretty new to and it seems fairly easy to use but when it comes to getting a value using the command line and returning that value to be used in another package or .js, it seems harder than I expected.

Long story short, I've used a package (akamai-ccu-purge), to enter a file to purge on the network successfully.

I want to make it more dynamic though by prompting the user to enter the file they want purged and then using that in the package.

After making a few tries using var stdin = process.openStdin(); I actually found another npm package called Prompt that seemed to be easier. Both ways seem to have the same problem though.

Node doesn't seem to want to stop for the input. It seems to want to automatically make the purge without waiting for input even though I've called that module first. It actually gets to the point where I should enter the file but it doesn't wait.

I am definitely missing something in my understanding or usage here, what am I doing wrong?

My code so far is:

var purgeUrl = require('./getUrl2');  

var PurgerFactory = require('../../node_modules/akamai-ccu-purge/index'); // this is the directory where the index.js of the node module was installed

// area where I placed the authentication tokens 
var config = {
  clientToken: //my tokens and secrets akamai requires
};

// area where urls are placed. More than one can be listed with comma separated values 
var objects = [
  purgeUrl // I am trying to pull this from the getUrl2 module
];

// Go for it! 
var Purger = PurgerFactory.create(config);

Purger.purgeObjects(objects, function(err, res) {
  console.log('------------------------');
  console.log('Purge Result:', res.body);
  console.log('------------------------');
  Purger.checkPurgeStatus(res.body.progressUri, function(err, res) {
    console.log('Purge Status', res.body);
    console.log('------------------------');
    Purger.checkQueueLength(function(err, res) {
      console.log('Queue Length', res.body);
      console.log('------------------------');
    });
  });
});

The getUrl2 module looks like this:

var prompt = require('../../node_modules/prompt'); 
  // 
  // Start the prompt 
  // 
  prompt.start();

  // 
  // Get property from the user 
  // 
  prompt.get(['newUrl'], function (err, result) {
    // 
    // Log the results. 
    // 
    console.log('Command-line input received:');
    console.log('  http://example.com/custom/' + result.newUrl);
    var purgeUrl = 'http://example.com/custom/' + result.newUrl;
    console.log(purgeUrl);
    module.exports = purgeUrl;
  });

Thanks again for the help!

Paul T. Rawkeen
  • 3,994
  • 3
  • 35
  • 51
nyhunter77
  • 614
  • 2
  • 7
  • 19

2 Answers2

2

I would probably just allow getURL2 to expose a method that will be invoked in the main module. For example:

// getURL2

var prompt = require('../../node_modules/prompt'); 

module.exports = {
  start: function(callback) {

    prompt.start();

    prompt.get(['newUrl'], function (err, result) {
      // the callback is defined in your main module
      return callback('http://example.com/custom/' + result.newUrl);
    });

  }
}

Then in your main module:

require('./getUrl2').start(function(purgeURL) {
  // do stuff with the purgeURL defined in the other module
});

The implementation may differ, but conceptually, you need to make your second module, which requires some sort of input from the user, happen as a result of that input. Callbacks are a common way to do this (as are Promises). However, as prompt is not necessarily exposing a method that would necessitate a Promise, you can do it with plain old callbacks.

Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • Thanks, this worked like a charm ... I thought it wasn't because it kept getting hung up but I guess Akamai was having an issue. - So I've read about promises and callbacks but they still confuse me a bit. - have any sources to really help me "get" what you suggested here? - Thanks again! – nyhunter77 Jan 02 '16 at 01:54
  • 1
    @nyhunter77 no prob. I would go back to the very basics of how JavaScript as a language works, and how it is different than other languages. They key is to understand the "event loop" (search on Google for the MDN article). – Josh Beam Jan 04 '16 at 19:10
2

You might also want to search around for articles on writing command line tools (sometimes referenced as CLIs) or command line apps with Node. I found the following article to be helpful when trying to figure this out myself:

http://javascriptplayground.com/blog/2015/03/node-command-line-tool/

Also, the command-line-args module worked well for me (though there's a number of other modules out there to choose from):

https://www.npmjs.com/package/command-line-args

Good luck!