Skip to content

Commit

Permalink
Fixes eslint warnings (#101)
Browse files Browse the repository at this point in the history
* address some eslint warnings

* refactoring for eslint warnings

* Update lib/pa11y-ci.js

remove destructuring

Co-Authored-By: Joey Ciechanowicz <1148637+joeyciechanowicz@users.noreply.github.com>

* Update lib/pa11y-ci.js

remove destructuring

Co-Authored-By: Joey Ciechanowicz <1148637+joeyciechanowicz@users.noreply.github.com>

* Updates per PR comments

* Fixed typo from syntax change merge.

Co-authored-by: Joey Ciechanowicz <1148637+joeyciechanowicz@users.noreply.github.com>
  • Loading branch information
kkoskelin and joeyciechanowicz committed Feb 14, 2020
1 parent 71660fd commit 0e33861
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 44 deletions.
41 changes: 17 additions & 24 deletions bin/pa11y-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const path = require('path');
const globby = require('globby');
const protocolify = require('protocolify');
const pkg = require('../package.json');
const program = require('commander');
const commander = require('commander');


// Here we're using Commander to specify the CLI options
program
commander
.version(pkg.version)
.usage('[options] <paths>')
.option(
Expand Down Expand Up @@ -55,7 +55,7 @@ program
.parse(process.argv);

// Parse the args into valid paths using glob and protocolify
const urls = globby.sync(program.args, {
const urls = globby.sync(commander.args, {
// Ensure not-found paths (like "google.com"), are returned
nonull: true
}).map(protocolify);
Expand All @@ -64,12 +64,12 @@ const urls = globby.sync(program.args, {
Promise.resolve()
.then(() => {
// Load config based on the `--config` flag
return loadConfig(program.config);
return loadConfig(commander.config);
})
.then(config => {
// Load a sitemap based on the `--sitemap` flag
if (program.sitemap) {
return loadSitemapIntoConfig(program, config);
if (commander.sitemap) {
return loadSitemapIntoConfig(commander, config);
}
return config;
})
Expand All @@ -79,7 +79,7 @@ Promise.resolve()
})
.then(report => {
// Output JSON if asked for it
if (program.json) {
if (commander.json) {
console.log(JSON.stringify(report, (key, value) => {
if (value instanceof Error) {
return {
Expand All @@ -91,7 +91,7 @@ Promise.resolve()
}
// Decide on an exit code based on whether
// errors are below threshold or everything passes
if (report.errors >= parseInt(program.threshold, 10) && report.passes < report.total) {
if (report.errors >= parseInt(commander.threshold, 10) && report.passes < report.total) {
process.exit(2);
} else {
process.exit(0);
Expand Down Expand Up @@ -121,7 +121,7 @@ function loadConfig(configPath) {
if (!config) {
config = loadLocalConfigWithJson(configPath);
}
if (program.config && !config) {
if (commander.config && !config) {
return reject(new Error(`The config file "${configPath}" could not be loaded`));
}
} catch (error) {
Expand All @@ -131,8 +131,8 @@ function loadConfig(configPath) {
}

// Allow loaded configs to return a promise
Promise.resolve(config).then(config => {
resolve(defaultConfig(config || {}));
Promise.resolve(config).then(loadedConfig => {
resolve(defaultConfig(loadedConfig || {}));
});
});
}
Expand Down Expand Up @@ -193,16 +193,15 @@ function defaultConfig(config) {
config.defaults.log = config.defaults.log || console;
// Setting to undefined rather than 0 allows for a fallback to the default
config.defaults.wrapWidth = process.stdout.columns || undefined;
if (program.json) {
if (commander.json) {
delete config.defaults.log;
}
return config;
}

// Load a sitemap from a remote URL, parse out the
// URLs, and add them to an existing config object
function loadSitemapIntoConfig(program, config) {
const sitemapUrl = program.sitemap;
function loadSitemapIntoConfig(program, initialConfig) {
const sitemapFind = (
program.sitemapFind ?
new RegExp(program.sitemapFind, 'gi') :
Expand All @@ -217,16 +216,10 @@ function loadSitemapIntoConfig(program, config) {

function getUrlsFromSitemap(sitemapUrl, config) {
return Promise.resolve()
.then(() => {
return fetch(sitemapUrl);
})
.then(response => {
return response.text();
})
.then(() => fetch(sitemapUrl))
.then(response => response.text())
.then(body => {
const $ = cheerio.load(body, {
xmlMode: true
});
const $ = cheerio.load(body, {xmlMode: true});

const isSitemapIndex = $('sitemapindex').length > 0;
if (isSitemapIndex) {
Expand Down Expand Up @@ -258,5 +251,5 @@ function loadSitemapIntoConfig(program, config) {
});
}

return getUrlsFromSitemap(sitemapUrl, config);
return getUrlsFromSitemap(program.sitemap, initialConfig);
}
47 changes: 29 additions & 18 deletions lib/pa11y-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const puppeteer = require('puppeteer');
// Just an empty function to use as default
// configuration and arguments
/* istanbul ignore next */
// eslint-disable-next-line no-empty-function
const noop = () => {};

// Here's the exports. `pa11yCi` is defined further down the
Expand Down Expand Up @@ -69,6 +70,30 @@ function pa11yCi(urls, options) {
results: {}
};

function processResults(results, reportConfig, url) {
const withinThreshold = reportConfig.threshold ?
results.issues.length <= reportConfig.threshold :
false;

let message = ` ${chalk.cyan('>')} ${url} - `;
if (results.issues.length && !withinThreshold) {
message += chalk.red(`${results.issues.length} errors`);
log.error(message);
report.results[url] = results.issues;
report.errors += results.issues.length;
} else {
message += chalk.green(`${results.issues.length} errors`);
if (withinThreshold) {
message += chalk.green(
` (within threshold of ${reportConfig.threshold})`
);
}
log.info(message);
report.results[url] = [];
report.passes += 1;
}
}

// This is the actual test runner, which the queue will
// execute on each of the URLs
async function testRunner(config) {
Expand All @@ -91,23 +116,7 @@ function pa11yCi(urls, options) {
// results to the report object
try {
const results = await pa11y(url, config);
const withinThreshold = config.threshold ? results.issues.length <= config.threshold : false;

let message = ` ${chalk.cyan('>')} ${url} - `;
if (results.issues.length && !withinThreshold) {
message += chalk.red(`${results.issues.length} errors`);
log.error(message);
report.results[url] = results.issues;
report.errors += results.issues.length;
} else {
message += chalk.green(`${results.issues.length} errors`);
if (withinThreshold) {
message += chalk.green(` (within threshold of ${config.threshold})`);
}
log.info(message);
report.results[url] = [];
report.passes += 1;
}
processResults(results, config, url);
} catch (error) {
log.error(` ${chalk.cyan('>')} ${url} - ${chalk.red('Failed to run')}`);
report.results[url] = [error];
Expand Down Expand Up @@ -136,7 +145,9 @@ function pa11yCi(urls, options) {
if (result instanceof Error) {
log.error(`\n ${redBullet} Error: ${wrap(result.message).trim()}`);
} else {
const context = (result.context ? result.context.replace(/\s+/g, ' ') : '[no context]');
const context = result.context ?
result.context.replace(/\s+/g, ' ') :
'[no context]';
log.error([
'',
` ${redBullet} ${wrap(result.message).trim()}`,
Expand Down
2 changes: 0 additions & 2 deletions test/unit/lib/pa11y-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ describe('lib/pa11y-ci', () => {
});

describe('.defaults', () => {
let defaults;

beforeEach(() => {
defaults = pa11yCi.defaults;
});
Expand Down

0 comments on commit 0e33861

Please sign in to comment.