Skip to content

Commit 647d3e1

Browse files
panvanovemberborn
andauthored
Check for --config file extensions after they fail to load, allowing custom loaders (#3135)
Co-authored-by: Mark Wubben <mark@novemberborn.net>
1 parent 9206928 commit 647d3e1

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

lib/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ export default async function loadCli() { // eslint-disable-line complexity
250250
setChalk(chalkOptions);
251251

252252
if (confError) {
253-
if (confError.parent) {
254-
exit(`${confError.message}\n\n${chalk.gray((confError.parent && confError.parent.stack) || confError.parent)}`);
253+
if (confError.cause) {
254+
exit(`${confError.message}\n\n${chalk.gray(confError.cause?.stack ?? confError.cause)}`);
255255
} else {
256256
exit(confError.message);
257257
}

lib/load-config.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ const loadConfigFile = async ({projectDir, configFile}) => {
2929
return null;
3030
}
3131

32-
throw Object.assign(new Error(`Error loading ${fileForErrorMessage}: ${error.message}`), {parent: error});
32+
throw Object.assign(new Error(`Error loading ${fileForErrorMessage}: ${error.message}`), {cause: error});
3333
}
3434
};
3535

3636
function resolveConfigFile(configFile) {
3737
if (configFile) {
3838
configFile = path.resolve(configFile); // Relative to CWD
39-
40-
if (!configFile.endsWith('.js') && !configFile.endsWith('.cjs') && !configFile.endsWith('.mjs')) {
41-
throw new Error('Config files must have .js, .cjs or .mjs extensions');
42-
}
4339
}
4440

4541
return configFile;
@@ -78,7 +74,7 @@ async function checkJsonFile(searchDir) {
7874
}
7975
}
8076

81-
export async function loadConfig({configFile, resolveFrom = process.cwd(), defaults = {}} = {}) {
77+
export async function loadConfig({configFile, resolveFrom = process.cwd(), defaults = {}} = {}) { // eslint-disable-line complexity
8278
let packageConf = await packageConfig('ava', {cwd: resolveFrom});
8379
const filepath = packageJsonPath(packageConf);
8480
const projectDir = filepath === undefined ? resolveFrom : path.dirname(filepath);
@@ -94,7 +90,17 @@ export async function loadConfig({configFile, resolveFrom = process.cwd(), defau
9490
let fileForErrorMessage;
9591
let conflicting = [];
9692
if (configFile) {
97-
const loaded = await loadConfigFile({projectDir, configFile});
93+
let loaded;
94+
try {
95+
loaded = await loadConfigFile({projectDir, configFile});
96+
} catch (error) {
97+
if (!configFile.endsWith('.js') && !configFile.endsWith('.cjs') && !configFile.endsWith('.mjs')) {
98+
throw Object.assign(new Error('Could not load config file; it should have .js, .cjs or .mjs extension'), {cause: error});
99+
}
100+
101+
throw error;
102+
}
103+
98104
if (loaded !== null) {
99105
({config: fileConf, fileForErrorMessage} = loaded);
100106
}

test/config/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ test.serial('receives a `projectDir` property', (...args) => ok('package-only')(
110110
});
111111

112112
test.serial('rethrows wrapped module errors', notOk('throws'), (t, error) => {
113-
t.is(error.parent.message, 'foo');
113+
t.is(error.cause.message, 'foo');
114114
});
115115

116116
test.serial('throws an error if a .js config file has no default export', notOk('no-default-export'));

test/config/snapshots/loader.js.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Generated by [AVA](https://avajs.dev).
1414

1515
> error message
1616
17-
'Config files must have .js, .cjs or .mjs extensions'
17+
'Could not load config file; it should have .js, .cjs or .mjs extension'
1818

1919
## throws an error if a config factory does not return a plain object
2020

test/config/snapshots/loader.js.snap

8 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy