forked from Gerhut/hypnotize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (39 loc) · 1.1 KB
/
index.js
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
var createServer = require('http').createServer
const URL = require('url').URL
var Nightmare = require('nightmare')
function getServerURL (server, path) {
var port = server.address().port
var urlObject = new URL(path, `http://127.0.0.1:${port}`)
return urlObject
}
// function before (func, before) {
// return function () {
// var args = Array.prototype.slice.call(arguments)
// before.call(this, args)
// return func.apply(this, args)
// }
// }
function watchArgs (func, watcher) {
return function () {
var args = Array.prototype.slice.call(arguments)
watcher(args)
return func.apply(this, args)
}
}
function hypnotize (app, options) {
var nightmare = Nightmare(options)
var server = createServer(app)
nightmare.queue(function (callback) {
nightmare.child.call = watchArgs(nightmare.child.call,
function (args) {
if (args[0] === 'goto') {
args[1] = getServerURL(server, args[1])
} else if (args[0] === 'quit') {
server.close()
}
})
server.listen(callback)
})
return nightmare
}
module.exports = hypnotize