Skip to content

Commit 168579c

Browse files
Support config file with CJS extension (#296)
1 parent c7f186a commit 168579c

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

bin/pa11y-ci.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,16 @@ Promise.resolve()
116116
// file specified by the user. It checks for config
117117
// files in the following order:
118118
// - no extension (JSON)
119+
// - cjs extension (JavaScript)
119120
// - js extension (JavaScript)
120121
// - json extension (JSON)
121122
function loadConfig(configPath) {
122123
return new Promise((resolve, reject) => {
123124
configPath = resolveConfigPath(configPath);
124125
let config;
125126
try {
126-
config = loadLocalConfigUnmodified(configPath);
127-
if (!config) {
128-
config = loadLocalConfigWithJs(configPath);
129-
}
130-
if (!config) {
131-
config = loadLocalConfigWithJson(configPath);
132-
}
127+
config = loadLocalConfigUnmodified(configPath) || loadConfigModule(configPath);
128+
133129
if (options.config && !config) {
134130
return reject(new Error(`The config file "${configPath}" could not be loaded`));
135131
}
@@ -154,8 +150,9 @@ function resolveConfigPath(configPath) {
154150
if (!path.isAbsolute(configPath)) {
155151
configPath = path.join(process.cwd(), configPath);
156152
}
157-
if (/\.js(on)?$/.test(configPath)) {
158-
configPath = configPath.replace(/\.js(on)?$/, '');
153+
const extensionPattern = /\.(cjs)?(js)?(json)?$/;
154+
if (extensionPattern.test(configPath)) {
155+
configPath = configPath.replace(extensionPattern, '');
159156
}
160157
return configPath;
161158
}
@@ -172,21 +169,15 @@ function loadLocalConfigUnmodified(configPath) {
172169
}
173170
}
174171

175-
// Load the config file but adding a .js extension
176-
function loadLocalConfigWithJs(configPath) {
177-
try {
178-
return require(`${configPath}.js`);
179-
} catch (error) {
180-
if (error.code !== 'MODULE_NOT_FOUND') {
181-
throw error;
182-
}
183-
}
172+
function loadConfigModule(pathWithoutExtension) {
173+
return loadConfigModuleWithExtension(pathWithoutExtension, 'cjs') ||
174+
loadConfigModuleWithExtension(pathWithoutExtension, 'js') ||
175+
loadConfigModuleWithExtension(pathWithoutExtension, 'json');
184176
}
185177

186-
// Load the config file but adding a .json extension
187-
function loadLocalConfigWithJson(configPath) {
178+
function loadConfigModuleWithExtension(pathWithoutExtension, extension) {
188179
try {
189-
return require(`${configPath}.json`);
180+
return require(`${pathWithoutExtension}.${extension}`);
190181
} catch (error) {
191182
if (error.code !== 'MODULE_NOT_FOUND') {
192183
throw error;

test/integration/cli-config.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ describe('pa11y-ci (with a config file that has a "json" extension)', () => {
4646

4747
});
4848

49+
describe('pa11y-ci (with a config file that has a "cjs" extension)', () => {
50+
51+
before(() => {
52+
return global.cliCall([
53+
'--config',
54+
'extension-cjs'
55+
]);
56+
});
57+
58+
it('loads the expected config', () => {
59+
assert.include(global.lastResult.output, 'http://localhost:8090/config-extension-cjs');
60+
});
61+
62+
});
63+
4964
describe('pa11y-ci (with a config file that has a "js" extension)', () => {
5065

5166
before(() => {
@@ -106,6 +121,21 @@ describe('pa11y-ci (with a config file that has a specified JS extension)', () =
106121

107122
});
108123

124+
describe('pa11y-ci (with a config file that has a specified CJS extension)', () => {
125+
126+
before(() => {
127+
return global.cliCall([
128+
'--config',
129+
'extension-cjs.cjs'
130+
]);
131+
});
132+
133+
it('loads the expected config', () => {
134+
assert.include(global.lastResult.output, 'http://localhost:8090/config-extension-cjs');
135+
});
136+
137+
});
138+
109139
describe('pa11y-ci (with a config file that has an absolute path)', () => {
110140

111141
before(() => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
module.exports = {
4+
urls: [
5+
'http://localhost:8090/config-extension-cjs'
6+
]
7+
};

0 commit comments

Comments
 (0)