1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +03:00

Fix reporting of config errors (#1342)

Errors are swallowed while resolving.
This commit is contained in:
Bogdan Chadkin
2021-02-19 12:11:35 +03:00
committed by GitHub
parent 400c867c5c
commit 330e78b479
7 changed files with 62 additions and 49 deletions

View File

@ -10,19 +10,26 @@ const {
const importConfig = async configFile => {
try {
const config = require(configFile);
if (config == null || typeof config !== 'object' || Array.isArray(config)) {
throw Error(`Invalid config file "${configFile}"`);
}
return config;
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
return null;
}
throw error;
await fs.promises.access(configFile);
} catch {
return null;
}
const config = require(configFile);
if (config == null || typeof config !== 'object' || Array.isArray(config)) {
throw Error(`Invalid config file "${configFile}"`);
}
return config;
};
const isFile = async (file) => {
try {
const stats = await fs.promises.stat(file);
return stats.isFile();
} catch {
return false;
}
}
const loadConfig = async (configFile, cwd = process.cwd()) => {
if (configFile != null) {
if (path.isAbsolute(configFile)) {
@ -33,13 +40,10 @@ const loadConfig = async (configFile, cwd = process.cwd()) => {
}
let dir = cwd;
while (true) {
try {
const file = path.join(dir, "svgo.config.js");
const stats = await fs.promises.stat(file);
if (stats.isFile()) {
return await importConfig(file);
}
} catch {}
const file = path.join(dir, "svgo.config.js");
if (await isFile(file)) {
return await importConfig(file);
}
const parent = path.dirname(dir);
if (dir === parent) {
return null;