Commit 107eedf4 107eedf48cfcb0b8bef808b9e4617f99aefc2a0f by Nicolas Perriault

cleaner bootstrap.js

1 parent 6e6cfb58
......@@ -31,10 +31,13 @@
/*global console phantom require*/
/*jshint maxstatements:30 maxcomplexity:10*/
// phantom check
if (!phantom) {
console.error('CasperJS needs to be executed in a PhantomJS environment http://phantomjs.org/');
phantom.exit(1);
}
// required version check
if (phantom.version.major === 1 && phantom.version.minor < 7) {
console.error('CasperJS needs at least PhantomJS v1.7 or later.');
phantom.exit(1);
......@@ -60,9 +63,29 @@ var CasperError = function CasperError(msg) {
};
CasperError.prototype = Object.getPrototypeOf(new Error());
// Patching fs
(function _fs(fs) {
// casperjs env initialization
(function(global, phantom){
"use strict";
// phantom args
// NOTE: we can't use require('system').args here for some very obscure reason
// do not even attempt at using it as it creates infinite recursion
var phantomArgs = phantom.args;
if (phantom.casperLoaded) {
return;
}
// Hooks in default phantomjs error handler to print a hint when a possible
// casperjs command misuse is detected.
phantom.onError = function onPhantomError(msg, trace) {
phantom.defaultErrorHandler.apply(phantom, arguments);
if (msg.indexOf("ReferenceError: Can't find variable: casper") === 0) {
console.error('Hint: you may want to use the `casperjs test` command.');
}
};
// Patching fs
var fs = (function patchFs(fs) {
if (!fs.hasOwnProperty('basename')) {
fs.basename = function basename(path) {
return path.replace(/.*\//, '');
......@@ -79,35 +102,49 @@ CasperError.prototype = Object.getPrototypeOf(new Error());
return (/^[a-z]{1,2}:/i).test(testPath) || testPath.indexOf("\\\\") === 0;
};
}
if (!fs.hasOwnProperty('pathJoin')) {
if (fs.hasOwnProperty('joinPath')) {
fs.pathJoin = fs.joinPath;
} else if (!fs.hasOwnProperty('pathJoin')) {
fs.pathJoin = function pathJoin() {
return Array.prototype.join.call(arguments, this.separator);
};
}
return fs;
})(require('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";
// CasperJS root path
if (!phantom.casperPath) {
try {
phantom.casperPath = phantom.args.map(function _map(arg) {
var match = arg.match(/^--casper-path=(.*)/);
if (match) {
return fs.absolute(match[1]);
}
}).filter(function _filter(path) {
return fs.isDirectory(path);
}).pop();
} catch (e) {
console.error("Couldn't find nor compute phantom.casperPath, exiting.");
phantom.exit(1);
}
}
// 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) {
if (require.patched) {
return require;
}
var fs = require('fs');
var patchedRequire = function _require(path) {
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 CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e);
......@@ -126,57 +163,51 @@ function patchRequire(require) {
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;
}
global.patchRequire = patchRequire;
/**
* Hooks in default phantomjs error handler to print a hint when a possible
* casperjs command misuse is detected.
*
* Initializes the CasperJS Command Line Interface.
*/
phantom.onError = function onPhantomError(msg, trace) {
phantom.defaultErrorHandler.apply(phantom, arguments);
if (msg.indexOf("ReferenceError: Can't find variable: casper") === 0) {
console.error('Hint: you may want to use the `casperjs test` command.');
function initCasperCli() {
var baseTestsPath = fs.pathJoin(phantom.casperPath, 'tests');
if (!!phantom.casperArgs.options.version) {
console.log(phantom.casperVersion.toString());
return phantom.exit();
} else if (phantom.casperArgs.get(0) === "test") {
phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js'));
phantom.casperTest = true;
phantom.casperArgs.drop("test");
} else if (phantom.casperArgs.get(0) === "selftest") {
phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js'));
phantom.casperSelfTest = phantom.casperTest = true;
phantom.casperArgs.options.includes = fs.pathJoin(baseTestsPath, 'selftest.js');
if (phantom.casperArgs.args.length <= 1) {
phantom.casperArgs.args.push(fs.pathJoin(baseTestsPath, 'suites'));
}
phantom.casperArgs.drop("selftest");
} else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) {
var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.');
var f = require("utils").format;
console.log(f('CasperJS version %s at %s, using PhantomJS version %s',
phantom.casperVersion.toString(),
phantom.casperPath, phantomVersion));
console.log(fs.read(fs.pathJoin(phantom.casperPath, 'bin', 'usage.txt')));
return phantom.exit(0);
}
};
/**
* Loads and initialize the CasperJS environment.
*/
phantom.loadCasper = function loadCasper() {
var fs = require('fs');
// casper root path
if (!phantom.casperPath) {
try {
phantom.casperPath = phantom.args.map(function _map(i) {
var match = i.match(/^--casper-path=(.*)/);
if (match) {
return fs.absolute(match[1]);
if (!phantom.casperScript) {
phantom.casperScript = phantom.casperArgs.get(0);
}
}).filter(function _filter(path) {
return fs.isDirectory(path);
}).pop();
} catch (e) {}
if (!fs.isFile(phantom.casperScript)) {
console.error('Unable to open file: ' + phantom.casperScript);
return phantom.exit(1);
}
if (!phantom.casperPath) {
console.error("Couldn't find nor compute phantom.casperPath, exiting.");
phantom.exit(1);
// filter out the called script name from casper args
phantom.casperArgs.drop(phantom.casperScript);
}
// Embedded, up-to-date, validatable & controlable CoffeeScript
......@@ -185,7 +216,6 @@ function bootstrap(global) {
// 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 CasperError('Cannot find package.json at ' + pkgFile);
......@@ -216,70 +246,19 @@ function bootstrap(global) {
})(phantom.casperPath);
// patch require (must be called in every casperjs module as of 1.1)
window.require = patchRequire(global.require);
global.require = patchRequire(global.require);
// casper cli args
phantom.casperArgs = require('cli').parse(phantomArgs);
// loaded status
phantom.casperLoaded = true;
};
/**
* Initializes the CasperJS Command Line Interface.
*/
phantom.initCasperCli = function initCasperCli() {
var fs = require("fs");
var baseTestsPath = fs.pathJoin(phantom.casperPath, 'tests');
if (!!phantom.casperArgs.options.version) {
console.log(phantom.casperVersion.toString());
return phantom.exit();
} else if (phantom.casperArgs.get(0) === "test") {
phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js'));
phantom.casperTest = true;
phantom.casperArgs.drop("test");
} else if (phantom.casperArgs.get(0) === "selftest") {
phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js'));
phantom.casperSelfTest = phantom.casperTest = true;
phantom.casperArgs.options.includes = fs.pathJoin(baseTestsPath, 'selftest.js');
if (phantom.casperArgs.args.length <= 1) {
phantom.casperArgs.args.push(fs.pathJoin(baseTestsPath, 'suites'));
}
phantom.casperArgs.drop("selftest");
} else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) {
var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.');
var f = require("utils").format;
console.log(f('CasperJS version %s at %s, using PhantomJS version %s',
phantom.casperVersion.toString(),
phantom.casperPath, phantomVersion));
console.log(fs.read(fs.pathJoin(phantom.casperPath, 'bin', 'usage.txt')));
return phantom.exit(0);
}
if (!phantom.casperScript) {
phantom.casperScript = phantom.casperArgs.get(0);
}
if (!fs.isFile(phantom.casperScript)) {
console.error('Unable to open file: ' + phantom.casperScript);
return phantom.exit(1);
if (true === phantom.casperArgs.get('cli')) {
initCasperCli();
}
// filter out the called script name from casper args
phantom.casperArgs.drop(phantom.casperScript);
phantom.casperLoaded = true;
// passed casperjs script execution
if (!phantom.injectJs(phantom.casperScript)) {
throw new CasperError('Unable to load script ' + phantom.casperScript + '; check file syntax');
}
};
if (!phantom.casperLoaded) {
phantom.loadCasper();
}
if (true === phantom.casperArgs.get('cli')) {
phantom.initCasperCli();
}
}
})(window, phantom);
......
......@@ -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');
......