-
Notifications
You must be signed in to change notification settings - Fork 68
/
index.ts
165 lines (147 loc) · 3.52 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import type { OptionValues } from 'commander';
import * as colors from 'colors';
import {
parseBrowser,
parseUrl,
reporter,
getAxeVersion,
getAxeSource,
saveOutcome,
bold,
error,
italics,
link
} from '../lib/utils';
import axeTestUrls from '../lib/axe-test-urls';
import event from '../lib/events';
import { startDriver } from '../lib/webdriver';
const cli = async (
args: OptionValues,
url: { args: string[] }
): Promise<void> => {
const {
save,
stdout,
dir,
exit,
timer,
showErrors,
reporter: noReporter,
chromeOptions,
verbose,
timeout,
path,
include,
exclude,
tags,
rules,
disable,
loadDelay
} = args;
const silentMode = !!stdout;
args.axeSource = getAxeSource(args.axeSource);
if (!args.axeSource) {
console.error(error('Unable to find the axe-core source file'));
process.exit(2);
}
args.browser = parseBrowser(args.browser);
/* istanbul ignore if */
if (chromeOptions) {
/* istanbul ignore if */
if (args.browser !== 'chrome-headless') {
console.error(
error(
'You may only provide --chrome-options when using headless chrome'
)
);
process.exit(2);
}
}
const driverConfigs = {
browser: args.browser,
timeout,
chromeOptions,
path
};
args.driver = startDriver(driverConfigs);
const cliReporter = reporter(noReporter, silentMode);
const axeVersion = getAxeVersion(args.axeSource);
if (!silentMode) {
console.log(
colors.bold('Running axe-core ' + axeVersion + ' in ' + args.browser)
);
}
const urls = url.args.map(parseUrl);
/* istanbul ignore if */
if (urls.length === 0) {
console.error(error('No url was specified. Check `axe --help` for help\n'));
}
const events = event({
silentMode,
timer,
cliReporter,
verbose,
exit
});
const testPageConfigParams = {
driver: args.driver,
timer,
loadDelay,
axeSource: args.axeSource,
include,
exclude,
tags,
rules,
disable
};
try {
const outcome = await axeTestUrls(urls, testPageConfigParams, events);
if (silentMode) {
process.stdout.write(JSON.stringify(outcome, null, 2));
return;
}
if (timer) {
console.timeEnd('Total test time');
}
/* istanbul ignore if */
if (Array.isArray(outcome)) {
console.log(bold('Testing complete of %d pages\n'), outcome.length);
}
if (save || dir) {
try {
const fileName = saveOutcome(outcome, save, dir);
console.log('Saved file at', fileName, '\n');
} catch (e) {
/* istanbul ignore next */
console.error(error('Unable to save file!\n') + e);
process.exit(1);
}
}
/* istanbul ignore if */
if (silentMode) {
return;
}
console.log(
italics(
'Please note that only 20% to 50% of all accessibility ' +
'issues can automatically be detected. \nManual testing is ' +
'always required. For more information see:\n%s\n'
),
link('https://dequeuniversity.com/curriculum/courses/testingmethods')
);
} catch (e) {
/* istanbul ignore else */
if (!showErrors) {
console.error(error('An error occurred while testing this page.'));
} else {
console.error(error('Error: %j \n $s'), e.message, e.stack);
}
console.error(
'Please report the problem to: ' +
link('https://github.com/dequelabs/axe-core-npm/issues/') +
'\n'
);
process.exit(1);
}
};
export default cli;