-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
126 lines (104 loc) · 3.01 KB
/
index.ts
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
import { readFileSync, unlinkSync } from 'fs';
import { createHash } from 'crypto';
import shellExec from 'shell-exec';
type ViolationInstance = {
impact: string;
html: string;
target: Array<string>;
failureSummary: string;
};
type ViolationType = {
id: string;
impact: string;
tags: Array<string>;
description: string;
help: string;
helpUrl: string;
nodes: Array<ViolationInstance>;
};
type AxeCliResult = {
violations: Array<ViolationType>;
};
const CHROMEDRIVER_PATH = process.env.CHROMEDRIVER_PATH || '/usr/local/bin/chromedriver';
const DEBUG = process.env.DEBUG === 'true';
const EXTRANEOUS = process.env.EXTRANEOUS === 'true';
const OUTPUT_DIRECTORY = process.env.OUTPUT_DIRECTORY || 'tmp';
const VERBOSE = process.env.VERBOSE === 'true';
(async () => {
const urlToTest = process.argv[2];
if (!urlToTest) {
console.error('You must specify a URL to test');
process.exit(1);
}
const urlToTestHash = createHash('sha256').update(urlToTest).digest('hex');
const outputFilePath = `${OUTPUT_DIRECTORY}/${urlToTestHash}.json`;
const { stderr, stdout } = await shellExec(`
axe \
--chromedriver-path=${CHROMEDRIVER_PATH} \
--save ${outputFilePath} \
${urlToTest}
`);
if (stderr.length) {
console.error(stderr.trim());
process.exit(1);
}
if (!stdout.length) {
console.error(`Could not generate report for: ${urlToTest}`);
process.exit(1);
}
let axeCliResult: AxeCliResult | undefined = undefined;
try {
axeCliResult = JSON.parse(readFileSync(outputFilePath, 'utf-8'))[0];
} catch (err) {
console.error(err);
}
if (DEBUG) {
console.log({ axeCliResult });
}
try {
unlinkSync(outputFilePath);
} catch (err) {
console.error(err);
}
if (!axeCliResult) {
process.exit(1);
}
const filteredViolationTypes = (axeCliResult?.violations || []).filter(violation => {
if (EXTRANEOUS) {
return true;
} else {
return !(violation.tags || []).includes('best-practice');
}
});
if (DEBUG) {
console.log({ filteredViolationTypes });
}
if (filteredViolationTypes.length === 0) {
console.log(`No violations found for: ${urlToTest}`);
process.exit(0);
}
let violationCount = 0;
filteredViolationTypes.forEach(violationType => {
violationCount += (violationType.nodes || []).length;
});
console.log(`Found ${violationCount} violations for: ${urlToTest}`);
if (!VERBOSE) {
process.exit(0);
}
filteredViolationTypes.forEach(violationType => {
console.log({
id: violationType.id,
impact: violationType.impact,
tags: (violationType.tags || []).join(', '),
description: violationType.description,
help: `${violationType.help} (Reference: ${violationType.helpUrl})`,
instances: (violationType.nodes || []).map(violationInstance => {
return {
html: violationInstance.html,
targets: (violationInstance.target || []).join(', '),
summary: violationInstance.failureSummary
};
})
});
});
})();