bootstrap.js
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*!
* Casper is a navigation utility for PhantomJS.
*
* Documentation: http://casperjs.org/
* Repository: http://github.com/n1k0/casperjs
*
* Copyright (c) 2011-2012 Nicolas Perriault
*
* Part of source code is Copyright Joyent, Inc. and other Node contributors.
*
* 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.
*
*/
/*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/');
}
// Common polyfills
if (typeof Function.prototype.bind !== "function") {
Function.prototype.bind = function(scope) {
"use strict";
var _function = this;
return function() {
return _function.apply(scope, arguments);
};
};
}
// 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());
// 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;
}
function __die(message) {
console.error(message);
phantom.exit(1);
}
function __terminate(message) {
console.log(message);
phantom.exit();
}
(function(version) {
// required version check
if (version.major !== 1) {
return __die('CasperJS needs PhantomJS v1.x');
}
if (version.minor < 8) {
return __die('CasperJS needs at least PhantomJS v1.8 or later.');
}
if (version.patch < 1) {
return __die('CasperJS needs at least PhantomJS v1.8.1 or later.');
}
})(phantom.version);
// 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(/.*\//, '');
};
}
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('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'));
// 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) {
return __die("Couldn't find nor compute phantom.casperPath, exiting.");
}
}
/**
* 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 patchedRequire = function _require(path) {
var moduleFilePath = fs.pathJoin(phantom.casperPath, 'modules', path + '.js');
if (!fs.exists(moduleFilePath)) {
return require(path); // native phantomjs' require() behavior
}
try {
return require(moduleFilePath);
} catch (e) {
var error = new CasperError('__mod_error(' + path + ':' + e.line + '):: ' + e);
error.file = moduleFilePath;
error.line = e.line;
error.stack = e.stack;
error.stackArray = JSON.parse(JSON.stringify(e.stackArray));
if (error.stackArray.length > 0) {
error.stackArray[0].sourceURL = moduleFilePath;
}
throw error;
}
};
patchedRequire.cache = require.cache;
patchedRequire.extensions = require.extensions;
patchedRequire.stubs = require.stubs;
patchedRequire.patched = true;
return patchedRequire;
}
global.patchRequire = patchRequire;
/**
* Initializes the CasperJS Command Line Interface.
*
*/
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;
return __terminate([
f('CasperJS version %s at %s, using PhantomJS version %s',
phantom.casperVersion.toString(),
phantom.casperPath,
phantomVersion)
, fs.read(fs.pathJoin(phantom.casperPath, 'bin', 'usage.txt'))
].join('\n'));
}
if (!phantom.casperScript) {
phantom.casperScript = phantom.casperArgs.get(0);
}
if (!fs.isFile(phantom.casperScript)) {
return __die('Unable to open file: ' + phantom.casperScript);
}
// filter out the called script name from casper args
phantom.casperArgs.drop(phantom.casperScript);
}
// Embedded, up-to-date, validatable & controlable CoffeeScript
phantom.injectJs(fs.pathJoin(phantom.casperPath, 'modules', 'vendors', 'coffee-script.js'));
// CasperJS version, extracted from package.json - see http://semver.org/
phantom.casperVersion = (function getVersion(path) {
var parts, patchPart, pkg, pkgFile;
pkgFile = fs.absolute(fs.pathJoin(path, 'package.json'));
if (!fs.exists(pkgFile)) {
throw new CasperError('Cannot find package.json at ' + pkgFile);
}
try {
pkg = JSON.parse(require('fs').read(pkgFile));
} catch (e) {
throw new CasperError('Cannot read package file contents: ' + e);
}
parts = pkg.version.trim().split(".");
if (parts.length < 3) {
throw new CasperError("Invalid version number");
}
patchPart = parts[2].split('-');
return {
major: ~~parts[0] || 0,
minor: ~~parts[1] || 0,
patch: ~~patchPart[0] || 0,
ident: patchPart[1] || "",
toString: function toString() {
var version = [this.major, this.minor, this.patch].join('.');
if (this.ident) {
version = [version, this.ident].join('-');
}
return version;
}
};
})(phantom.casperPath);
// patch require (must be called in every casperjs module as of 1.1)
global.require = patchRequire(global.require);
// casper cli args
phantom.casperArgs = require('cli').parse(phantomArgs);
if (true === phantom.casperArgs.get('cli')) {
initCasperCli();
}
// casper loading status flag
phantom.casperLoaded = true;
// passed casperjs script execution
if (phantom.casperScript && !phantom.injectJs(phantom.casperScript)) {
return __die('Unable to load script ' + phantom.casperScript + '; check file syntax');
}
})(window, phantom);