Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeffreytgilbert/8154269 to your computer and use it in GitHub Desktop.
Save jeffreytgilbert/8154269 to your computer and use it in GitHub Desktop.
These are the relevant bits to filter the output from a phantomjs instance when it's dumping out warnings from the old Qt library on OS X 10.9 Mavericks, since the phantom lib doesn't look to be getting updated for another 3 months or so.
var stream = require('stream'),
util = require('util');
function FilterThirdPartyWarnings(options){
// allow use without new
if (!(this instanceof FilterThirdPartyWarnings)) {
return new FilterThirdPartyWarnings(options);
}
stream.Transform.call(this, options);
};
util.inherits(FilterThirdPartyWarnings, stream.Transform);
FilterThirdPartyWarnings.prototype._transform = function(chunk, encoding, done){
var self = this;
if(self.decodeStrings === false){
var data = chunk; // this chunk is a string
} else {
var data = chunk.toString(); // this chunk is a buffer
}
if (self._lastLineData) { data = self._lastLineData + data; }
var lines = data.split('\n');
self._lastLineData = lines.splice(lines.length-1,1)[0]; // save the last line of data
lines.map(function(value){
if(value.match(/\*\*\* WARNING|CoreText/g)){ // skips the data if it's found to have a warning
return;
}
// otherwise push it back to the stack
self.push(value+'\n', encoding);
});
done();
};
// when done with the filtering, push the last line of data out too
FilterThirdPartyWarnings.prototype._flush = function (done) {
if (this._lastLineData) {
if(this._lastLineData.match(/\*\*\* WARNING|CoreText/g)){ // skips the data if it's found to have a warning
this.push('');
} else {
this.push(this._lastLineData);
}
}
this._lastLineData = null;
done();
};
function spawnPhantomJS(cmdPath, args) {
var phantomjs = spawn(cmdPath, args),
outFilter = new FilterThirdPartyWarnings( { objectMode:true } );
errFilter = new FilterThirdPartyWarnings( { objectMode:true } );
phantomjs.stdout.pipe(outFilter).pipe(process.stdout);
phantomjs.stderr.pipe(errFilter).pipe(process.stdout);
phantomjs.on('exit', function(code){
if (code === 127) { print("Perhaps phantomjs is not installed?\n"); }
process.exit(code);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment