a11yanalyze
Version:
A command-line tool for developers and QA engineers to test web pages and websites for WCAG 2.2 AA accessibility compliance
95 lines • 4.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StorybookBatchRunner = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const path_1 = require("path");
const fs_1 = require("fs");
const page_scanner_1 = require("../scanner/page-scanner");
const vpat_reporter_1 = require("../output/vpat-reporter");
class StorybookBatchRunner {
async run(options) {
// 1. Discover stories
const stories = await this.discoverStories(options.storybookUrl);
if (!stories.length) {
throw new Error('No stories found in Storybook instance.');
}
// 2. Ensure output directory exists
if (!(0, fs_1.existsSync)(options.outputDir)) {
(0, fs_1.mkdirSync)(options.outputDir, { recursive: true });
}
// 3. Scan each story and generate reports
const summary = [];
for (const story of stories) {
const scanResult = await this.scanStory(story.url, options);
const vpatReport = vpat_reporter_1.VpatReporter.generateJsonReport(story.name, scanResult);
const baseName = story.name.replace(/[^a-zA-Z0-9_-]/g, '_');
if (options.format === 'json' || options.format === 'both') {
const jsonPath = (0, path_1.join)(options.outputDir, `${baseName}.vpat.json`);
(0, fs_1.writeFileSync)(jsonPath, JSON.stringify(vpatReport, null, 2), 'utf8');
}
if (options.format === 'markdown' || options.format === 'both') {
const mdPath = (0, path_1.join)(options.outputDir, `${baseName}.vpat.md`);
const md = vpat_reporter_1.VpatReporter.generateMarkdownReport(vpatReport);
(0, fs_1.writeFileSync)(mdPath, md, 'utf8');
}
summary.push({
name: story.name,
url: story.url,
supports: vpatReport.summary.supports,
partiallySupports: vpatReport.summary.partiallySupports,
doesNotSupport: vpatReport.summary.doesNotSupport,
notApplicable: vpatReport.summary.notApplicable,
notEvaluated: vpatReport.summary.notEvaluated,
});
}
// 4. Write summary index
(0, fs_1.writeFileSync)((0, path_1.join)(options.outputDir, 'vpat-index.json'), JSON.stringify(summary, null, 2), 'utf8');
}
async discoverStories(storybookUrl) {
// Try to fetch /stories.json (Storybook 6+)
try {
const res = await (0, node_fetch_1.default)(`${storybookUrl.replace(/\/$/, '')}/stories.json`);
if (res.ok) {
const data = await res.json();
// Type guard for 'data' before accessing data.stories
if (typeof data === 'object' && data !== null && 'stories' in data) {
// Now safe to access data.stories
return Object.values(data.stories).map((story) => ({
name: story.name || story.id,
url: `${storybookUrl.replace(/\/$/, '')}/iframe.html?id=${story.id}`,
}));
}
}
}
catch (e) {
// Fallback to crawling sidebar or other methods in future
}
// If not found, return empty for now
return [];
}
async scanStory(url, options) {
// Use the PageScanner with minimal config for now
const pageScanner = new page_scanner_1.PageScanner({
headless: true,
viewport: { width: 1280, height: 720 },
timeout: options.timeout || 30000,
}, {
wcagLevel: options.wcagLevel || 'AA',
includeAAA: false,
includeARIA: true,
customRules: [],
disabledRules: [],
});
await pageScanner.initialize();
// Run the scan and get results
const scanResult = await pageScanner.scan(url);
// Use keyboardNavigation and screenReaderSimulation from scanResult
// (No need to call simulateKeyboardNavigation or simulateScreenReader here)
return scanResult;
}
}
exports.StorybookBatchRunner = StorybookBatchRunner;
//# sourceMappingURL=storybook-batch.js.map