Skip to content

Commit a855b40

Browse files
committed
Fail when specified config is wrong or json is specified
1 parent aa8e0bd commit a855b40

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

lib/svgo-node.js

-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ const {
99
} = require('./svgo.js');
1010

1111
const importConfig = async configFile => {
12-
try {
13-
await fs.promises.access(configFile);
14-
} catch {
15-
return null;
16-
}
1712
const config = require(configFile);
1813
if (config == null || typeof config !== 'object' || Array.isArray(config)) {
1914
throw Error(`Invalid config file "${configFile}"`);

lib/svgo/coa.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = function makeProgram(program) {
3434
.option('-f, --folder <FOLDER>', 'Input folder, optimize and rewrite all *.svg files')
3535
.option('-o, --output <OUTPUT...>', 'Output file or folder (by default the same as the input), "-" for STDOUT')
3636
.option('-p, --precision <INTEGER>', 'Set number of digits in the fractional part, overrides plugins params')
37-
.option('--config <CONFIG>', 'Config file or JSON string to extend or replace default')
37+
.option('--config <CONFIG>', 'Custom config file, only .js is supported')
3838
.option('--datauri <FORMAT>', 'Output as Data URI string (base64), URI encoded (enc) or unencoded (unenc)')
3939
.option('--multipass', 'Pass over SVGs multiple times to ensure all optimizations are applied')
4040
.option('--pretty', 'Make SVG pretty printed')

test/config/_index.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,20 @@ describe('config', function() {
206206
);
207207
expect(config).to.deep.equal({ plugins: [] });
208208
});
209-
it('gives null module does not exist', async () => {
210-
const absoluteConfig = await loadConfig(
211-
path.join(process.cwd(), './test/config/fixtures/config.js'),
212-
);
213-
expect(absoluteConfig).to.equal(null);
214-
const searchedConfig = await loadConfig(
209+
it('gives null when config is not found', async () => {
210+
const config = await loadConfig(
215211
null,
216212
path.join(process.cwd(), './test/config'),
217213
);
218-
expect(searchedConfig).to.equal(null);
214+
expect(config).to.equal(null);
215+
});
216+
it('is failed when specified config does not exist', async () => {
217+
try {
218+
await loadConfig("{}");
219+
expect.fail('Config is loaded successfully');
220+
} catch (error) {
221+
expect(error.message).to.match(/Cannot find module/);
222+
}
219223
});
220224
it('is failed to load when module exports not an object', async () => {
221225
try {

0 commit comments

Comments
 (0)