Skip to content

Commit

Permalink
Module API: provide wrapped results object in callback
Browse files Browse the repository at this point in the history
  • Loading branch information
macbre committed Jan 22, 2014
1 parent 51edcb4 commit 8cbaa49
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/index.js
Expand Up @@ -13,7 +13,7 @@ function phantomas(url, options, callback) {
events = new emitter(),
path = '',
proc,
results = '';
data = '';

// options can be omitted
if (typeof options === 'function') {
Expand Down Expand Up @@ -80,27 +80,32 @@ function phantomas(url, options, callback) {

// gather data from stdout
proc.stdout.on('data', function(buf) {
results += buf;
data += buf;
});

// process results
proc.on('close', function(code) {
var debug = require('debug')('phantomas:results'),
json = false;
json = false,
results = false;

debug('Process returned code #%d', code);
debug('JSON: %s', results);
debug('JSON: %s', data);

// (try to) parse to JSON
try {
json = JSON.parse(results);
json = JSON.parse(data);
}
catch(ex) {
debug('Error when parsing JSON (%s): <%s>', ex, results);
debug('Error when parsing JSON (%s): <%s>', ex, data);
}

if (json !== false) {
events.emit('results', json);
events.emit('data', json);

// wrap JSON data into results object
results = new (require('../core/results'))(json);
events.emit('results', results);
}

if (code > 0) {
Expand All @@ -110,7 +115,7 @@ function phantomas(url, options, callback) {
}

if (typeof callback === 'function') {
callback(code === 0 ? null : code, json);
callback(code === 0 ? null : code, json, results);
}
});

Expand Down

0 comments on commit 8cbaa49

Please sign in to comment.