Commit 6e6cfb58 6e6cfb58b2319b643ad2a4a589cd2b855b191361 by Nicolas Perriault

everything is broken

1 parent 5164f4a8
......@@ -38,19 +38,9 @@ if (!phantom) {
if (phantom.version.major === 1 && phantom.version.minor < 7) {
console.error('CasperJS needs at least PhantomJS v1.7 or later.');
phantom.exit(1);
} else {
try {
bootstrap(window);
} catch (e) {
console.error(e);
(e.stackArray || []).forEach(function(entry) {
console.error(' In ' + entry.sourceURL + ':' + entry.line);
});
phantom.exit(1);
}
}
// Polyfills
// Common polyfills
if (typeof Function.prototype.bind !== "function") {
Function.prototype.bind = function(scope) {
"use strict";
......@@ -61,28 +51,66 @@ if (typeof Function.prototype.bind !== "function") {
};
}
/**
* CasperJS ships with its own implementation of CommonJS' require() because
* PhantomJS' native one doesn't allow to specify supplementary, alternative
* lookup directories to fetch modules from.
*
*/
// Custom base error
var CasperError = function CasperError(msg) {
"use strict";
Error.call(this);
this.message = msg;
this.name = 'CasperError';
};
CasperError.prototype = Object.getPrototypeOf(new Error());
// Patching fs
(function _fs(fs) {
"use strict";
if (!fs.hasOwnProperty('basename')) {
fs.basename = function basename(path) {
return path.replace(/.*\//, '');
};
}
if (!fs.hasOwnProperty('dirname')) {
fs.dirname = function dirname(path) {
return path.replace(/\\/g, '/').replace(/\/[^\/]*$/, '');
};
}
if (!fs.hasOwnProperty('isWindows')) {
fs.isWindows = function isWindows() {
var testPath = arguments[0] || this.workingDirectory;
return (/^[a-z]{1,2}:/i).test(testPath) || testPath.indexOf("\\\\") === 0;
};
}
if (!fs.hasOwnProperty('pathJoin')) {
fs.pathJoin = function pathJoin() {
return Array.prototype.join.call(arguments, this.separator);
};
}
return fs;
})(require('fs'));
// Patched require to allow loading of native casperjs modules.
// Every casperjs native module have to first call this function in order to
// load a native casperjs module:
//
// var require = patchRequire(require);
// var utils = require('utils');
function patchRequire(require) {
"use strict";
require = require || window.require;
if (require.patched) {
return require;
}
var fs = require('fs');
var patchedRequire = function _require(path) {
var fs = require('fs');
console.log(path);
var moduleFilePath = fs.pathJoin(phantom.casperPath, 'modules', path + '.js');
if (!fs.exists(moduleFilePath)) {
console.log('normal');
return require(path); // native phantomjs' require() behavior
}
try {
console.log('custom');
return require(moduleFilePath);
} catch (e) {
var error = new window.CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e);
var error = new CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e);
error.file = moduleFilePath;
error.line = e.line;
error.stack = e.stack;
......@@ -94,10 +122,23 @@ function patchRequire(require) {
}
};
patchedRequire.cache = require.cache;
patchedRequire.extensions = require.extensions;
patchedRequire.stubs = require.stubs;
patchedRequire.patched = true;
return patchedRequire;
}
try {
bootstrap(window);
} catch (e) {
console.error(e);
(e.stackArray || []).forEach(function(entry) {
console.error(' In ' + entry.sourceURL + ':' + entry.line);
});
phantom.exit(1);
}
// bootstrap
function bootstrap(global) {
"use strict";
var phantomArgs = require('system').args;
......@@ -118,33 +159,7 @@ function bootstrap(global) {
* Loads and initialize the CasperJS environment.
*/
phantom.loadCasper = function loadCasper() {
// Patching fs
// TODO: watch for these methods being implemented in official fs module
var fs = (function _fs(fs) {
if (!fs.hasOwnProperty('basename')) {
fs.basename = function basename(path) {
return path.replace(/.*\//, '');
};
}
if (!fs.hasOwnProperty('dirname')) {
fs.dirname = function dirname(path) {
return path.replace(/\\/g, '/').replace(/\/[^\/]*$/, '');
};
}
if (!fs.hasOwnProperty('isWindows')) {
fs.isWindows = function isWindows() {
var testPath = arguments[0] || this.workingDirectory;
return (/^[a-z]{1,2}:/i).test(testPath) || testPath.indexOf("\\\\") === 0;
};
}
if (!fs.hasOwnProperty('pathJoin')) {
fs.pathJoin = function pathJoin() {
return Array.prototype.join.call(arguments, this.separator);
};
}
return fs;
})(require('fs'));
var fs = require('fs');
// casper root path
if (!phantom.casperPath) {
try {
......@@ -167,32 +182,22 @@ function bootstrap(global) {
// Embedded, up-to-date, validatable & controlable CoffeeScript
phantom.injectJs(fs.pathJoin(phantom.casperPath, 'modules', 'vendors', 'coffee-script.js'));
// custom global CasperError
global.CasperError = function CasperError(msg) {
Error.call(this);
this.message = msg;
this.name = 'CasperError';
};
// standard Error prototype inheritance
global.CasperError.prototype = Object.getPrototypeOf(new Error());
// CasperJS version, extracted from package.json - see http://semver.org/
phantom.casperVersion = (function getVersion(path) {
var parts, patchPart, pkg, pkgFile;
var fs = require('fs');
pkgFile = fs.absolute(fs.pathJoin(path, 'package.json'));
if (!fs.exists(pkgFile)) {
throw new global.CasperError('Cannot find package.json at ' + pkgFile);
throw new CasperError('Cannot find package.json at ' + pkgFile);
}
try {
pkg = JSON.parse(require('fs').read(pkgFile));
} catch (e) {
throw new global.CasperError('Cannot read package file contents: ' + e);
throw new CasperError('Cannot read package file contents: ' + e);
}
parts = pkg.version.trim().split(".");
if (parts.length < 3) {
throw new global.CasperError("Invalid version number");
throw new CasperError("Invalid version number");
}
patchPart = parts[2].split('-');
return {
......@@ -211,10 +216,10 @@ function bootstrap(global) {
})(phantom.casperPath);
// patch require (must be called in every casperjs module as of 1.1)
global.require = patchRequire(global.require);
window.require = patchRequire(global.require);
// casper cli args
phantom.casperArgs = global.require('cli').parse(phantom.args);
phantom.casperArgs = require('cli').parse(phantomArgs);
// loaded status
phantom.casperLoaded = true;
......@@ -265,14 +270,8 @@ function bootstrap(global) {
phantom.casperArgs.drop(phantom.casperScript);
// passed casperjs script execution
var injected = false;
try {
injected = phantom.injectJs(phantom.casperScript);
} catch (e) {
throw new global.CasperError('Error loading script ' + phantom.casperScript + ': ' + e);
}
if (!injected) {
throw new global.CasperError('Unable to load script ' + phantom.casperScript + '; check file syntax');
if (!phantom.injectJs(phantom.casperScript)) {
throw new CasperError('Unable to load script ' + phantom.casperScript + '; check file syntax');
}
};
......
......@@ -30,7 +30,7 @@
/*global CasperError console exports phantom __utils__ patchRequire*/
var require = patchRequire(require);
//var require = patchRequire(require);
var colorizer = require('colorizer');
var events = require('events');
var fs = require('fs');
......
......@@ -31,8 +31,8 @@
/*global CasperError console exports phantom patchRequire*/
var require = patchRequire(require);
var system = require('system');
var utils = require('utils');
var system = require('system');
/**
* Extracts, normalize ad organize PhantomJS CLI arguments in a dedicated
......
......@@ -2,8 +2,9 @@
* CasperJS local HTTP test server
*/
/*global phantom casper require*/
/*global phantom casper patchRequire*/
var require = patchRequire(require);
var colorizer = require('colorizer').create('Colorizer');
var fs = require('fs');
var utils = require('utils');
......