Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 12 additions & 21 deletions bin/pa11y-ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,16 @@ Promise.resolve()
// file specified by the user. It checks for config
// files in the following order:
// - no extension (JSON)
// - cjs extension (JavaScript)
// - js extension (JavaScript)
// - json extension (JSON)
function loadConfig(configPath) {
return new Promise((resolve, reject) => {
configPath = resolveConfigPath(configPath);
let config;
try {
config = loadLocalConfigUnmodified(configPath);
if (!config) {
config = loadLocalConfigWithJs(configPath);
}
if (!config) {
config = loadLocalConfigWithJson(configPath);
}
config = loadLocalConfigUnmodified(configPath) || loadConfigModule(configPath);

if (options.config && !config) {
return reject(new Error(`The config file "${configPath}" could not be loaded`));
}
Expand All @@ -154,8 +150,9 @@ function resolveConfigPath(configPath) {
if (!path.isAbsolute(configPath)) {
configPath = path.join(process.cwd(), configPath);
}
if (/\.js(on)?$/.test(configPath)) {
configPath = configPath.replace(/\.js(on)?$/, '');
const extensionPattern = /\.(cjs)?(js)?(json)?$/;
if (extensionPattern.test(configPath)) {
configPath = configPath.replace(extensionPattern, '');
}
return configPath;
}
Expand All @@ -172,21 +169,15 @@ function loadLocalConfigUnmodified(configPath) {
}
}

// Load the config file but adding a .js extension
function loadLocalConfigWithJs(configPath) {
try {
return require(`${configPath}.js`);
} catch (error) {
if (error.code !== 'MODULE_NOT_FOUND') {
throw error;
}
}
function loadConfigModule(pathWithoutExtension) {
return loadConfigModuleWithExtension(pathWithoutExtension, 'cjs') ||
loadConfigModuleWithExtension(pathWithoutExtension, 'js') ||
loadConfigModuleWithExtension(pathWithoutExtension, 'json');
}

// Load the config file but adding a .json extension
function loadLocalConfigWithJson(configPath) {
function loadConfigModuleWithExtension(pathWithoutExtension, extension) {
try {
return require(`${configPath}.json`);
return require(`${pathWithoutExtension}.${extension}`);
} catch (error) {
if (error.code !== 'MODULE_NOT_FOUND') {
throw error;
Expand Down
30 changes: 30 additions & 0 deletions test/integration/cli-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ describe('pa11y-ci (with a config file that has a "json" extension)', () => {

});

describe('pa11y-ci (with a config file that has a "cjs" extension)', () => {

before(() => {
return global.cliCall([
'--config',
'extension-cjs'
]);
});

it('loads the expected config', () => {
assert.include(global.lastResult.output, 'http://localhost:8090/config-extension-cjs');
});

});

describe('pa11y-ci (with a config file that has a "js" extension)', () => {

before(() => {
Expand Down Expand Up @@ -105,6 +120,21 @@ describe('pa11y-ci (with a config file that has a specified JS extension)', () =

});

describe('pa11y-ci (with a config file that has a specified CJS extension)', () => {

before(() => {
return global.cliCall([
'--config',
'extension-cjs.cjs'
]);
});

it('loads the expected config', () => {
assert.include(global.lastResult.output, 'http://localhost:8090/config-extension-cjs');
});

});

describe('pa11y-ci (with a config file that has an absolute path)', () => {

before(() => {
Expand Down
7 changes: 7 additions & 0 deletions test/integration/mock/config/extension-cjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
urls: [
'http://localhost:8090/config-extension-cjs'
]
};
Loading