Commit 6e6cfb58 6e6cfb58b2319b643ad2a4a589cd2b855b191361 by Nicolas Perriault

everything is broken

1 parent 5164f4a8
...@@ -38,19 +38,9 @@ if (!phantom) { ...@@ -38,19 +38,9 @@ if (!phantom) {
38 if (phantom.version.major === 1 && phantom.version.minor < 7) { 38 if (phantom.version.major === 1 && phantom.version.minor < 7) {
39 console.error('CasperJS needs at least PhantomJS v1.7 or later.'); 39 console.error('CasperJS needs at least PhantomJS v1.7 or later.');
40 phantom.exit(1); 40 phantom.exit(1);
41 } else {
42 try {
43 bootstrap(window);
44 } catch (e) {
45 console.error(e);
46 (e.stackArray || []).forEach(function(entry) {
47 console.error(' In ' + entry.sourceURL + ':' + entry.line);
48 });
49 phantom.exit(1);
50 }
51 } 41 }
52 42
53 // Polyfills 43 // Common polyfills
54 if (typeof Function.prototype.bind !== "function") { 44 if (typeof Function.prototype.bind !== "function") {
55 Function.prototype.bind = function(scope) { 45 Function.prototype.bind = function(scope) {
56 "use strict"; 46 "use strict";
...@@ -61,28 +51,66 @@ if (typeof Function.prototype.bind !== "function") { ...@@ -61,28 +51,66 @@ if (typeof Function.prototype.bind !== "function") {
61 }; 51 };
62 } 52 }
63 53
64 /** 54 // Custom base error
65 * CasperJS ships with its own implementation of CommonJS' require() because 55 var CasperError = function CasperError(msg) {
66 * PhantomJS' native one doesn't allow to specify supplementary, alternative 56 "use strict";
67 * lookup directories to fetch modules from. 57 Error.call(this);
68 * 58 this.message = msg;
69 */ 59 this.name = 'CasperError';
60 };
61 CasperError.prototype = Object.getPrototypeOf(new Error());
62
63 // Patching fs
64 (function _fs(fs) {
65 "use strict";
66 if (!fs.hasOwnProperty('basename')) {
67 fs.basename = function basename(path) {
68 return path.replace(/.*\//, '');
69 };
70 }
71 if (!fs.hasOwnProperty('dirname')) {
72 fs.dirname = function dirname(path) {
73 return path.replace(/\\/g, '/').replace(/\/[^\/]*$/, '');
74 };
75 }
76 if (!fs.hasOwnProperty('isWindows')) {
77 fs.isWindows = function isWindows() {
78 var testPath = arguments[0] || this.workingDirectory;
79 return (/^[a-z]{1,2}:/i).test(testPath) || testPath.indexOf("\\\\") === 0;
80 };
81 }
82 if (!fs.hasOwnProperty('pathJoin')) {
83 fs.pathJoin = function pathJoin() {
84 return Array.prototype.join.call(arguments, this.separator);
85 };
86 }
87 return fs;
88 })(require('fs'));
89
90 // Patched require to allow loading of native casperjs modules.
91 // Every casperjs native module have to first call this function in order to
92 // load a native casperjs module:
93 //
94 // var require = patchRequire(require);
95 // var utils = require('utils');
70 function patchRequire(require) { 96 function patchRequire(require) {
71 "use strict"; 97 "use strict";
72 require = require || window.require;
73 if (require.patched) { 98 if (require.patched) {
74 return require; 99 return require;
75 } 100 }
101 var fs = require('fs');
76 var patchedRequire = function _require(path) { 102 var patchedRequire = function _require(path) {
77 var fs = require('fs'); 103 console.log(path);
78 var moduleFilePath = fs.pathJoin(phantom.casperPath, 'modules', path + '.js'); 104 var moduleFilePath = fs.pathJoin(phantom.casperPath, 'modules', path + '.js');
79 if (!fs.exists(moduleFilePath)) { 105 if (!fs.exists(moduleFilePath)) {
106 console.log('normal');
80 return require(path); // native phantomjs' require() behavior 107 return require(path); // native phantomjs' require() behavior
81 } 108 }
82 try { 109 try {
110 console.log('custom');
83 return require(moduleFilePath); 111 return require(moduleFilePath);
84 } catch (e) { 112 } catch (e) {
85 var error = new window.CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e); 113 var error = new CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e);
86 error.file = moduleFilePath; 114 error.file = moduleFilePath;
87 error.line = e.line; 115 error.line = e.line;
88 error.stack = e.stack; 116 error.stack = e.stack;
...@@ -94,10 +122,23 @@ function patchRequire(require) { ...@@ -94,10 +122,23 @@ function patchRequire(require) {
94 } 122 }
95 }; 123 };
96 patchedRequire.cache = require.cache; 124 patchedRequire.cache = require.cache;
125 patchedRequire.extensions = require.extensions;
126 patchedRequire.stubs = require.stubs;
97 patchedRequire.patched = true; 127 patchedRequire.patched = true;
98 return patchedRequire; 128 return patchedRequire;
99 } 129 }
100 130
131 try {
132 bootstrap(window);
133 } catch (e) {
134 console.error(e);
135 (e.stackArray || []).forEach(function(entry) {
136 console.error(' In ' + entry.sourceURL + ':' + entry.line);
137 });
138 phantom.exit(1);
139 }
140
141 // bootstrap
101 function bootstrap(global) { 142 function bootstrap(global) {
102 "use strict"; 143 "use strict";
103 var phantomArgs = require('system').args; 144 var phantomArgs = require('system').args;
...@@ -118,33 +159,7 @@ function bootstrap(global) { ...@@ -118,33 +159,7 @@ function bootstrap(global) {
118 * Loads and initialize the CasperJS environment. 159 * Loads and initialize the CasperJS environment.
119 */ 160 */
120 phantom.loadCasper = function loadCasper() { 161 phantom.loadCasper = function loadCasper() {
121 // Patching fs 162 var fs = require('fs');
122 // TODO: watch for these methods being implemented in official fs module
123 var fs = (function _fs(fs) {
124 if (!fs.hasOwnProperty('basename')) {
125 fs.basename = function basename(path) {
126 return path.replace(/.*\//, '');
127 };
128 }
129 if (!fs.hasOwnProperty('dirname')) {
130 fs.dirname = function dirname(path) {
131 return path.replace(/\\/g, '/').replace(/\/[^\/]*$/, '');
132 };
133 }
134 if (!fs.hasOwnProperty('isWindows')) {
135 fs.isWindows = function isWindows() {
136 var testPath = arguments[0] || this.workingDirectory;
137 return (/^[a-z]{1,2}:/i).test(testPath) || testPath.indexOf("\\\\") === 0;
138 };
139 }
140 if (!fs.hasOwnProperty('pathJoin')) {
141 fs.pathJoin = function pathJoin() {
142 return Array.prototype.join.call(arguments, this.separator);
143 };
144 }
145 return fs;
146 })(require('fs'));
147
148 // casper root path 163 // casper root path
149 if (!phantom.casperPath) { 164 if (!phantom.casperPath) {
150 try { 165 try {
...@@ -167,32 +182,22 @@ function bootstrap(global) { ...@@ -167,32 +182,22 @@ function bootstrap(global) {
167 // Embedded, up-to-date, validatable & controlable CoffeeScript 182 // Embedded, up-to-date, validatable & controlable CoffeeScript
168 phantom.injectJs(fs.pathJoin(phantom.casperPath, 'modules', 'vendors', 'coffee-script.js')); 183 phantom.injectJs(fs.pathJoin(phantom.casperPath, 'modules', 'vendors', 'coffee-script.js'));
169 184
170 // custom global CasperError
171 global.CasperError = function CasperError(msg) {
172 Error.call(this);
173 this.message = msg;
174 this.name = 'CasperError';
175 };
176
177 // standard Error prototype inheritance
178 global.CasperError.prototype = Object.getPrototypeOf(new Error());
179
180 // CasperJS version, extracted from package.json - see http://semver.org/ 185 // CasperJS version, extracted from package.json - see http://semver.org/
181 phantom.casperVersion = (function getVersion(path) { 186 phantom.casperVersion = (function getVersion(path) {
182 var parts, patchPart, pkg, pkgFile; 187 var parts, patchPart, pkg, pkgFile;
183 var fs = require('fs'); 188 var fs = require('fs');
184 pkgFile = fs.absolute(fs.pathJoin(path, 'package.json')); 189 pkgFile = fs.absolute(fs.pathJoin(path, 'package.json'));
185 if (!fs.exists(pkgFile)) { 190 if (!fs.exists(pkgFile)) {
186 throw new global.CasperError('Cannot find package.json at ' + pkgFile); 191 throw new CasperError('Cannot find package.json at ' + pkgFile);
187 } 192 }
188 try { 193 try {
189 pkg = JSON.parse(require('fs').read(pkgFile)); 194 pkg = JSON.parse(require('fs').read(pkgFile));
190 } catch (e) { 195 } catch (e) {
191 throw new global.CasperError('Cannot read package file contents: ' + e); 196 throw new CasperError('Cannot read package file contents: ' + e);
192 } 197 }
193 parts = pkg.version.trim().split("."); 198 parts = pkg.version.trim().split(".");
194 if (parts.length < 3) { 199 if (parts.length < 3) {
195 throw new global.CasperError("Invalid version number"); 200 throw new CasperError("Invalid version number");
196 } 201 }
197 patchPart = parts[2].split('-'); 202 patchPart = parts[2].split('-');
198 return { 203 return {
...@@ -211,10 +216,10 @@ function bootstrap(global) { ...@@ -211,10 +216,10 @@ function bootstrap(global) {
211 })(phantom.casperPath); 216 })(phantom.casperPath);
212 217
213 // patch require (must be called in every casperjs module as of 1.1) 218 // patch require (must be called in every casperjs module as of 1.1)
214 global.require = patchRequire(global.require); 219 window.require = patchRequire(global.require);
215 220
216 // casper cli args 221 // casper cli args
217 phantom.casperArgs = global.require('cli').parse(phantom.args); 222 phantom.casperArgs = require('cli').parse(phantomArgs);
218 223
219 // loaded status 224 // loaded status
220 phantom.casperLoaded = true; 225 phantom.casperLoaded = true;
...@@ -265,14 +270,8 @@ function bootstrap(global) { ...@@ -265,14 +270,8 @@ function bootstrap(global) {
265 phantom.casperArgs.drop(phantom.casperScript); 270 phantom.casperArgs.drop(phantom.casperScript);
266 271
267 // passed casperjs script execution 272 // passed casperjs script execution
268 var injected = false; 273 if (!phantom.injectJs(phantom.casperScript)) {
269 try { 274 throw new CasperError('Unable to load script ' + phantom.casperScript + '; check file syntax');
270 injected = phantom.injectJs(phantom.casperScript);
271 } catch (e) {
272 throw new global.CasperError('Error loading script ' + phantom.casperScript + ': ' + e);
273 }
274 if (!injected) {
275 throw new global.CasperError('Unable to load script ' + phantom.casperScript + '; check file syntax');
276 } 275 }
277 }; 276 };
278 277
......
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
30 30
31 /*global CasperError console exports phantom __utils__ patchRequire*/ 31 /*global CasperError console exports phantom __utils__ patchRequire*/
32 32
33 var require = patchRequire(require); 33 //var require = patchRequire(require);
34 var colorizer = require('colorizer'); 34 var colorizer = require('colorizer');
35 var events = require('events'); 35 var events = require('events');
36 var fs = require('fs'); 36 var fs = require('fs');
......
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
31 /*global CasperError console exports phantom patchRequire*/ 31 /*global CasperError console exports phantom patchRequire*/
32 32
33 var require = patchRequire(require); 33 var require = patchRequire(require);
34 var system = require('system');
35 var utils = require('utils'); 34 var utils = require('utils');
35 var system = require('system');
36 36
37 /** 37 /**
38 * Extracts, normalize ad organize PhantomJS CLI arguments in a dedicated 38 * Extracts, normalize ad organize PhantomJS CLI arguments in a dedicated
......
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
2 * CasperJS local HTTP test server 2 * CasperJS local HTTP test server
3 */ 3 */
4 4
5 /*global phantom casper require*/ 5 /*global phantom casper patchRequire*/
6 6
7 var require = patchRequire(require);
7 var colorizer = require('colorizer').create('Colorizer'); 8 var colorizer = require('colorizer').create('Colorizer');
8 var fs = require('fs'); 9 var fs = require('fs');
9 var utils = require('utils'); 10 var utils = require('utils');
......