Commit 34362eaa 34362eaac5389a0720b7fbd2bcec397e51e8d5fb by Nicolas Perriault

added a proper cli module

1 parent 5ec02b49
......@@ -47,7 +47,8 @@ phantom.casperVersion = {
}
};
// patching fs
// Patching fs
// TODO: watch for these methods being implemented in official fs module
var fs = (function(fs) {
if (!fs.hasOwnProperty('basename')) {
fs.basename = function(path) {
......@@ -70,64 +71,9 @@ var fs = (function(fs) {
// casper root path
phantom.casperPath = fs.absolute(phantom.libraryScript);
// casper cli args
phantom.casperArgs = (function(cliArgs) {
var extract = {
args: [],
options: {},
get: function(what) {
if (typeof what === "number") {
return this.args[what];
} else if (typeof what === "string") {
return this.options[what];
} else {
throw new Error("Unsupported cli arg getter " + typeof what);
}
}
};
cliArgs.forEach(function(arg) {
if (arg.indexOf('--') === 0) {
// named option
var optionMatch = arg.match(/^--(.*)=(.*)/i);
if (optionMatch) {
extract.options[optionMatch[1]] = optionMatch[2];
} else {
// flag
var flagMatch = arg.match(/^--(.*)/);
if (flagMatch) {
extract.options[flagMatch[1]] = true;
}
}
} else {
// positional arg
extract.args.push(arg);
}
});
return extract;
})(phantom.args);
var sourceIds = {};
// Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/
// TODO: remoive when phantomjs has js engine upgrade
if (!new Error().hasOwnProperty('stack')) {
Object.defineProperty(Error.prototype, 'stack', {
set: function(string) {
this._stack = string;
},
get: function() {
if (this._stack) {
return this._stack;
} else if (this.fileName || this.sourceId) {
return this.toString() + '\nat ' + getErrorMessage(this);
}
return this.toString() + '\nat unknown';
},
configurable: true,
enumerable: true
});
}
// Patching require()
// Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/
// TODO: remove when PhantomJS has full module support
require = (function(require, requireDir) {
......@@ -175,7 +121,7 @@ require = (function(require, requireDir) {
}
}
if (!file) {
throw new Error("Can't find module " + path);
throw new Error("CasperJS couldn't find module " + path);
}
if (file in requireCache) {
return requireCache[file].exports;
......@@ -215,6 +161,31 @@ require = (function(require, requireDir) {
};
})(require, phantom.casperPath);
// Adding stack traces to Error
// Inspired by phantomjs-nodify: https://github.com/jgonera/phantomjs-nodify/
// TODO: remove when phantomjs has js engine upgrade
if (!new Error().hasOwnProperty('stack')) {
Object.defineProperty(Error.prototype, 'stack', {
set: function(string) {
this._stack = string;
},
get: function() {
if (this._stack) {
return this._stack;
} else if (this.fileName || this.sourceId) {
return this.toString() + '\nat ' + getErrorMessage(this);
}
return this.toString() + '\nat unknown';
},
configurable: true,
enumerable: true
});
}
// casper cli args
phantom.casperArgs = require('cli').parse(phantom.args);
// loaded status
phantom.casperLoaded = true;
if (!!phantom.casperArgs.options.version) {
......@@ -227,7 +198,7 @@ if (!!phantom.casperArgs.options.version) {
phantom.exit(0);
}
phantom.casperScript = phantom.casperArgs.args[0];
phantom.casperScript = phantom.casperArgs.get(0);
if (!fs.isFile(phantom.casperScript)) {
console.log('Unable to open file: ' + phantom.casperScript);
......@@ -238,4 +209,7 @@ if (!fs.isFile(phantom.casperScript)) {
phantom.casperArgs.args = phantom.casperArgs.args.filter(function(arg) {
return arg !== phantom.casperScript;
});
// passed script execution
// TODO: proper syntax validation?
phantom.injectJs(phantom.casperScript);
......
/*!
* Casper is a navigation utility for PhantomJS.
*
* Documentation: http://n1k0.github.com/casperjs/
* Repository: http://github.com/n1k0/casperjs
*
* Copyright (c) 2011 Nicolas Perriault
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
var utils = require('utils');
/**
* Extracts, normalize ad organize PhantomJS CLI arguments in a dedicated
* Object.
*
* @param array phantomArgs phantom.args value
* @return Object
*/
exports.parse = function(phantomArgs) {
if (!utils.isType(phantomArgs, "runtimearray")) {
throw new Error('parse() can only process a phantomjs arguments array');
}
var extract = {
args: [],
options: {},
get: function(what) {
if (typeof what === "number") {
return this.args[what];
} else if (typeof what === "string") {
return this.options[what];
} else {
throw new Error("Unsupported cli arg getter " + typeof what);
}
}
};
phantomArgs.forEach(function(arg) {
if (arg.indexOf('--') === 0) {
// named option
var optionMatch = arg.match(/^--(.*)=(.*)/i);
if (optionMatch) {
extract.options[optionMatch[1]] = optionMatch[2];
} else {
// flag
var flagMatch = arg.match(/^--(.*)/);
if (flagMatch) {
extract.options[flagMatch[1]] = true;
}
}
} else {
// positional arg
extract.args.push(arg);
}
});
return extract;
};
......@@ -22,16 +22,14 @@ var casper = require('casper').create({
verbose: true
});
(function(casper) {
var tests = [];
if (casper.cli.args.length) {
var tests = [];
if (casper.cli.args.length) {
tests = casper.cli.args.filter(function(path) {
return fs.isFile(path) || fs.isDirectory(path);
});
}
if (!tests.length) {
}
if (!tests.length) {
// default test suite is casperjs' one
tests = [fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'suites'))];
}
casper.test.runSuites.apply(casper.test, tests);
})(casper);
}
casper.test.runSuites.apply(casper.test, tests);
......
# A small subset of the run.js written in coffeescript
"A small subset of the run.js written in coffeescript"
do (casper) ->
steps = 0
......