-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
151 lines (133 loc) · 4.67 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
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
'use strict';
var path = require('path');
var fs = require('fs');
var through = require('through2');
var spawn = require('child_process').spawn;
var merge = require('merge');
var chalk = require('chalk');
var gutil = require('gulp-util');
var phantomDir = path.dirname(path.dirname(require.resolve('phantomjs')));
var phantomCLI = path.resolve(phantomDir, 'bin/phantomjs');
var run = path.resolve(__dirname, './lib/run.js');
var reports = {};
var lastReport = [];
module.exports = function(opts) {
opts = merge({
ignoreSSL: true,
webSecurity: false,
standard: 'WCAG2AA',
verbose: false,
timeout: 60000
}, opts);
return through.obj(function(file, enc, next) {
var running = true;
var args = [
'--ignore-ssl-errors=' + opts.ignoreSSL,
'--web-security=' + opts.webSecurity,
run,
file.path,
opts.standard
];
if (opts.verbose) {
gutil.log('Running PhantomJS', phantomCLI, args);
}
var child = spawn(phantomCLI, args);
var output = '';
setTimeout(function() {
if (running) {
gutil.log(chalk.red('HTMLCS timeout ('+opts.timeout+' seconds)'), file.path);
child.kill();
}
}, opts.timeout);
child.stdout.on('data', function(data) {
if (opts.verbose) {
gutil.log('received data:', data.toString().length, 'bytes');
}
output += data.toString();
});
child.stderr.on('data', function(data) {
console.log(chalk.red(data.toString()));
});
child.on('exit', function() {
running = false;
try {
reports[file.path] = JSON.parse(output);
lastReport = reports[file.path];
file.htmlcs = {
opts: opts,
report: reports[file.path]
};
} catch(e) {
console.log(chalk.red(e.message));
console.log(chalk.red(e.stack));
console.log(chalk.red('Writing temporary output to htmlcs-debug.log'));
fs.writeFileSync(path.join(process.cwd(), 'htmlcs-debug.log'), output);
}
this.push(file);
next();
}.bind(this));
});
};
module.exports.getLastReport = function(filter) {
var report = lastReport;
if (filter) {
report.messages = lastReport.messages.filter(function(item){
return filter.indexOf(item.type) !== -1;
});
}
return report;
};
module.exports.reporter = function(opts) {
opts = merge({
filter: null,
showTrace: false
}, opts);
return through.obj(function(file, enc, next) {
var summary = {};
if (!file.htmlcs || !file.htmlcs.report) {
this.push(file);
return next();
}
if (file.htmlcs.report.hasOwnProperty('error')) {
console.log(chalk.red('ERROR [PhantomJS runtime]: '+file.htmlcs.report.error.msg), file.path);
if (opts.showTrace) {
console.log(file.htmlcs.report.error.trace);
}
return next();
}
file.htmlcs.report.messages.forEach(function(item) {
var key = item.type + 'S';
if (!summary.hasOwnProperty(key)) {
summary[key] = 0;
}
summary[key] += 1;
});
if (summary.ERRORS) {
gutil.log(chalk.red(summary.ERRORS) + ' sniff error' +
(summary.ERRORS > 1 || summary.ERRORS < 1 ? 's' : '') +
' found in:', chalk.magenta(file.path));
}
file.htmlcs.report.messages.forEach(function(item) {
if (!opts.filter || opts.filter.indexOf(item.type) !== -1) {
console.log(
item.type + ': ' +
chalk.cyan(item.code.split('.').slice(0, 3).join(' ')));
console.log(' ' + item.msg);
console.log(' ' + item.outerHTML);
}
});
if (file.htmlcs.report.errors.length) {
gutil.log(chalk.red(file.htmlcs.report.errors.length) + ' runtime error' +
(file.htmlcs.report.errors.length > 1 || file.htmlcs.report.errors.length < 1 ? 's' : '') +
' found in:', chalk.magenta(file.path));
file.htmlcs.report.errors.forEach(function(error) {
console.log(chalk.red(error.msg));
if (opts.showTrace) {
console.log(error.trace);
}
});
}
this.push(file);
next();
});
};