Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
John McEleney
/
casperjs
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
107eedf4
...
107eedf48cfcb0b8bef808b9e4617f99aefc2a0f
authored
2013-02-23 11:05:06 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
cleaner bootstrap.js
1 parent
6e6cfb58
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
134 additions
and
155 deletions
bin/bootstrap.js
modules/casper.js
bin/bootstrap.js
View file @
107eedf
...
...
@@ -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,94 +63,20 @@ var CasperError = function CasperError(msg) {
};
CasperError
.
prototype
=
Object
.
getPrototypeOf
(
new
Error
());
//
Patching fs
(
function
_fs
(
fs
)
{
//
casperjs env initialization
(
function
(
global
,
phantom
)
{
"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'
));
// 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
;
// 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"
;
if
(
require
.
patched
)
{
return
require
;
if
(
phantom
.
casperLoaded
)
{
return
;
}
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
);
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
;
}
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
;
/**
* Hooks in default phantomjs error handler to print a hint when a possible
* casperjs command misuse is detected.
*
*/
// 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
)
{
...
...
@@ -155,81 +84,92 @@ function bootstrap(global) {
}
};
/**
* 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
]);
}
}).
filter
(
function
_filter
(
path
)
{
return
fs
.
isDirectory
(
path
);
}).
pop
();
}
catch
(
e
)
{}
// 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'
));
if
(
!
phantom
.
casperPath
)
{
// 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
);
}
}
// 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
;
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
);
// 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
{
pkg
=
JSON
.
parse
(
require
(
'fs'
).
read
(
pkgFile
)
);
return
require
(
moduleFilePath
);
}
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
;
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
;
}
};
})(
phantom
.
casperPath
);
// patch require (must be called in every casperjs module as of 1.1)
window
.
require
=
patchRequire
(
global
.
require
);
// casper cli args
phantom
.
casperArgs
=
require
(
'cli'
).
parse
(
phantomArgs
);
// loaded status
phantom
.
casperLoaded
=
true
;
};
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.
*/
phantom
.
initCasperCli
=
function
initCasperCli
()
{
var
fs
=
require
(
"fs"
);
function
initCasperCli
()
{
var
baseTestsPath
=
fs
.
pathJoin
(
phantom
.
casperPath
,
'tests'
);
if
(
!!
phantom
.
casperArgs
.
options
.
version
)
{
...
...
@@ -268,18 +208,57 @@ function bootstrap(global) {
// 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'
));
// passed casperjs script execution
if
(
!
phantom
.
injectJs
(
phantom
.
casperScript
))
{
throw
new
CasperError
(
'Unable to load script '
+
phantom
.
casperScript
+
'; check file syntax'
);
// 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
);
if
(
!
phantom
.
casperLoaded
)
{
phantom
.
loadCasper
();
}
// 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'
))
{
phantom
.
initCasperCli
();
initCasperCli
();
}
}
phantom
.
casperLoaded
=
true
;
// passed casperjs script execution
if
(
!
phantom
.
injectJs
(
phantom
.
casperScript
))
{
throw
new
CasperError
(
'Unable to load script '
+
phantom
.
casperScript
+
'; check file syntax'
);
}
})(
window
,
phantom
);
...
...
modules/casper.js
View file @
107eedf
...
...
@@ -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'
);
...
...
Please
register
or
sign in
to post a comment