-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patha11y.cjs
80 lines (71 loc) · 2.05 KB
/
a11y.cjs
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
/* eslint-disable no-console */
const { exec } = require('child_process');
const { promisify } = require('util');
const https = require('https');
const execAsync = promisify(exec);
const defaultPath = 'https://wdsbt.local'; // Update this to your theme folder path
// Create an HTTPS agent that ignores SSL certificate errors
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
async function run() {
// Dynamically import inquirer and node-fetch
const [{ default: inquirer }, { default: fetch }] = await Promise.all([
import('inquirer'),
// eslint-disable-next-line import/no-extraneous-dependencies
import('node-fetch'),
]);
// Prompt for the URL
const answers = await inquirer.prompt([
{
type: 'input',
name: 'url',
message:
'Please enter the URL to test for accessibility (leave blank to use your local: https://wdsbt.local):',
validate(value) {
if (!value) {
return true; // Allow empty input
}
const pass = value.match(/^https?:\/\/[^\s$.?#].[^\s]*$/);
if (pass) {
return true;
}
return 'Please enter a valid URL';
},
},
]);
let url = answers.url || defaultPath; // Use default path if no URL is provided
// Normalize URL by removing trailing slash if it exists
if (url.endsWith('/')) {
url = url.slice(0, -1);
}
const sitemapUrl = `${url}/wp-sitemap.xml`;
try {
const response = await fetch(sitemapUrl, { agent: httpsAgent });
if (response.ok) {
console.log(
`Sitemap found at ${sitemapUrl}. Running pa11y-ci on the sitemap...`
);
const { stdout, stderr } = await execAsync(
`pa11y-ci --sitemap ${sitemapUrl}`
);
console.log(stdout);
if (stderr) {
console.error(`Error: ${stderr}`);
}
} else {
console.log(
`No sitemap found at ${sitemapUrl}. Running pa11y-ci on the main page...`
);
const { stdout, stderr } = await execAsync(`pa11y-ci ${url}`);
console.log(stdout);
if (stderr) {
console.error(`Error: ${stderr}`);
}
}
} catch (err) {
console.error(`Execution error: ${err}`);
}
}
run();
/* eslint-enable no-console */