cleaner bootstrap.js
Showing
2 changed files
with
96 additions
and
117 deletions
... | @@ -31,10 +31,13 @@ | ... | @@ -31,10 +31,13 @@ |
31 | /*global console phantom require*/ | 31 | /*global console phantom require*/ |
32 | /*jshint maxstatements:30 maxcomplexity:10*/ | 32 | /*jshint maxstatements:30 maxcomplexity:10*/ |
33 | 33 | ||
34 | // phantom check | ||
34 | if (!phantom) { | 35 | if (!phantom) { |
35 | console.error('CasperJS needs to be executed in a PhantomJS environment http://phantomjs.org/'); | 36 | console.error('CasperJS needs to be executed in a PhantomJS environment http://phantomjs.org/'); |
37 | phantom.exit(1); | ||
36 | } | 38 | } |
37 | 39 | ||
40 | // required version check | ||
38 | if (phantom.version.major === 1 && phantom.version.minor < 7) { | 41 | if (phantom.version.major === 1 && phantom.version.minor < 7) { |
39 | console.error('CasperJS needs at least PhantomJS v1.7 or later.'); | 42 | console.error('CasperJS needs at least PhantomJS v1.7 or later.'); |
40 | phantom.exit(1); | 43 | phantom.exit(1); |
... | @@ -60,9 +63,29 @@ var CasperError = function CasperError(msg) { | ... | @@ -60,9 +63,29 @@ var CasperError = function CasperError(msg) { |
60 | }; | 63 | }; |
61 | CasperError.prototype = Object.getPrototypeOf(new Error()); | 64 | CasperError.prototype = Object.getPrototypeOf(new Error()); |
62 | 65 | ||
63 | // Patching fs | 66 | // casperjs env initialization |
64 | (function _fs(fs) { | 67 | (function(global, phantom){ |
65 | "use strict"; | 68 | "use strict"; |
69 | // phantom args | ||
70 | // NOTE: we can't use require('system').args here for some very obscure reason | ||
71 | // do not even attempt at using it as it creates infinite recursion | ||
72 | var phantomArgs = phantom.args; | ||
73 | |||
74 | if (phantom.casperLoaded) { | ||
75 | return; | ||
76 | } | ||
77 | |||
78 | // Hooks in default phantomjs error handler to print a hint when a possible | ||
79 | // casperjs command misuse is detected. | ||
80 | phantom.onError = function onPhantomError(msg, trace) { | ||
81 | phantom.defaultErrorHandler.apply(phantom, arguments); | ||
82 | if (msg.indexOf("ReferenceError: Can't find variable: casper") === 0) { | ||
83 | console.error('Hint: you may want to use the `casperjs test` command.'); | ||
84 | } | ||
85 | }; | ||
86 | |||
87 | // Patching fs | ||
88 | var fs = (function patchFs(fs) { | ||
66 | if (!fs.hasOwnProperty('basename')) { | 89 | if (!fs.hasOwnProperty('basename')) { |
67 | fs.basename = function basename(path) { | 90 | fs.basename = function basename(path) { |
68 | return path.replace(/.*\//, ''); | 91 | return path.replace(/.*\//, ''); |
... | @@ -79,35 +102,49 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); | ... | @@ -79,35 +102,49 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); |
79 | return (/^[a-z]{1,2}:/i).test(testPath) || testPath.indexOf("\\\\") === 0; | 102 | return (/^[a-z]{1,2}:/i).test(testPath) || testPath.indexOf("\\\\") === 0; |
80 | }; | 103 | }; |
81 | } | 104 | } |
82 | if (!fs.hasOwnProperty('pathJoin')) { | 105 | if (fs.hasOwnProperty('joinPath')) { |
106 | fs.pathJoin = fs.joinPath; | ||
107 | } else if (!fs.hasOwnProperty('pathJoin')) { | ||
83 | fs.pathJoin = function pathJoin() { | 108 | fs.pathJoin = function pathJoin() { |
84 | return Array.prototype.join.call(arguments, this.separator); | 109 | return Array.prototype.join.call(arguments, this.separator); |
85 | }; | 110 | }; |
86 | } | 111 | } |
87 | return fs; | 112 | return fs; |
88 | })(require('fs')); | 113 | })(require('fs')); |
89 | 114 | ||
90 | // Patched require to allow loading of native casperjs modules. | 115 | // CasperJS root path |
91 | // Every casperjs native module have to first call this function in order to | 116 | if (!phantom.casperPath) { |
92 | // load a native casperjs module: | 117 | try { |
93 | // | 118 | phantom.casperPath = phantom.args.map(function _map(arg) { |
94 | // var require = patchRequire(require); | 119 | var match = arg.match(/^--casper-path=(.*)/); |
95 | // var utils = require('utils'); | 120 | if (match) { |
96 | function patchRequire(require) { | 121 | return fs.absolute(match[1]); |
97 | "use strict"; | 122 | } |
123 | }).filter(function _filter(path) { | ||
124 | return fs.isDirectory(path); | ||
125 | }).pop(); | ||
126 | } catch (e) { | ||
127 | console.error("Couldn't find nor compute phantom.casperPath, exiting."); | ||
128 | phantom.exit(1); | ||
129 | } | ||
130 | } | ||
131 | |||
132 | // Patched require to allow loading of native casperjs modules. | ||
133 | // Every casperjs native module have to first call this function in order to | ||
134 | // load a native casperjs module: | ||
135 | // | ||
136 | // var require = patchRequire(require); | ||
137 | // var utils = require('utils'); | ||
138 | function patchRequire(require) { | ||
98 | if (require.patched) { | 139 | if (require.patched) { |
99 | return require; | 140 | return require; |
100 | } | 141 | } |
101 | var fs = require('fs'); | ||
102 | var patchedRequire = function _require(path) { | 142 | var patchedRequire = function _require(path) { |
103 | console.log(path); | ||
104 | var moduleFilePath = fs.pathJoin(phantom.casperPath, 'modules', path + '.js'); | 143 | var moduleFilePath = fs.pathJoin(phantom.casperPath, 'modules', path + '.js'); |
105 | if (!fs.exists(moduleFilePath)) { | 144 | if (!fs.exists(moduleFilePath)) { |
106 | console.log('normal'); | ||
107 | return require(path); // native phantomjs' require() behavior | 145 | return require(path); // native phantomjs' require() behavior |
108 | } | 146 | } |
109 | try { | 147 | try { |
110 | console.log('custom'); | ||
111 | return require(moduleFilePath); | 148 | return require(moduleFilePath); |
112 | } catch (e) { | 149 | } catch (e) { |
113 | var error = new CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e); | 150 | var error = new CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e); |
... | @@ -126,57 +163,51 @@ function patchRequire(require) { | ... | @@ -126,57 +163,51 @@ function patchRequire(require) { |
126 | patchedRequire.stubs = require.stubs; | 163 | patchedRequire.stubs = require.stubs; |
127 | patchedRequire.patched = true; | 164 | patchedRequire.patched = true; |
128 | return patchedRequire; | 165 | return patchedRequire; |
129 | } | 166 | } |
130 | 167 | global.patchRequire = patchRequire; | |
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 | ||
142 | function bootstrap(global) { | ||
143 | "use strict"; | ||
144 | var phantomArgs = require('system').args; | ||
145 | 168 | ||
146 | /** | 169 | /** |
147 | * Hooks in default phantomjs error handler to print a hint when a possible | 170 | * Initializes the CasperJS Command Line Interface. |
148 | * casperjs command misuse is detected. | ||
149 | * | ||
150 | */ | 171 | */ |
151 | phantom.onError = function onPhantomError(msg, trace) { | 172 | function initCasperCli() { |
152 | phantom.defaultErrorHandler.apply(phantom, arguments); | 173 | var baseTestsPath = fs.pathJoin(phantom.casperPath, 'tests'); |
153 | if (msg.indexOf("ReferenceError: Can't find variable: casper") === 0) { | 174 | |
154 | console.error('Hint: you may want to use the `casperjs test` command.'); | 175 | if (!!phantom.casperArgs.options.version) { |
176 | console.log(phantom.casperVersion.toString()); | ||
177 | return phantom.exit(); | ||
178 | } else if (phantom.casperArgs.get(0) === "test") { | ||
179 | phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js')); | ||
180 | phantom.casperTest = true; | ||
181 | phantom.casperArgs.drop("test"); | ||
182 | } else if (phantom.casperArgs.get(0) === "selftest") { | ||
183 | phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js')); | ||
184 | phantom.casperSelfTest = phantom.casperTest = true; | ||
185 | phantom.casperArgs.options.includes = fs.pathJoin(baseTestsPath, 'selftest.js'); | ||
186 | if (phantom.casperArgs.args.length <= 1) { | ||
187 | phantom.casperArgs.args.push(fs.pathJoin(baseTestsPath, 'suites')); | ||
188 | } | ||
189 | phantom.casperArgs.drop("selftest"); | ||
190 | } else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) { | ||
191 | var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.'); | ||
192 | var f = require("utils").format; | ||
193 | console.log(f('CasperJS version %s at %s, using PhantomJS version %s', | ||
194 | phantom.casperVersion.toString(), | ||
195 | phantom.casperPath, phantomVersion)); | ||
196 | console.log(fs.read(fs.pathJoin(phantom.casperPath, 'bin', 'usage.txt'))); | ||
197 | return phantom.exit(0); | ||
155 | } | 198 | } |
156 | }; | ||
157 | 199 | ||
158 | /** | 200 | if (!phantom.casperScript) { |
159 | * Loads and initialize the CasperJS environment. | 201 | phantom.casperScript = phantom.casperArgs.get(0); |
160 | */ | ||
161 | phantom.loadCasper = function loadCasper() { | ||
162 | var fs = require('fs'); | ||
163 | // casper root path | ||
164 | if (!phantom.casperPath) { | ||
165 | try { | ||
166 | phantom.casperPath = phantom.args.map(function _map(i) { | ||
167 | var match = i.match(/^--casper-path=(.*)/); | ||
168 | if (match) { | ||
169 | return fs.absolute(match[1]); | ||
170 | } | 202 | } |
171 | }).filter(function _filter(path) { | 203 | |
172 | return fs.isDirectory(path); | 204 | if (!fs.isFile(phantom.casperScript)) { |
173 | }).pop(); | 205 | console.error('Unable to open file: ' + phantom.casperScript); |
174 | } catch (e) {} | 206 | return phantom.exit(1); |
175 | } | 207 | } |
176 | 208 | ||
177 | if (!phantom.casperPath) { | 209 | // filter out the called script name from casper args |
178 | console.error("Couldn't find nor compute phantom.casperPath, exiting."); | 210 | phantom.casperArgs.drop(phantom.casperScript); |
179 | phantom.exit(1); | ||
180 | } | 211 | } |
181 | 212 | ||
182 | // Embedded, up-to-date, validatable & controlable CoffeeScript | 213 | // Embedded, up-to-date, validatable & controlable CoffeeScript |
... | @@ -185,7 +216,6 @@ function bootstrap(global) { | ... | @@ -185,7 +216,6 @@ function bootstrap(global) { |
185 | // CasperJS version, extracted from package.json - see http://semver.org/ | 216 | // CasperJS version, extracted from package.json - see http://semver.org/ |
186 | phantom.casperVersion = (function getVersion(path) { | 217 | phantom.casperVersion = (function getVersion(path) { |
187 | var parts, patchPart, pkg, pkgFile; | 218 | var parts, patchPart, pkg, pkgFile; |
188 | var fs = require('fs'); | ||
189 | pkgFile = fs.absolute(fs.pathJoin(path, 'package.json')); | 219 | pkgFile = fs.absolute(fs.pathJoin(path, 'package.json')); |
190 | if (!fs.exists(pkgFile)) { | 220 | if (!fs.exists(pkgFile)) { |
191 | throw new CasperError('Cannot find package.json at ' + pkgFile); | 221 | throw new CasperError('Cannot find package.json at ' + pkgFile); |
... | @@ -216,70 +246,19 @@ function bootstrap(global) { | ... | @@ -216,70 +246,19 @@ function bootstrap(global) { |
216 | })(phantom.casperPath); | 246 | })(phantom.casperPath); |
217 | 247 | ||
218 | // patch require (must be called in every casperjs module as of 1.1) | 248 | // patch require (must be called in every casperjs module as of 1.1) |
219 | window.require = patchRequire(global.require); | 249 | global.require = patchRequire(global.require); |
220 | 250 | ||
221 | // casper cli args | 251 | // casper cli args |
222 | phantom.casperArgs = require('cli').parse(phantomArgs); | 252 | phantom.casperArgs = require('cli').parse(phantomArgs); |
223 | 253 | ||
224 | // loaded status | 254 | if (true === phantom.casperArgs.get('cli')) { |
225 | phantom.casperLoaded = true; | 255 | initCasperCli(); |
226 | }; | ||
227 | |||
228 | /** | ||
229 | * Initializes the CasperJS Command Line Interface. | ||
230 | */ | ||
231 | phantom.initCasperCli = function initCasperCli() { | ||
232 | var fs = require("fs"); | ||
233 | var baseTestsPath = fs.pathJoin(phantom.casperPath, 'tests'); | ||
234 | |||
235 | if (!!phantom.casperArgs.options.version) { | ||
236 | console.log(phantom.casperVersion.toString()); | ||
237 | return phantom.exit(); | ||
238 | } else if (phantom.casperArgs.get(0) === "test") { | ||
239 | phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js')); | ||
240 | phantom.casperTest = true; | ||
241 | phantom.casperArgs.drop("test"); | ||
242 | } else if (phantom.casperArgs.get(0) === "selftest") { | ||
243 | phantom.casperScript = fs.absolute(fs.pathJoin(baseTestsPath, 'run.js')); | ||
244 | phantom.casperSelfTest = phantom.casperTest = true; | ||
245 | phantom.casperArgs.options.includes = fs.pathJoin(baseTestsPath, 'selftest.js'); | ||
246 | if (phantom.casperArgs.args.length <= 1) { | ||
247 | phantom.casperArgs.args.push(fs.pathJoin(baseTestsPath, 'suites')); | ||
248 | } | ||
249 | phantom.casperArgs.drop("selftest"); | ||
250 | } else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) { | ||
251 | var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.'); | ||
252 | var f = require("utils").format; | ||
253 | console.log(f('CasperJS version %s at %s, using PhantomJS version %s', | ||
254 | phantom.casperVersion.toString(), | ||
255 | phantom.casperPath, phantomVersion)); | ||
256 | console.log(fs.read(fs.pathJoin(phantom.casperPath, 'bin', 'usage.txt'))); | ||
257 | return phantom.exit(0); | ||
258 | } | ||
259 | |||
260 | if (!phantom.casperScript) { | ||
261 | phantom.casperScript = phantom.casperArgs.get(0); | ||
262 | } | ||
263 | |||
264 | if (!fs.isFile(phantom.casperScript)) { | ||
265 | console.error('Unable to open file: ' + phantom.casperScript); | ||
266 | return phantom.exit(1); | ||
267 | } | 256 | } |
268 | 257 | ||
269 | // filter out the called script name from casper args | 258 | phantom.casperLoaded = true; |
270 | phantom.casperArgs.drop(phantom.casperScript); | ||
271 | 259 | ||
272 | // passed casperjs script execution | 260 | // passed casperjs script execution |
273 | if (!phantom.injectJs(phantom.casperScript)) { | 261 | if (!phantom.injectJs(phantom.casperScript)) { |
274 | throw new CasperError('Unable to load script ' + phantom.casperScript + '; check file syntax'); | 262 | throw new CasperError('Unable to load script ' + phantom.casperScript + '; check file syntax'); |
275 | } | 263 | } |
276 | }; | 264 | })(window, phantom); |
277 | |||
278 | if (!phantom.casperLoaded) { | ||
279 | phantom.loadCasper(); | ||
280 | } | ||
281 | |||
282 | if (true === phantom.casperArgs.get('cli')) { | ||
283 | phantom.initCasperCli(); | ||
284 | } | ||
285 | } | ... | ... |
... | @@ -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'); | ... | ... |
-
Please register or sign in to post a comment