Skip to content

Commit

Permalink
Remove json string as --config value support
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Feb 14, 2021
1 parent cb14e3c commit a1c1b7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 36 deletions.
40 changes: 15 additions & 25 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,23 @@ function action(args, opts) {

// --config
if (opts.config) {
// string
if (opts.config.charAt(0) === '{') {
try {
config = JSON.parse(opts.config);
} catch (e) {
return printErrorAndExit(`Error: Couldn't parse config JSON.\n${String(e)}`);
var configPath = PATH.resolve(opts.config),
configData;
try {
// require() adds some weird output on YML files
configData = FS.readFileSync(configPath, 'utf8');
config = JSON.parse(configData);
} catch (err) {
if (err.code === 'ENOENT') {
return printErrorAndExit(`Error: couldn't find config file '${opts.config}'.`);
} else if (err.code === 'EISDIR') {
return printErrorAndExit(`Error: directory '${opts.config}' is not a config file.`);
}
// external file
} else {
var configPath = PATH.resolve(opts.config),
configData;
try {
// require() adds some weird output on YML files
configData = FS.readFileSync(configPath, 'utf8');
config = JSON.parse(configData);
} catch (err) {
if (err.code === 'ENOENT') {
return printErrorAndExit(`Error: couldn't find config file '${opts.config}'.`);
} else if (err.code === 'EISDIR') {
return printErrorAndExit(`Error: directory '${opts.config}' is not a config file.`);
}
config = YAML.safeLoad(configData);
config.__DIR = PATH.dirname(configPath); // will use it to resolve custom plugins defined via path
config = YAML.safeLoad(configData);
config.__DIR = PATH.dirname(configPath); // will use it to resolve custom plugins defined via path

if (!config || Array.isArray(config)) {
return printErrorAndExit(`Error: invalid config file '${opts.config}'.`);
}
if (!config || Array.isArray(config)) {
return printErrorAndExit(`Error: invalid config file '${opts.config}'.`);
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions test/coa/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,6 @@ describe('coa', function() {
), 0);
}

it('should throw an error if "config" can not be parsed', function(done) {
replaceConsoleError();

runProgram(['--input', svgPath, '--config', '{']).then(onComplete, onComplete);

function onComplete() {
restoreConsoleError();
done(/Error: Couldn't parse config JSON/.test(output) ? null : 'Error was not thrown');
}
});

it('should work properly with string input', function(done) {
runProgram(['--string', fs.readFileSync(svgPath, 'utf8'), '--output', 'temp.svg', '--quiet']).then(function() {
done();
Expand Down

0 comments on commit a1c1b7f

Please sign in to comment.