Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

commiting total refactor with updated phapper

  • Loading branch information...
commit 8cebbe2d919ed8ae2a6d88ac71fb1ec34a984261 1 parent 939aad3
Josh Mervine authored
View
8 Makefile
@@ -4,14 +4,18 @@ NODE_EXEC=$(NODE_LIBS) ./node_modules/.bin/
console:
$(NODE_LIBS) node
-tests:
+test: unit functional .PHONY
+
+unit:
$(NODE_EXEC)nodeunit ./test/*_test.js
functional:
- $(NODE_LIBS) node ./test/functional.js
+ $(NODE_EXEC)nodeunit ./test/functional.js
setup:
npm install
clean:
rm -rf ./node_modules/
+
+.PHONY:
View
78 install.sh
@@ -0,0 +1,78 @@
+#!/usr/bin/env bash
+libdir="$(echo $(cd $(dirname $0); pwd)/lib)"
+#http://yslow.org/yslow-phantomjs-3.1.5.zip
+srcpath="http://yslow.org"
+version="3.1.5"
+yspkg="yslow-phantomjs-$version.zip"
+
+decomp="unzip -q -o"
+
+function yay_finished {
+ echo " "
+ echo "You can reference it within node like so:"
+ echo " "
+ echo " var YSlow = require('yslow');"
+ echo " var yslow = new YSlow(url, args);"
+ echo " console.log(yslow.yslowjs);"
+ echo " "
+ exit 0
+}
+
+function write_yspath_js {
+ echo "module.exports.yslowjs = '$1';" > $libdir/yspath.js
+ echo "yslow.js is installed."
+ echo "=> $1"
+}
+
+# check for yslowjs && exit
+if test -f $libdir/yslow_phantom.js; then
+ write_yspath_js $libdir/yslow_phantom.js
+ yay_finished
+fi
+
+# dowload how? || fail
+if which wget > /dev/null; then
+ dldr="wget"
+elif which curl > /dev/null; then
+ dldr="curl -O"
+else
+ echo "I need wget or curl to download yslow.js"
+ exit 1
+fi
+
+# check for previous temp dir || create
+if ! test "$yslowjs_temp" && test -d $yslowjs_temp; then
+ yslowjs_temp="/tmp/yslowjs.temp"
+ mkdir -p $yslowjs_temp
+ if [ $? -ne 0 ]; then
+ echo "Couldn't make temp dir!";
+ exit 1;
+ fi
+fi
+
+cd $yslowjs_temp
+# check for previous download || download
+if test -f $yspkg; then
+ echo "Found previous package, skipping download."
+ echo "=> $yslowjs_temp/$yspkg"
+ package="$yslowjs_temp/$yspkg"
+else
+ $dldr "$srcpath/$yspkg"
+fi
+
+# decompress package
+echo "Decompressing $yspkg"
+$decomp $yspkg
+if [ $? -ne 0 ]; then
+ echo "Error occured decompressing $yspkg"
+ exit 1
+fi
+mv yslow.js $libdir/yslow_phantom.js
+if [ $? -ne 0 ]; then
+ echo "Error moving yslow.js in to place."
+ exit 1
+fi
+
+write_yspath_js "$libdir/yslow_phantom.js"
+yay_finished
+
View
95 lib/yslow.js
@@ -1,78 +1,49 @@
-var fs = require('fs');
-var execSync = require('exec-sync');
-var exec = require('child_process').exec;
-var Phapper = require('phapper');
+var path = require('path');
+var Phapper = require('phapper');
-function YSlow(opts) {
- if (!opts) {
- opts = {};
+function YSlow(url, args) {
+ // mostly for test stub
+ if (!YSlow.prototype.script) {
+ this.script = require(path.join(__dirname, 'yspath')).yslowjs;
}
- if (!opts.hasOwnProperty('url')) {
- opts.url = "http://localhost";
- }
+ args = args || [];
+ args.push('--format');
+ args.push('json');
- yslow = opts.yslow || coerce_yslow();
- delete opts.yslow;
+ this.url = url || 'http://localhost';
- this.phantom = new Phapper(yslow, optionsToArray(opts));
+ args.push(this.url);
+ this.phantom = new Phapper(this.script, args);
}
YSlow.prototype = {
run: function (callback) {
- return this.phantom.run(callback);
- },
-
- runSync: function () {
- this.results = this.phantom.runSync();
- return this.results;
- }
-};
-
-// private functions
-function optionsToArray(opts) {
- var url = opts.url;
- delete opts.url;
-
- var options = [];
- var keys = Object.keys(opts);
- for (var i = 0; i < keys.length; i++) {
- var key = keys[i];
- if (typeof opts[key] !== 'undefined') {
- options.push("--"+key);
- options.push(opts[key]);
- }
- }
-
- options.push(url);
- return options;
-}
-
-// This is about the uglyest way I think to do this, but it's
-// also the only way I can think of.
-function coerce_yslow() {
- if (process.env.NODE_PATH) {
- var node_paths = process.env.NODE_PATH.split(":");
- for (var i = 0; i < node_paths.length; i++) {
- if (node_paths[i] === ".") {
- node_paths[i] = "./";
+ this.phantom.run(function (parsed, raw) {
+ if (raw.stderr) {
+ throw new Error(raw.stderr);
}
-
- if (node_paths[i].indexOf("/", node_paths[i].length-1) === -1) {
- node_paths[i] = node_paths[i]+"/";
+ if (raw.error) {
+ throw new Error(raw.error);
}
-
- if (fs.existsSync(node_paths[i]+"yslowjs/lib/phantomjs/yslow.js")) {
- return node_paths[i]+"yslowjs/lib/phantomjs/yslow.js";
+ if (!parsed) {
+ throw new Error('Error parsing results');
}
+ callback(parsed);
+ });
+ },
- if (fs.existsSync(node_paths[i]+"phantomjs/yslow.js")) {
- return node_paths[i]+"phantomjs/yslow.js";
- }
+ runSync: function () {
+ var res = this.phantom.runSync();
+ if (res.stderr) {
+ throw new Error(res.stderr);
}
- }
- throw new Error("yslowjs couldn't find yslow.js");
-}
+ if (!res.parsed) {
+ throw new Error('Error parsing results from '+this.url);
+ }
+ this.results = res.parsed;
+ return this.results;
+ },
+};
-// export
module.exports = YSlow;
View
5 lib/yslow_phantom.js
5 additions, 0 deletions not shown
View
2  lib/yspath.js
@@ -1 +1 @@
-module.exports.yslowjs = require('path').join(__dirname, 'yslow.js');
+module.exports.yslowjs = require('path').join(__dirname, 'yslow_phantom.js');
View
4 package.json
@@ -4,7 +4,7 @@
"description": "Wrapper for phantomjs yslow.js",
"main": "./lib/yslow.js",
"scripts": {
- "test": "make tests"
+ "test": "make test"
},
"repository": {
"type": "git",
@@ -23,7 +23,7 @@
},
"dependencies": {
"exec-sync": "~0.1.6",
- "phapper": "0.0.1"
+ "phapper": "*"
},
"devDependencies": {
"nodeunit": "~0.8.1"
View
45 test/functional.js
@@ -1,25 +1,34 @@
-#!/usr/bin/env node
-var YSlow = require('lib/yslow');
-var yslow = new YSlow({
- url: "http://mervine.net/projects/npms/yslowjs",
- info: "basic"
-});
+module.exports = {
+ 'new YSlow(url, args) functional test': function (test) {
+ test.expect(7);
+ var YSlow = require('lib/yslow');
+ test.ok(YSlow, 'require YSlow');
-console.log("Configuration...");
+ var yslow = new YSlow('https://raw.github.com/jmervine/yslowjs/master/README.md');
+ test.ok(yslow, 'new YSlow');
-console.log("> yslow.phantom.command():");
-console.log("=> " + yslow.phantom.command());
+ console.log('\nRunning (Sync)....');
-console.log("\nRunning (Sync)....");
+ var results = yslow.runSync();
+ test.ok(results, 'yslow.runSync');
-var results = yslow.runSync();
+ console.log('=> overall: ' + results.o);
+ test.ok(results.o, 'runSync: results.o');
-console.log("=> overall: " + results.o);
-console.log("=> load time: " + results.lt);
+ console.log('=> load time: ' + results.lt);
+ test.ok(results.lt, 'runSync: results.lt');
-console.log("\nRunning (Async)....");
-yslow.run( function (results) {
- console.log("=> overall: " + results.o);
- console.log("=> load time: " + results.lt);
-});
+ console.log('\nRunning (Async)....');
+
+ yslow.run( function (result) {
+ console.log('=> overall: ' + result.o);
+ test.ok(result.o, 'run: result.o');
+
+ console.log('=> load time: ' + result.lt);
+ test.ok(result.lt, 'run: result.lt');
+
+ test.done();
+ });
+ }
+};
View
65 test/yslow_test.js
@@ -1,16 +1,31 @@
-var YSlow = require('lib/yslow');
+var YSlow = require('lib/yslow');
// BEGIN stubs
-var sample_results = {
- o: 75
+var sample_runSync = {
+ parsed: {
+ o: 75
+ },
+ stderr: null,
+ stdout: "{ 'o': 75 }"
+};
+
+var sample_run = {
+ parsed: {
+ o: 75
+ },
+ raw: {
+ error: null,
+ stdout: "{ 'o': 75 }",
+ stderr: null
+ }
};
var stub_runSync = function () {
- return sample_results;
+ return sample_runSync;
};
var stub_run = function (callback) {
- callback(sample_results);
+ callback(sample_run.parsed, sample_run.raw);
};
// END stubs
@@ -18,11 +33,7 @@ var yslow;
module.exports = {
setUp: function (callback) {
- yslow = new YSlow({
- url: "http://localhost/foobar",
- info: "basic",
- yslow: "./test/support/yslow.js" });
-
+ yslow = new YSlow('http://localhost/foobar');
yslow.phantom.run = stub_run;
yslow.phantom.runSync = stub_runSync;
callback();
@@ -34,16 +45,18 @@ module.exports = {
},
'new YSlow(opts)': function (test) {
- test.expect(6);
+ test.expect(7);
test.ok(yslow);
test.ok(yslow.phantom);
- test.ok(yslow.phantom.script.indexOf("yslow.js"));
- test.ok(yslow.phantom.args.indexOf('--info') !== -1);
- test.ok(yslow.phantom.args.indexOf('basic') !== -1);
+ test.ok(yslow.phantom.script.indexOf('yslow_phantom.js'));
+
+ test.ok(yslow.phantom.args.indexOf('--format') !== -1);
+ test.ok(yslow.phantom.args.indexOf('json') !== -1);
yslow = new YSlow();
- test.ok(yslow.phantom.script.indexOf("yslow.js"));
+ test.equal('http://localhost', yslow.url);
+ test.ok(yslow.phantom.script.indexOf('yslow_phantom.js'));
test.done();
},
@@ -69,14 +82,24 @@ module.exports = {
test.done();
},
- 'Phapper#command': function (test) {
+ 'Phapper#commandString': function (test) {
+ test.expect(4);
+
var yslow = new YSlow();
+ var cmdStr = yslow.phantom.commandString();
+
+ test.ok(cmdStr, 'expecting command string');
+
+ var value;
+
+ value = (cmdStr.indexOf('phantomjs ') !== -1);
+ test.ok(value, 'expecting "phantomjs" in command string');
+
+ value = (cmdStr.indexOf('yslow_phantom.js') !== -1);
+ test.ok(value, 'expecting "yslow_phantom.js" in command string');
- test.ok(yslow.phantom.command());
- test.equal(
- "phantomjs ./lib/phantomjs/yslow.js http://localhost",
- yslow.phantom.command()
- );
+ value = (cmdStr.indexOf('http://localhost') !== -1);
+ test.ok(value, 'expecting "http://localhost" in command string');
test.done();
},
Please sign in to comment.
Something went wrong with that request. Please try again.