-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathcsv.js
74 lines (65 loc) · 2.02 KB
/
csv.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
'use strict';
const fs = require('fs');
const {resolve, isAbsolute, dirname} = require('path');
function writeReport(fileName, data) {
try {
const dirPath = dirname(fileName);
if (!(fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory())) {
fs.mkdirSync(dirPath, {recursive: true});
}
fs.writeFileSync(fileName, data);
} catch (error) {
console.error(`Unable to write ${fileName}`);
console.error(error);
}
}
function resolveFile(fileName) {
if (typeof fileName !== 'string') {
return null;
}
return isAbsolute(fileName) ? fileName : resolve(process.cwd(), fileName);
}
function stringify(value) {
if (typeof value === 'undefined' || value === null || value === '') {
return '';
}
const str = String(value);
return needsQuote(str) ? quoteField(str) : str;
}
function quoteField(field) {
return `"${field.replace(/"/g, '""')}"`;
}
function needsQuote(str) {
return str.includes(',') || str.includes('\r') || str.includes('\n') || str.includes('"');
}
module.exports = function csvReporter(options = {}) {
const fileName = resolveFile(options.fileName);
return {
afterAll(report) {
let csvString = 'url,code,type,typeCode,message,context,selector,runner\n';
const results = report.results;
for (const url in results) {
if (Object.prototype.hasOwnProperty.call(results, url)) {
const urlErrors = report.results[url];
for (const errorRow of urlErrors) {
const typeCode = errorRow.typeCode;
const message = stringify(errorRow.message);
const context = stringify(errorRow.context);
const selector = stringify(errorRow.selector);
csvString += `${url},${errorRow.code},${errorRow.type},${typeCode},` +
`${message},${context},` +
`${selector},${errorRow.runner}\n`;
}
}
}
// If reporter options specify an output file, write to file.
// Otherwise, write to console for backwards compatibility with
// previous --csv CLI option.
if (fileName) {
writeReport(fileName, csvString);
} else {
console.log(csvString);
}
}
};
};