Skip to content

Commit

Permalink
Fix reporting of config errors
Browse files Browse the repository at this point in the history
Errors are swallowed while resolving.
  • Loading branch information
TrySound committed Feb 18, 2021
1 parent b65f699 commit 457702e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 31 deletions.
6 changes: 5 additions & 1 deletion bin/svgo
@@ -1,6 +1,10 @@
#!/usr/bin/env node

const { red } = require('chalk');
const { program } = require('commander');
const makeProgram = require('../lib/svgo/coa');
makeProgram(program);
program.parseAsync(process.argv);
program.parseAsync(process.argv).catch(error => {
console.error(red(error.stack));
process.exit(1);
});
12 changes: 5 additions & 7 deletions lib/svgo-node.js
Expand Up @@ -33,13 +33,11 @@ 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");
const stats = await fs.promises.stat(file);
if (stats.isFile()) {
return await importConfig(file);
}
const parent = path.dirname(dir);
if (dir === parent) {
return null;
Expand Down
31 changes: 8 additions & 23 deletions lib/svgo/coa.js
Expand Up @@ -9,7 +9,6 @@ const pluginsMap = require('../../plugins/plugins.js');
const PKG = require('../../package.json');
const { encodeSVGDatauri, decodeSVGDatauri } = require('./tools.js');
const regSVGFile = /\.svg$/i;
const noop = () => {};

/**
* Synchronously check if path is a directory. Tolerant to errors like ENOENT.
Expand Down Expand Up @@ -97,18 +96,14 @@ async function action(args, opts) {
if (typeof process == 'object' && process.versions && process.versions.node && PKG && PKG.engines.node) {
var nodeVersion = String(PKG.engines.node).match(/\d*(\.\d+)*/)[0];
if (parseFloat(process.versions.node) < parseFloat(nodeVersion)) {
return printErrorAndExit(`Error: ${PKG.name} requires Node.js version ${nodeVersion} or higher.`);
throw Error(`${PKG.name} requires Node.js version ${nodeVersion} or higher.`);
}
}

// --config
try {
const loadedConfig = await loadConfig(opts.config);
if (loadedConfig != null) {
config = loadedConfig;
}
} catch (error) {
return printErrorAndExit(error.message);
const loadedConfig = await loadConfig(opts.config);
if (loadedConfig != null) {
config = loadedConfig;
}

// --quiet
Expand Down Expand Up @@ -166,7 +161,7 @@ async function action(args, opts) {
// --folder
if (opts.folder) {
var ouputFolder = output && output[0] || opts.folder;
return optimizeFolder(config, opts.folder, ouputFolder).then(noop, printErrorAndExit);
await optimizeFolder(config, opts.folder, ouputFolder);
}

// --input
Expand All @@ -183,8 +178,9 @@ async function action(args, opts) {
});
// file
} else {
return Promise.all(input.map((file, n) => optimizeFile(config, file, output[n])))
.then(noop, printErrorAndExit);
await Promise.all(
input.map((file, n) => optimizeFile(config, file, output[n]))
);
}

// --string
Expand Down Expand Up @@ -390,15 +386,4 @@ function showAvailablePlugins() {
console.log('Currently available plugins:\n' + list);
}

/**
* Write an error and exit.
* @param {Error} error
* @return {Promise} a promise for running tests
*/
function printErrorAndExit(error) {
console.error(chalk.red(error));
process.exit(1);
return Promise.reject(error); // for tests
}

module.exports.checkIsDir = checkIsDir;

0 comments on commit 457702e

Please sign in to comment.