@dcaldeira/tooling 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -34
- package/checks/a11y.mjs +206 -206
- package/checks/architecture.mjs +137 -137
- package/checks/audit.mjs +121 -121
- package/checks/bundle.mjs +126 -126
- package/checks/dependencies.mjs +277 -277
- package/checks/duplication.mjs +135 -0
- package/checks/lint-scss.mjs +363 -349
- package/checks/package.mjs +30 -30
- package/checks/playwright-summary.mjs +53 -53
- package/checks/sass.mjs +67 -67
- package/checks/scan-secrets.mjs +58 -58
- package/checks/shared.mjs +86 -86
- package/checks/translations.mjs +55 -55
- package/checks/visual-compare.mjs +148 -148
- package/checks/visual-report-html.mjs +470 -470
- package/checks/visual-report.mjs +13 -13
- package/config.schema.json +977 -927
- package/configs/eslint.mjs +27 -27
- package/configs/knip.json +11 -11
- package/configs/playwright.mjs +15 -15
- package/configs/vitest.mjs +16 -16
- package/package.json +105 -105
- package/release/finalize.mjs +65 -65
- package/release/github.mjs +64 -64
- package/release/prepare.mjs +30 -30
- package/runner/browsers.mjs +73 -73
- package/runner/cache.mjs +121 -121
- package/runner/ci-plan.mjs +54 -54
- package/runner/cli.mjs +250 -248
- package/runner/config.mjs +148 -148
- package/runner/docs-install.mjs +93 -93
- package/runner/execute.mjs +298 -286
- package/runner/generate.mjs +122 -122
- package/runner/git.mjs +122 -122
- package/runner/graph.mjs +250 -243
- package/runner/insights.mjs +313 -313
- package/runner/provision.mjs +53 -53
- package/runner/repo-standard.mjs +104 -0
- package/runner/report.mjs +35 -35
- package/runner/serve-static.mjs +76 -76
- package/runner/setup.mjs +241 -237
- package/runner/summary.mjs +112 -112
- package/runner/watch.mjs +82 -82
- package/templates/ci.yml +25 -25
- package/templates/dependabot-merge.yml +13 -13
- package/templates/dependabot.yml +26 -26
- package/templates/pre-push +13 -13
- package/templates/release.yml +23 -23
package/README.md
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
# Repository tooling
|
|
2
|
-
|
|
3
|
-
`@dcaldeira/tooling` shares one check graph between development, push gates and reusable GitHub Actions workflows. Node 22.13 or later; npm workspaces; no paid service is required.
|
|
4
|
-
|
|
5
|
-
```sh
|
|
6
|
-
npm ci --ignore-scripts
|
|
7
|
-
npm test
|
|
8
|
-
npm run pack:tooling
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
Consumers install an exact public npm version, commit their lockfile and pin reusable workflows to the full commit SHA. The initial version is **0.1.0**. No package or repository credentials are embedded.
|
|
12
|
-
|
|
13
|
-
| Command | Purpose |
|
|
14
|
-
| ---------------------------------------------------------- | ------------------------------------------------------------------------------- |
|
|
15
|
-
| `tooling --profile=dev --changed` | Changed formatting/lint/styles, related unit tests and relevant translations |
|
|
16
|
-
| `tooling --profile=push --changed --bail` | Every applicable check; records a reusable pass only for a clean unchanged tree |
|
|
17
|
-
| `tooling --profile=ci --all` | Complete validation with prerequisites enforced |
|
|
18
|
-
| `tooling --profile=ci --changed --base=origin/main --plan` | Explain selection without running checks |
|
|
19
|
-
|
|
20
|
-
See [project features](https://github.com/DuarteCaldeira/tooling/blob/main/docs/project-features.md), [setup](https://github.com/DuarteCaldeira/tooling/blob/main/docs/setup.md), [configuration](https://github.com/DuarteCaldeira/tooling/blob/main/docs/configuration.md), [release trust](https://github.com/DuarteCaldeira/tooling/blob/main/docs/releases.md), [migration](https://github.com/DuarteCaldeira/tooling/blob/main/docs/migration.md) and [troubleshooting](https://github.com/DuarteCaldeira/tooling/blob/main/docs/troubleshooting.md). Rookie's configuration and adoption assets are in [examples/rookie](https://github.com/DuarteCaldeira/tooling/blob/main/examples/rookie).
|
|
21
|
-
|
|
22
|
-
Use `tooling init --preset=react-vite` or `tooling init --preset=workspace --app=frontend` to scaffold a consumer, then `tooling doctor` to check its setup. Pass `--tooling-sha=<40-character SHA>` to pin generated workflows. Templates ship inside the npm package.
|
|
23
|
-
|
|
24
|
-
Independent checks run with bounded concurrency and shared-resource locks. `--concurrency=1` runs sequentially. Development watch mode responds to filesystem events, with ignored generated files filtered out. Functional and visual browser suites are disjoint by default. Documentation CI can opt into a locked standalone formatter installation.
|
|
25
|
-
|
|
26
|
-
Workspace scopes follow declared shared-code dependencies, and deterministic diagnostics reuse successful results by their inputs. ESLint uses persistent content caches; noEmit TypeScript projects can opt into CI metadata persistence. Full validation with `--all` bypasses diagnostic success records, and `--no-cache` disables runner-managed reuse. Empty plans avoid installation and reports. Browser provisioning is shared by PR and release workflows and includes Lighthouse-only projects.
|
|
27
|
-
|
|
28
|
-
Docs select only formatting. Styles select formatting and style lint. Mixed changes use their union. Dependencies, shared tooling and unknown files broaden validation. Local push and hosted CI use the same definitions. Missing prerequisites, skipped required checks, failed dependencies and interrupted checks cannot record a pass.
|
|
29
|
-
|
|
30
|
-
The HTML summary links to Playwright's self-contained report, which contains expected/actual/diff images, scenario names, viewports, browser details and traces. Reports are retained for three days in Actions.
|
|
31
|
-
|
|
32
|
-
MIT licensed. Public publishing requires GitHub/npm credentials or npm trusted publishing to be configured by the owner. Local preparation alone does not publish anything.
|
|
33
|
-
|
|
34
|
-
Opt-in architecture, rendered accessibility and dependency-consistency checks share the same runner. Use `tooling generate component --name=UserCard` or `tooling generate hook --name=useProfile` for typed scaffolds, and `tooling history` for recent validation performance. PR/release workflows publish an Actions summary with report artifacts.
|
|
1
|
+
# Repository tooling
|
|
2
|
+
|
|
3
|
+
`@dcaldeira/tooling` shares one check graph between development, push gates and reusable GitHub Actions workflows. Node 22.13 or later; npm workspaces; no paid service is required.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm ci --ignore-scripts
|
|
7
|
+
npm test
|
|
8
|
+
npm run pack:tooling
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Consumers install an exact public npm version, commit their lockfile and pin reusable workflows to the full commit SHA. The initial version is **0.1.0**. No package or repository credentials are embedded.
|
|
12
|
+
|
|
13
|
+
| Command | Purpose |
|
|
14
|
+
| ---------------------------------------------------------- | ------------------------------------------------------------------------------- |
|
|
15
|
+
| `tooling --profile=dev --changed` | Changed formatting/lint/styles, related unit tests and relevant translations |
|
|
16
|
+
| `tooling --profile=push --changed --bail` | Every applicable check; records a reusable pass only for a clean unchanged tree |
|
|
17
|
+
| `tooling --profile=ci --all` | Complete validation with prerequisites enforced |
|
|
18
|
+
| `tooling --profile=ci --changed --base=origin/main --plan` | Explain selection without running checks |
|
|
19
|
+
|
|
20
|
+
See [project features](https://github.com/DuarteCaldeira/tooling/blob/main/docs/project-features.md), [setup](https://github.com/DuarteCaldeira/tooling/blob/main/docs/setup.md), [configuration](https://github.com/DuarteCaldeira/tooling/blob/main/docs/configuration.md), [release trust](https://github.com/DuarteCaldeira/tooling/blob/main/docs/releases.md), [migration](https://github.com/DuarteCaldeira/tooling/blob/main/docs/migration.md) and [troubleshooting](https://github.com/DuarteCaldeira/tooling/blob/main/docs/troubleshooting.md). Rookie's configuration and adoption assets are in [examples/rookie](https://github.com/DuarteCaldeira/tooling/blob/main/examples/rookie).
|
|
21
|
+
|
|
22
|
+
Use `tooling init --preset=react-vite` or `tooling init --preset=workspace --app=frontend` to scaffold a consumer, then `tooling doctor` to check its setup. Pass `--tooling-sha=<40-character SHA>` to pin generated workflows. Templates ship inside the npm package.
|
|
23
|
+
|
|
24
|
+
Independent checks run with bounded concurrency and shared-resource locks. `--concurrency=1` runs sequentially. Development watch mode responds to filesystem events, with ignored generated files filtered out. Functional and visual browser suites are disjoint by default. Documentation CI can opt into a locked standalone formatter installation.
|
|
25
|
+
|
|
26
|
+
Workspace scopes follow declared shared-code dependencies, and deterministic diagnostics reuse successful results by their inputs. ESLint uses persistent content caches; noEmit TypeScript projects can opt into CI metadata persistence. Full validation with `--all` bypasses diagnostic success records, and `--no-cache` disables runner-managed reuse. Empty plans avoid installation and reports. Browser provisioning is shared by PR and release workflows and includes Lighthouse-only projects.
|
|
27
|
+
|
|
28
|
+
Docs select only formatting. Styles select formatting and style lint. Mixed changes use their union. Dependencies, shared tooling and unknown files broaden validation. Local push and hosted CI use the same definitions. Missing prerequisites, skipped required checks, failed dependencies and interrupted checks cannot record a pass.
|
|
29
|
+
|
|
30
|
+
The HTML summary links to Playwright's self-contained report, which contains expected/actual/diff images, scenario names, viewports, browser details and traces. Reports are retained for three days in Actions.
|
|
31
|
+
|
|
32
|
+
MIT licensed. Public publishing requires GitHub/npm credentials or npm trusted publishing to be configured by the owner. Local preparation alone does not publish anything.
|
|
33
|
+
|
|
34
|
+
Opt-in architecture, rendered accessibility and dependency-consistency checks share the same runner. Use `tooling generate component --name=UserCard` or `tooling generate hook --name=useProfile` for typed scaffolds, and `tooling history` for recent validation performance. PR/release workflows publish an Actions summary with report artifacts.
|
package/checks/a11y.mjs
CHANGED
|
@@ -1,206 +1,206 @@
|
|
|
1
|
-
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
-
import { mkdirSync } from 'node:fs';
|
|
3
|
-
import { join } from 'node:path';
|
|
4
|
-
import { setTimeout as delay } from 'node:timers/promises';
|
|
5
|
-
import { pathToFileURL } from 'node:url';
|
|
6
|
-
import { executable } from '../runner/execute.mjs';
|
|
7
|
-
import { inside, loadConfig } from '../runner/config.mjs';
|
|
8
|
-
import { peer, rootDirectory, writeDetails } from './shared.mjs';
|
|
9
|
-
|
|
10
|
-
export async function startServer(root, settings) {
|
|
11
|
-
const cwd = inside(root, settings.cwd ?? '.');
|
|
12
|
-
const command = executable(cwd, settings.command[0]);
|
|
13
|
-
if (!command) throw new Error(`Missing server executable ${settings.command[0]}`);
|
|
14
|
-
const child = spawn(command[0], [...command.slice(1), ...settings.command.slice(1)], {
|
|
15
|
-
cwd,
|
|
16
|
-
env: process.env,
|
|
17
|
-
stdio: ['ignore', 'ignore', 'pipe'],
|
|
18
|
-
windowsHide: true,
|
|
19
|
-
detached: process.platform !== 'win32',
|
|
20
|
-
});
|
|
21
|
-
let failure,
|
|
22
|
-
output = '';
|
|
23
|
-
child.once('error', (error) => {
|
|
24
|
-
failure = error;
|
|
25
|
-
});
|
|
26
|
-
child.stderr.on('data', (chunk) => {
|
|
27
|
-
output = (output + chunk).slice(-4000);
|
|
28
|
-
});
|
|
29
|
-
const stop = async () => {
|
|
30
|
-
if (!child.pid) return;
|
|
31
|
-
if (process.platform === 'win32') {
|
|
32
|
-
if (child.exitCode === null)
|
|
33
|
-
spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], {
|
|
34
|
-
stdio: 'ignore',
|
|
35
|
-
windowsHide: true,
|
|
36
|
-
timeout: 5000,
|
|
37
|
-
});
|
|
38
|
-
} else {
|
|
39
|
-
try {
|
|
40
|
-
process.kill(-child.pid, 'SIGTERM');
|
|
41
|
-
} catch (error) {
|
|
42
|
-
if (error.code !== 'ESRCH') throw error;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
if (child.exitCode === null) {
|
|
46
|
-
await Promise.race([
|
|
47
|
-
new Promise((resolve) => child.once('exit', resolve)),
|
|
48
|
-
delay(1000, undefined, { ref: false }),
|
|
49
|
-
]);
|
|
50
|
-
if (child.exitCode === null && process.platform !== 'win32') {
|
|
51
|
-
try {
|
|
52
|
-
process.kill(-child.pid, 'SIGKILL');
|
|
53
|
-
} catch (error) {
|
|
54
|
-
if (error.code !== 'ESRCH') throw error;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
try {
|
|
60
|
-
const deadline = Date.now() + (settings.timeout ?? 30000);
|
|
61
|
-
while (Date.now() < deadline) {
|
|
62
|
-
if (failure || child.exitCode !== null)
|
|
63
|
-
throw new Error(
|
|
64
|
-
`Accessibility server did not stay running: ${failure?.message ?? (output || child.exitCode)}`,
|
|
65
|
-
);
|
|
66
|
-
try {
|
|
67
|
-
const response = await fetch(settings.url, { signal: AbortSignal.timeout(1000) });
|
|
68
|
-
if (response.ok) return stop;
|
|
69
|
-
} catch {}
|
|
70
|
-
await delay(100);
|
|
71
|
-
}
|
|
72
|
-
throw new Error(
|
|
73
|
-
`Accessibility server did not become ready within ${settings.timeout ?? 30000} ms`,
|
|
74
|
-
);
|
|
75
|
-
} catch (error) {
|
|
76
|
-
await stop();
|
|
77
|
-
throw error;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export async function scanAccessibility(
|
|
82
|
-
settings,
|
|
83
|
-
{ chromium, AxeBuilder, root = process.cwd(), server = startServer } = {},
|
|
84
|
-
) {
|
|
85
|
-
if (!settings?.routes?.length) throw new Error('Configure a11y.baseURL and at least one route');
|
|
86
|
-
const base = new URL(settings.baseURL);
|
|
87
|
-
if (!['http:', 'https:'].includes(base.protocol))
|
|
88
|
-
throw new Error('Accessibility baseURL must use HTTP or HTTPS');
|
|
89
|
-
const impacts = settings.failImpacts ?? ['minor', 'moderate', 'serious', 'critical'];
|
|
90
|
-
let stop, browser;
|
|
91
|
-
const pages = [],
|
|
92
|
-
errors = [];
|
|
93
|
-
try {
|
|
94
|
-
if (settings.server)
|
|
95
|
-
stop = await server(root, {
|
|
96
|
-
...settings.server,
|
|
97
|
-
url: settings.server.url ?? settings.baseURL,
|
|
98
|
-
});
|
|
99
|
-
mkdirSync(join(root, '.tooling'), { recursive: true });
|
|
100
|
-
browser = await chromium.launch({
|
|
101
|
-
headless: true,
|
|
102
|
-
args: [`--log-file=${join(root, '.tooling/a11y-chromium.log')}`],
|
|
103
|
-
...(settings.executablePath || process.env.PLAYWRIGHT_CHROMIUM_PATH
|
|
104
|
-
? { executablePath: settings.executablePath ?? process.env.PLAYWRIGHT_CHROMIUM_PATH }
|
|
105
|
-
: {}),
|
|
106
|
-
});
|
|
107
|
-
for (const viewport of settings.viewports ?? [{ name: 'desktop', width: 1280, height: 720 }]) {
|
|
108
|
-
const context = await browser.newContext({
|
|
109
|
-
viewport: { width: viewport.width, height: viewport.height },
|
|
110
|
-
...(settings.storageState ? { storageState: inside(root, settings.storageState) } : {}),
|
|
111
|
-
});
|
|
112
|
-
context.setDefaultTimeout?.(settings.timeout ?? 30000);
|
|
113
|
-
try {
|
|
114
|
-
for (const route of settings.routes) {
|
|
115
|
-
const url = new URL(route.path, base);
|
|
116
|
-
if (url.origin !== base.origin)
|
|
117
|
-
throw new Error('Accessibility routes must stay on baseURL origin');
|
|
118
|
-
const page = await context.newPage();
|
|
119
|
-
try {
|
|
120
|
-
const response = await page.goto(url.href, {
|
|
121
|
-
waitUntil: 'domcontentloaded',
|
|
122
|
-
timeout: settings.timeout ?? 30000,
|
|
123
|
-
});
|
|
124
|
-
if (response && !response.ok())
|
|
125
|
-
throw new Error(`Navigation returned HTTP ${response.status()}`);
|
|
126
|
-
if (new URL(page.url()).origin !== base.origin)
|
|
127
|
-
throw new Error(
|
|
128
|
-
'Accessibility route redirected outside baseURL origin; configure authentication state',
|
|
129
|
-
);
|
|
130
|
-
if (route.waitFor)
|
|
131
|
-
await page.locator(route.waitFor).waitFor({ timeout: settings.timeout ?? 30000 });
|
|
132
|
-
for (const action of route.actions ?? []) {
|
|
133
|
-
const locator = page.locator(action.selector);
|
|
134
|
-
if (action.type === 'click') await locator.click();
|
|
135
|
-
else if (action.type === 'fill') await locator.fill(action.value);
|
|
136
|
-
else if (action.type === 'press') await locator.press(action.value);
|
|
137
|
-
}
|
|
138
|
-
let axe = new AxeBuilder({ page });
|
|
139
|
-
axe = axe.withTags(settings.tags ?? ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa']);
|
|
140
|
-
for (const selector of route.include ?? []) axe = axe.include(selector);
|
|
141
|
-
for (const selector of route.exclude ?? []) axe = axe.exclude(selector);
|
|
142
|
-
const result = await axe.analyze();
|
|
143
|
-
pages.push({
|
|
144
|
-
route: route.name ?? route.path,
|
|
145
|
-
viewport: viewport.name,
|
|
146
|
-
violations: result.violations.map((violation) => ({
|
|
147
|
-
id: violation.id,
|
|
148
|
-
impact: violation.impact,
|
|
149
|
-
help: violation.help,
|
|
150
|
-
helpUrl: violation.helpUrl,
|
|
151
|
-
targets: violation.nodes.flatMap((node) => node.target),
|
|
152
|
-
failing: impacts.includes(violation.impact),
|
|
153
|
-
})),
|
|
154
|
-
incomplete: result.incomplete.map((rule) => ({ id: rule.id, help: rule.help })),
|
|
155
|
-
passedRules: result.passes.length,
|
|
156
|
-
});
|
|
157
|
-
} catch (error) {
|
|
158
|
-
errors.push({
|
|
159
|
-
route: route.name ?? route.path,
|
|
160
|
-
viewport: viewport.name,
|
|
161
|
-
message: error.message,
|
|
162
|
-
});
|
|
163
|
-
} finally {
|
|
164
|
-
await page.close();
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
} finally {
|
|
168
|
-
await context.close();
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
} finally {
|
|
172
|
-
try {
|
|
173
|
-
if (browser) await browser.close();
|
|
174
|
-
} finally {
|
|
175
|
-
if (stop) await stop();
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
const failures = pages.reduce(
|
|
179
|
-
(sum, page) => sum + page.violations.filter((violation) => violation.failing).length,
|
|
180
|
-
0,
|
|
181
|
-
);
|
|
182
|
-
return { pages, errors, failures, complete: !errors.length && failures === 0 };
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
186
|
-
const root = rootDirectory(),
|
|
187
|
-
{ config } = loadConfig(root);
|
|
188
|
-
const { chromium } = peer('@playwright/test');
|
|
189
|
-
const axe = peer('@axe-core/playwright');
|
|
190
|
-
const result = await scanAccessibility(config.a11y, {
|
|
191
|
-
root,
|
|
192
|
-
chromium,
|
|
193
|
-
AxeBuilder: axe.default ?? axe.AxeBuilder,
|
|
194
|
-
});
|
|
195
|
-
writeDetails({ a11y: result });
|
|
196
|
-
for (const page of result.pages)
|
|
197
|
-
for (const violation of page.violations.filter((violation) => violation.failing))
|
|
198
|
-
console.error(
|
|
199
|
-
`${page.route} [${page.viewport}] ${violation.impact}: ${violation.help} (${violation.id})`,
|
|
200
|
-
);
|
|
201
|
-
for (const error of result.errors) console.error(`${error.route}: ${error.message}`);
|
|
202
|
-
console.log(
|
|
203
|
-
`Accessibility: ${result.pages.length} rendered pages, ${result.failures} violations, ${result.errors.length} scan errors`,
|
|
204
|
-
);
|
|
205
|
-
process.exitCode = result.complete ? 0 : 1;
|
|
206
|
-
}
|
|
1
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
2
|
+
import { mkdirSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
import { executable } from '../runner/execute.mjs';
|
|
7
|
+
import { inside, loadConfig } from '../runner/config.mjs';
|
|
8
|
+
import { peer, rootDirectory, writeDetails } from './shared.mjs';
|
|
9
|
+
|
|
10
|
+
export async function startServer(root, settings) {
|
|
11
|
+
const cwd = inside(root, settings.cwd ?? '.');
|
|
12
|
+
const command = executable(cwd, settings.command[0]);
|
|
13
|
+
if (!command) throw new Error(`Missing server executable ${settings.command[0]}`);
|
|
14
|
+
const child = spawn(command[0], [...command.slice(1), ...settings.command.slice(1)], {
|
|
15
|
+
cwd,
|
|
16
|
+
env: process.env,
|
|
17
|
+
stdio: ['ignore', 'ignore', 'pipe'],
|
|
18
|
+
windowsHide: true,
|
|
19
|
+
detached: process.platform !== 'win32',
|
|
20
|
+
});
|
|
21
|
+
let failure,
|
|
22
|
+
output = '';
|
|
23
|
+
child.once('error', (error) => {
|
|
24
|
+
failure = error;
|
|
25
|
+
});
|
|
26
|
+
child.stderr.on('data', (chunk) => {
|
|
27
|
+
output = (output + chunk).slice(-4000);
|
|
28
|
+
});
|
|
29
|
+
const stop = async () => {
|
|
30
|
+
if (!child.pid) return;
|
|
31
|
+
if (process.platform === 'win32') {
|
|
32
|
+
if (child.exitCode === null)
|
|
33
|
+
spawnSync('taskkill', ['/pid', String(child.pid), '/T', '/F'], {
|
|
34
|
+
stdio: 'ignore',
|
|
35
|
+
windowsHide: true,
|
|
36
|
+
timeout: 5000,
|
|
37
|
+
});
|
|
38
|
+
} else {
|
|
39
|
+
try {
|
|
40
|
+
process.kill(-child.pid, 'SIGTERM');
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (error.code !== 'ESRCH') throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (child.exitCode === null) {
|
|
46
|
+
await Promise.race([
|
|
47
|
+
new Promise((resolve) => child.once('exit', resolve)),
|
|
48
|
+
delay(1000, undefined, { ref: false }),
|
|
49
|
+
]);
|
|
50
|
+
if (child.exitCode === null && process.platform !== 'win32') {
|
|
51
|
+
try {
|
|
52
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error.code !== 'ESRCH') throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
try {
|
|
60
|
+
const deadline = Date.now() + (settings.timeout ?? 30000);
|
|
61
|
+
while (Date.now() < deadline) {
|
|
62
|
+
if (failure || child.exitCode !== null)
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Accessibility server did not stay running: ${failure?.message ?? (output || child.exitCode)}`,
|
|
65
|
+
);
|
|
66
|
+
try {
|
|
67
|
+
const response = await fetch(settings.url, { signal: AbortSignal.timeout(1000) });
|
|
68
|
+
if (response.ok) return stop;
|
|
69
|
+
} catch {}
|
|
70
|
+
await delay(100);
|
|
71
|
+
}
|
|
72
|
+
throw new Error(
|
|
73
|
+
`Accessibility server did not become ready within ${settings.timeout ?? 30000} ms`,
|
|
74
|
+
);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
await stop();
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function scanAccessibility(
|
|
82
|
+
settings,
|
|
83
|
+
{ chromium, AxeBuilder, root = process.cwd(), server = startServer } = {},
|
|
84
|
+
) {
|
|
85
|
+
if (!settings?.routes?.length) throw new Error('Configure a11y.baseURL and at least one route');
|
|
86
|
+
const base = new URL(settings.baseURL);
|
|
87
|
+
if (!['http:', 'https:'].includes(base.protocol))
|
|
88
|
+
throw new Error('Accessibility baseURL must use HTTP or HTTPS');
|
|
89
|
+
const impacts = settings.failImpacts ?? ['minor', 'moderate', 'serious', 'critical'];
|
|
90
|
+
let stop, browser;
|
|
91
|
+
const pages = [],
|
|
92
|
+
errors = [];
|
|
93
|
+
try {
|
|
94
|
+
if (settings.server)
|
|
95
|
+
stop = await server(root, {
|
|
96
|
+
...settings.server,
|
|
97
|
+
url: settings.server.url ?? settings.baseURL,
|
|
98
|
+
});
|
|
99
|
+
mkdirSync(join(root, '.tooling'), { recursive: true });
|
|
100
|
+
browser = await chromium.launch({
|
|
101
|
+
headless: true,
|
|
102
|
+
args: [`--log-file=${join(root, '.tooling/a11y-chromium.log')}`],
|
|
103
|
+
...(settings.executablePath || process.env.PLAYWRIGHT_CHROMIUM_PATH
|
|
104
|
+
? { executablePath: settings.executablePath ?? process.env.PLAYWRIGHT_CHROMIUM_PATH }
|
|
105
|
+
: {}),
|
|
106
|
+
});
|
|
107
|
+
for (const viewport of settings.viewports ?? [{ name: 'desktop', width: 1280, height: 720 }]) {
|
|
108
|
+
const context = await browser.newContext({
|
|
109
|
+
viewport: { width: viewport.width, height: viewport.height },
|
|
110
|
+
...(settings.storageState ? { storageState: inside(root, settings.storageState) } : {}),
|
|
111
|
+
});
|
|
112
|
+
context.setDefaultTimeout?.(settings.timeout ?? 30000);
|
|
113
|
+
try {
|
|
114
|
+
for (const route of settings.routes) {
|
|
115
|
+
const url = new URL(route.path, base);
|
|
116
|
+
if (url.origin !== base.origin)
|
|
117
|
+
throw new Error('Accessibility routes must stay on baseURL origin');
|
|
118
|
+
const page = await context.newPage();
|
|
119
|
+
try {
|
|
120
|
+
const response = await page.goto(url.href, {
|
|
121
|
+
waitUntil: 'domcontentloaded',
|
|
122
|
+
timeout: settings.timeout ?? 30000,
|
|
123
|
+
});
|
|
124
|
+
if (response && !response.ok())
|
|
125
|
+
throw new Error(`Navigation returned HTTP ${response.status()}`);
|
|
126
|
+
if (new URL(page.url()).origin !== base.origin)
|
|
127
|
+
throw new Error(
|
|
128
|
+
'Accessibility route redirected outside baseURL origin; configure authentication state',
|
|
129
|
+
);
|
|
130
|
+
if (route.waitFor)
|
|
131
|
+
await page.locator(route.waitFor).waitFor({ timeout: settings.timeout ?? 30000 });
|
|
132
|
+
for (const action of route.actions ?? []) {
|
|
133
|
+
const locator = page.locator(action.selector);
|
|
134
|
+
if (action.type === 'click') await locator.click();
|
|
135
|
+
else if (action.type === 'fill') await locator.fill(action.value);
|
|
136
|
+
else if (action.type === 'press') await locator.press(action.value);
|
|
137
|
+
}
|
|
138
|
+
let axe = new AxeBuilder({ page });
|
|
139
|
+
axe = axe.withTags(settings.tags ?? ['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa']);
|
|
140
|
+
for (const selector of route.include ?? []) axe = axe.include(selector);
|
|
141
|
+
for (const selector of route.exclude ?? []) axe = axe.exclude(selector);
|
|
142
|
+
const result = await axe.analyze();
|
|
143
|
+
pages.push({
|
|
144
|
+
route: route.name ?? route.path,
|
|
145
|
+
viewport: viewport.name,
|
|
146
|
+
violations: result.violations.map((violation) => ({
|
|
147
|
+
id: violation.id,
|
|
148
|
+
impact: violation.impact,
|
|
149
|
+
help: violation.help,
|
|
150
|
+
helpUrl: violation.helpUrl,
|
|
151
|
+
targets: violation.nodes.flatMap((node) => node.target),
|
|
152
|
+
failing: impacts.includes(violation.impact),
|
|
153
|
+
})),
|
|
154
|
+
incomplete: result.incomplete.map((rule) => ({ id: rule.id, help: rule.help })),
|
|
155
|
+
passedRules: result.passes.length,
|
|
156
|
+
});
|
|
157
|
+
} catch (error) {
|
|
158
|
+
errors.push({
|
|
159
|
+
route: route.name ?? route.path,
|
|
160
|
+
viewport: viewport.name,
|
|
161
|
+
message: error.message,
|
|
162
|
+
});
|
|
163
|
+
} finally {
|
|
164
|
+
await page.close();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
} finally {
|
|
168
|
+
await context.close();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} finally {
|
|
172
|
+
try {
|
|
173
|
+
if (browser) await browser.close();
|
|
174
|
+
} finally {
|
|
175
|
+
if (stop) await stop();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const failures = pages.reduce(
|
|
179
|
+
(sum, page) => sum + page.violations.filter((violation) => violation.failing).length,
|
|
180
|
+
0,
|
|
181
|
+
);
|
|
182
|
+
return { pages, errors, failures, complete: !errors.length && failures === 0 };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
186
|
+
const root = rootDirectory(),
|
|
187
|
+
{ config } = loadConfig(root);
|
|
188
|
+
const { chromium } = peer('@playwright/test');
|
|
189
|
+
const axe = peer('@axe-core/playwright');
|
|
190
|
+
const result = await scanAccessibility(config.a11y, {
|
|
191
|
+
root,
|
|
192
|
+
chromium,
|
|
193
|
+
AxeBuilder: axe.default ?? axe.AxeBuilder,
|
|
194
|
+
});
|
|
195
|
+
writeDetails({ a11y: result });
|
|
196
|
+
for (const page of result.pages)
|
|
197
|
+
for (const violation of page.violations.filter((violation) => violation.failing))
|
|
198
|
+
console.error(
|
|
199
|
+
`${page.route} [${page.viewport}] ${violation.impact}: ${violation.help} (${violation.id})`,
|
|
200
|
+
);
|
|
201
|
+
for (const error of result.errors) console.error(`${error.route}: ${error.message}`);
|
|
202
|
+
console.log(
|
|
203
|
+
`Accessibility: ${result.pages.length} rendered pages, ${result.failures} violations, ${result.errors.length} scan errors`,
|
|
204
|
+
);
|
|
205
|
+
process.exitCode = result.complete ? 0 : 1;
|
|
206
|
+
}
|