forked from iamchrismiller/grunt-casper
/
casper.js
76 lines (67 loc) · 2.45 KB
/
casper.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
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
/*global require */
"use strict";
module.exports = function (grunt) {
//casper spawn helpers
var casperlib = require('./lib/casper').init(grunt);
//get the duration of each casper task
var Duration = require("duration");
grunt.registerMultiTask('casper', 'execute casperjs tasks', function () {
var args = Array.prototype.slice.call(arguments),
options = this.options(),
done = this.async(),
taskName = this.nameArgs,
startTime = new Date();
//Once Current Task is complete
//Log Duration and Finish
function taskComplete() {
var msg = "Casper Task '" + taskName + "' took ~" + new Duration(startTime).milliseconds + "ms to complete";
grunt.log.success(msg);
done();
}
grunt.verbose.writeflags(options, 'Options');
grunt.verbose.writeflags(args, 'Arguments');
this.files.forEach(function (file) {
if (file.src.length) {
//Allow Files in each task to be run concurrently
if (options.parallel) {
//Don't Pass this through to spawn
delete options.parallel;
//Set Default Concurrency at 5 (Supposed Memory Leak > 10)
var concurrency = 5;
if (options.concurrency) {
if (concurrency > 10) {
grunt.verbose.writeln('Concurrency Too High. Max 10, updating to 10.');
concurrency = 10;
} else if (concurrency < 1) {
grunt.verbose.writeln('Concurrency Too Low. Min 1, updating to default 5.');
} else {
concurrency = options.concurrency;
}
//Don't Pass this through to spawn
delete options.concurrency;
}
//Run Tests In Parallel
if (file.src) {
grunt.util.async.forEachLimit(file.src, concurrency, function(srcFile, next) {
//Spawn Child Process
casperlib.spawnCasper(srcFile, file.dest, options, args, next);
}, function(err) {
if (err) grunt.log.write('error:', err);
//Call Done and Log Duration
taskComplete();
});
}
} else {
if (file.src) {
casperlib.spawnCasper(file.src, file.dest, options, args, function() {
//Call Done and Log Duration
taskComplete();
});
}
}
} else {
grunt.fail.warn('Unable to compile; no valid source files were found.');
}
});
});
};