UNPKG

@qualweb/cli

Version:
157 lines 6.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EvaluateAction = EvaluateAction; const node_fs_1 = require("node:fs"); const commander_1 = require("commander"); const types_1 = require("./types"); const core_1 = require("@qualweb/core"); const act_rules_1 = require("@qualweb/act-rules"); const WcagTechniquesModule_1 = require("@qualweb/wcag-techniques/WcagTechniquesModule"); const best_practices_1 = require("@qualweb/best-practices"); const earl_reporter_1 = require("@qualweb/earl-reporter"); const counter_1 = require("@qualweb/counter"); class QualwebOptionsBuilder { opts; qualwebOptions; modulesToRun; constructor(opts) { this.opts = opts; this.qualwebOptions = { modules: [], }; this.modulesToRun = new Set(this.opts.module); } validateActRuleOptions() { let hasValidationErrors = false; if (this.modulesToRun.has(types_1.ModuleOptionsEnum.ACTRules) || this.opts.actLevels || this.opts.actPrinciples || this.opts.excludeAct || this.opts.actRules) { if (this.opts.actRules && this.opts.actRules.error.length > 0) { console.error(`Unknown ACT rules in inclusion list: ${this.opts.actRules.error.join(', ')}`); hasValidationErrors = true; } if (this.opts.excludeAct && this.opts.excludeAct.error.length > 0) { console.error(`Unknown ACT rules in exclusion list: ${this.opts.excludeAct.error.join(', ')}`); hasValidationErrors = true; } this.qualwebOptions.modules.push(new act_rules_1.ACTRules({ include: this.opts.actRules?.ok, exclude: this.opts.excludeAct?.ok, levels: this.opts.actLevels, })); } return hasValidationErrors; } validateWcagTechniqueOptions() { let hasValidationErrors = false; if (this.modulesToRun.has(types_1.ModuleOptionsEnum.WCAGTechniques) || this.opts.wcagLevels || this.opts.wcagPrinciples || this.opts.wcagTechniques || this.opts.excludeWcag) { if (this.opts.wcagTechniques && this.opts.wcagTechniques.error.length > 0) { console.error(`Unknown WCAG techniques in inclusion list: ${this.opts.wcagTechniques.error.join(', ')}`); hasValidationErrors = true; } if (this.opts.excludeWcag && this.opts.excludeWcag.error.length > 0) { console.error(`Unknown WCAG techniques in exclusion list: ${this.opts.excludeWcag.error.join(', ')}`); hasValidationErrors = true; } this.qualwebOptions.modules.push(new WcagTechniquesModule_1.WCAGTechniquesModule({ include: this.opts.actRules?.ok, exclude: this.opts.excludeAct?.ok, levels: this.opts.actLevels, })); } return hasValidationErrors; } validateBestPracticeOptions() { let hasValidationErrors = false; if (this.modulesToRun.has(types_1.ModuleOptionsEnum.BestPractices) || this.opts.bestPractices || this.opts.excludeBp) { if (this.opts.bestPractices && this.opts.bestPractices.error.length > 0) { console.error(`Unknown best practices in inclusion list: ${this.opts.bestPractices.error.join(', ')}`); hasValidationErrors = true; } if (this.opts.excludeWcag && this.opts.excludeWcag.error.length > 0) { console.error(`Unknown best practices in exclusion list: ${this.opts.excludeWcag.error.join(', ')}`); hasValidationErrors = true; } this.qualwebOptions.modules.push(new best_practices_1.BestPractices({ include: this.opts.actRules?.ok, exclude: this.opts.excludeAct?.ok, })); } return hasValidationErrors; } validateCounterOptions() { if (this.modulesToRun.has(types_1.ModuleOptionsEnum.Counter)) { this.qualwebOptions.modules.push(new counter_1.Counter()); } return false; } validateViewportOptions() { if (this.opts.viewportResolution || this.opts.mobile || this.opts.orientation || this.opts.userAgent) { this.qualwebOptions.viewport = { mobile: this.opts.mobile, landscape: this.opts.orientation === 'landscape', userAgent: this.opts.userAgent, resolution: this.opts.viewportResolution, }; } } validatePuppeteerOptions() { this.qualwebOptions.maxParallelEvaluations = this.opts.maxParallelEvaluations; this.qualwebOptions.waitUntil = this.opts.waitUntil; this.qualwebOptions.timeout = this.opts.timeout; } validateInputOptions() { this.qualwebOptions.url = this.opts.url; this.qualwebOptions.file = this.opts.file; } parse() { this.validateViewportOptions(); this.validatePuppeteerOptions(); this.validateInputOptions(); const hasValidationErrors = this.validateActRuleOptions() || this.validateWcagTechniqueOptions() || this.validateBestPracticeOptions() || this.validateCounterOptions(); if (hasValidationErrors) { throw new Error('One or more inputs were invalid. Please correct them and try again.'); } if (this.modulesToRun.size === 0) { this.qualwebOptions.modules.push(new act_rules_1.ACTRules(), new best_practices_1.BestPractices(), new WcagTechniquesModule_1.WCAGTechniquesModule(), new counter_1.Counter()); } } getQualwebOptions() { return this.qualwebOptions; } } async function EvaluateAction() { const opts = this.opts(); const optionsParser = new QualwebOptionsBuilder(opts); try { optionsParser.parse(); } catch { console.error('One or more inputs were invalid. Please correct them and try again.'); return; } const qwOptions = optionsParser.getQualwebOptions(); const qw = new core_1.QualWeb(); await qw.start(); const reports = await qw.evaluate(qwOptions); await qw.stop(); let outputString; switch (opts.format) { case 'json': outputString = JSON.stringify(reports, null, 2); break; case 'earl': outputString = JSON.stringify((0, earl_reporter_1.generateEARLReport)(reports), null, 2); break; default: throw new commander_1.InvalidOptionArgumentError(`Invalid output format: ${opts.format}`); } if (opts.outFile) { await node_fs_1.promises.writeFile(opts.outFile, outputString, 'utf-8'); } else { console.info(outputString); } } //# sourceMappingURL=evaluateAction.js.map