Skip to content

Commit

Permalink
Merge pull request #177 from macbre/optimist
Browse files Browse the repository at this point in the history
Use optimist module for handling phantomas options
  • Loading branch information
macbre committed Dec 18, 2013
2 parents 4427b2e + 43a5720 commit 1cbdeaa
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 83 deletions.
53 changes: 28 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function phantomas(url, options, callback) {
var args = [],
events = new emitter(),
path = '',
process,
proc,
results = '';

// options can be omitted
Expand All @@ -20,18 +20,21 @@ function phantomas(url, options, callback) {
options = {};
}

debug('nodejs %s', process.version);
debug('PhantomJS v%s installed in %s', phantomjs.version, phantomjs.path);
debug('phantomas v%s installed in %s', VERSION, __dirname);
debug('URL: <%s>', url);
debug('Options: %s', JSON.stringify(options));

// options handling
options = options || {};

options.url = url;
options.url = options.url || url || false;
options.format = options.format || 'json';

// build path to PhantomJS
path = phantomjs.path;
args.push(__dirname + '/phantomjs/phantomas.js');
args.push(__dirname + '/scripts/phantomas.js');

// build args
Object.keys(options).forEach(function(key) {
Expand All @@ -51,42 +54,41 @@ function phantomas(url, options, callback) {
debug('Running %s %s', path, args.join(' '));

// spawn the process
process = spawn(path, args);
proc = spawn(path, args);

debug('Spawned with pid #%d', process.pid);
debug('Spawned with pid #%d', proc.pid);

process.on('error', function(err) {
proc.on('error', function(err) {
debug('Error: %s', err);
});

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

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

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

if (code === 0) {
// emit RAW data (in format requested as --format)
events.emit('results', results);

// (try to) parse to JSON
if (options.format === 'json') {
try {
json = JSON.parse(results);
events.emit('data', json);
}
catch(ex) {
debug('Error when parsing JSON (%s): %s', ex, results);
};
// emit RAW data (in format requested as --format)
events.emit('results', results);

// (try to) parse to JSON
if (options.format === 'json') {
try {
json = JSON.parse(results);
events.emit('data', json);
}
catch(ex) {
debug('Error when parsing JSON (%s): %s', ex, results);
};
}
else {

if (code > 0) {
if (events.listeners('error').length > 0) {
events.emit('error', code);
}
Expand All @@ -98,13 +100,14 @@ function phantomas(url, options, callback) {
});

return {
pid: process.pid,
stdout: process.stdout,
stderr: process.stderr,
pid: proc.pid,
stdout: proc.stdout,
stderr: proc.stderr,
on: events.on.bind(events)
};
}

phantomas.path = __dirname;
phantomas.version = VERSION;

module.exports = phantomas;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"css-parse": "1.5",
"debug": "0.7.x",
"fast-stats": ">=0.0.2",
"optimist": "0.6.x",
"phantomjs": ">=1.9",
"slick": "1.0",
"tap-eater": ">=0.0.1",
Expand All @@ -44,7 +45,7 @@
"preferGlobal": true,
"scripts": {
"test": "vows --spec",
"lint": "jshint --verbose core/ modules/ phantomjs/ test/ lib/*.js phantomas.js analyze-css.js run-multiple.js"
"lint": "jshint --verbose core/ modules/ scripts/ test/ lib/*.js phantomas.js analyze-css.js run-multiple.js"
},
"jshintConfig": {
"node": true,
Expand Down
95 changes: 88 additions & 7 deletions phantomas.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,94 @@
#!/usr/bin/env node
var path = require('path'),
spawn = require('child_process').spawn,
phantomjs = require('phantomjs');
/**
* PhantomJS-based web performance metrics collector
*
* Run "node phantomas.js" to get help
*
* @see https://github.com/macbre/phantomas
*/
var phantomas = require('./index'),
program = require('optimist'),
child,
options = {},
program,
url = '';

var phantomas = spawn(phantomjs.path, [path.resolve(__dirname, 'phantomjs/phantomas.js')].concat(process.argv.slice(2)));
// parse options
program
.usage('phantomas --url <url> [options]')

phantomas.stdout.pipe(process.stdout);
phantomas.stderr.pipe(process.stderr);
// mandatory
.describe('url', 'Set URL to work with').string('url')

phantomas.on('close', function (code) {
// version / help
.describe('version', 'Show version number and quit').boolean('version').alias('version', 'V')
.describe('help', 'This help text').boolean('help').alias('help', 'h')

// optional params
.describe('allow-domain', 'allow requests to given domain(s) - aka whitelist [domain],[domain],...')
.describe('block-domain', 'disallow requests to given domain(s) - aka blacklist [domain],[domain],...')
.describe('config', 'uses JSON-formatted config file to set parameters')
.describe('cookie', 'document.cookie formatted string for setting a single cookie (e.g. "bar=foo;domain=url")')
.describe('cookie-jar', 'persistent cookie JAR across requests')
.describe('disable-js', 'disable JavaScript on the page that will be loaded').boolean('disable-js')
.describe('format', 'output format').default('format', 'plain')
.describe('log', 'log to a given file')
.describe('modules', 'run selected modules only [moduleOne],[moduleTwo],...')
.describe('no-externals', 'block requests to 3rd party domains').boolean('no-externals')
.describe('screenshot', 'render fully loaded page to a given file')
.describe('silent', 'don\'t write anything to the console').boolean('silent')
.describe('skip-modules', 'skip selected modules [moduleOne],[moduleTwo],...')
.describe('timeout', 'timeout for phantomas run').default('timeout', 15)
.describe('user-agent', 'provide a custom user agent')
.describe('verbose', 'writes debug messages to the console').boolean('verbose').alias('verbose', 'v')
.describe('viewport', 'phantomJS viewport dimensions [width]x[height]').default('viewport', '1280x1024')

// experimental features
.describe('analyze-css', 'emit in-depth CSS metrics - EXPERIMENTAL').boolean('analyze-css')
.describe('film-strip', 'register film strip when page is loading (PNG files will be saved in ./filmstrip directory) - EXPERIMENTAL').boolean('film-strip');

// parse it
options = program.parse(process.argv);

// show version number
if (options.version === true) {
console.log('phantomas v%s', phantomas.version);
process.exit(0);
}

// show help
if (options.help === true) {
program.showHelp();
process.exit(0);
}

// --url is mandatory -> show help
if (typeof options.url !== 'string' && typeof options.config === 'undefined') {
program.showHelp();
process.exit(255);
}

url = options.url;
delete options.url;
delete options._;
delete options.$0;

// handle --no-foo options
options['no-externals'] = options.externals === false;
delete options.externals;

// spawn phantomas process
child = phantomas(url, options);

// emit --verbose messages
child.stderr.pipe(process.stderr);

// pass raw results
child.on('results', function (res) {
process.stdout.write(res);
});

// pass exit code
child.on('error', function (code) {
process.exit(code);
});
50 changes: 0 additions & 50 deletions phantomjs/phantomas.js

This file was deleted.

26 changes: 26 additions & 0 deletions scripts/phantomas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* phantomas script executed by PhantomJS
*
* Don't run it directly. Use phantomas.js nodejs script instead!
*/
var args = require('system').args,
// parse script arguments
params = require('../lib/args').parse(args),
phantomas = require('../core/phantomas'),
instance;

// compatibility layer for NodeJS modules
process = {argv: []};

// run phantomas
instance = new phantomas(params);

try {
instance.run();
}
catch(ex) {
console.log('phantomas v' + phantomas.version + ' failed with an error:');
console.log(ex);

phantom.exit(255);
}

0 comments on commit 1cbdeaa

Please sign in to comment.