-
Notifications
You must be signed in to change notification settings - Fork 1
/
pa11y.js
executable file
·127 lines (107 loc) · 2.9 KB
/
pa11y.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
#!/usr/bin/env node
/**
* Dependencies
*/
const pa11y = require('pa11y');
const path = require('path');
const fs = require('fs');
const args = require(`${__dirname}/util/args`).args;
const resolve = require(`${__dirname}/util/resolve`);
const cnsl = require(`${__dirname}/util/console`);
const config = resolve('config/pa11y');
const alerts = resolve('config/alerts');
const global = resolve('config/global');
/**
* Constants
*/
const DIST = path.join(global.base, global.dist);
const BASE_PATH = DIST;
const EXT = '.html';
/**
* a11y Linter
*
* @param {String} file Glob or file path to lint
*
* @return Linting data
*/
const a11y = async (file = `${process.env.PWD}/dist/index.html`) => {
if (!args.nondescript && !args.silent) {
cnsl.lint(`${alerts.loading} Running Pa11y CI on ${alerts.str.path(file)}`);
}
let results = await pa11y(file, config);
if (results.issues.length) {
if (args.nondescript || args.silent) {
cnsl.lint(`${alerts.str.path(results.pageUrl)}`);
} else {
cnsl.lint(`${alerts.error} Pa11y suggestions for ${alerts.str.path(results.pageUrl)}`);
}
results.issues.forEach(issue => {
cnsl.lint([
`${alerts.str.string(issue.context.split('\n').map(s => s.trim()).join(''))}\n`,
`${alerts.str.comment(issue.runner)}\t`,
issue.type === 'error' ? alerts.str.error('error') : alerts.str.warning('warn'),
`\t${issue.message} `,
`${alerts.str.comment(issue.code.split(',').pop())}\n`
].join(''));
});
} else {
if (!args.nondescript && !args.silent) {
cnsl.lint(`${alerts.success} No Pa11y suggestions for ${alerts.str.path(results.pageUrl)}`);
}
}
return results;
};
/**
* Main script
*
* @param {String} file Glob or file path to lint
*/
const main = async (file) => {
try {
await a11y(file);
return file;
} catch (err) {
cnsl.error(`Pa11y failed (main): ${err.stack}`);
}
};
/**
* Read a specific file or if it's a directory, read all of the files in it
*
* @param {String} file A single file or directory to recursively walk
* @param {String} dir The base directory of the file
*/
const walk = async (file, dir = BASE_PATH) => {
file = (file.includes(dir)) ? file : path.join(dir, file);
if (file.includes(EXT)) {
await main(file);
} else if (fs.lstatSync(file).isDirectory()) {
try {
let files = fs.readdirSync(file, 'utf-8');
for (let i = files.length - 1; i >= 0; i--) {
await walk(files[i], file);
}
} catch (err) {
cnsl.error(`Pa11y failed (walk): ${err.stack}`);
}
}
};
/**
* Runner for the script
*
* @param {String} dir The base directory of the file
*/
const run = async (dir = BASE_PATH) => {
await walk(dir);
cnsl.success(`Pa11y finished`);
process.exit();
};
/**
* Export our methods
*
* @type {Object}
*/
module.exports = {
main: main,
run: run,
config: config
};