-
Notifications
You must be signed in to change notification settings - Fork 0
/
a11y-cli.js
95 lines (80 loc) · 2.06 KB
/
a11y-cli.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
const core = require("@actions/core");
const exec = require("@actions/exec");
const io = require("@actions/io");
const quote = require("quote");
const waitOnUrl = require("wait-on");
const cliParser = require("argument-vector")();
const pa11y = require("pa11y");
const logIssue = (issue, failOnError) => {
core.debug(failOnError);
if (failOnError) {
core.setFailed(issue.message);
} else {
core.warning(issue.message);
}
};
const startServer = (startCommand) => {
const cwd = process.cwd();
const args = cliParser.parse(startCommand);
core.debug(`parsed command: ${args.join(" ")}`);
return io.which(args[0], true).then((toolPath) => {
core.debug(`found command "${toolPath}"`);
core.debug(`with arguments ${args.slice(1).join(" ")}`);
const toolArguments = args.slice(1);
const argsString = toolArguments.join(" ");
core.debug(`running ${quote(toolPath)} ${argsString} in ${cwd}`);
exec.exec(quote(toolPath), toolArguments);
});
};
const runTests = async (urls, failOnError) => {
const promises = urls.map((url) => {
return pa11y(url, {
chromeLaunchConfig: {
args: ["--no-sandbox"],
},
});
});
return await Promise.all(promises).then((tests) => {
let failed = false;
tests.forEach(({ pageUrl, issues }) => {
if (issues.length) {
failed = true;
core.startGroup(pageUrl);
}
issues.forEach((issue) => {
logIssue(issue, failOnError);
});
if (issues.length) {
core.endGroup();
}
});
if (failed) {
throw new Error("There are failing tests");
}
});
};
module.exports = async ({
urls,
failOnError,
startCommand,
waitOn,
waitOnTimeout,
}) => {
try {
if (startCommand) {
await startServer(startCommand);
}
if (waitOn) {
await waitOnUrl({
resources: [waitOn],
interval: 1000,
timeout: waitOnTimeout * 1000,
});
}
await runTests(urls, failOnError);
process.exit(0);
} catch (ex) {
core.debug(ex);
process.exit(failOnError ? 1 : 0);
}
};