Skip to content

Commit

Permalink
fix: nicer error message on bad config file
Browse files Browse the repository at this point in the history
  • Loading branch information
LironEr committed Jul 1, 2023
1 parent 67acc31 commit 5acbaa4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/bundlemon/src/cli/__tests__/assets/bad-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {;
3 changes: 3 additions & 0 deletions packages/bundlemon/src/cli/__tests__/assets/bad-format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"files": [
}
3 changes: 3 additions & 0 deletions packages/bundlemon/src/cli/__tests__/assets/bad-format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files:
- 'a.js'
- 'b.js
Empty file.
39 changes: 39 additions & 0 deletions packages/bundlemon/src/cli/__tests__/configFile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { loadConfigFile } from '../configFile';

describe('load config file', () => {
beforeEach(() => {
jest.resetAllMocks();
});

describe('failure', () => {
test('empty', async () => {
const config = await loadConfigFile('src/cli/__tests__/assets/empty.json');

expect(config).toBeUndefined();
});

test('bad format JSON', async () => {
const config = await loadConfigFile('src/cli/__tests__/assets/bad-format.json');

expect(config).toBeUndefined();
});

test('bad format YAML', async () => {
const config = await loadConfigFile('src/cli/__tests__/assets/bad-format.yaml');

expect(config).toBeUndefined();
});

test('bad format JS', async () => {
const config = await loadConfigFile('src/cli/__tests__/assets/bad-format.js');

expect(config).toBeUndefined();
});

test('not found', async () => {
const config = await loadConfigFile('not-found');

expect(config).toBeUndefined();
});
});
});
17 changes: 11 additions & 6 deletions packages/bundlemon/src/cli/configFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ export async function loadConfigFile(configPath?: string): Promise<Config | unde
logger.debug(`Load config file from "${configPath}"`);
}

const cosmiconfigResult = await (configPath ? explorer.load(configPath) : explorer.search());
try {
const cosmiconfigResult = await (configPath ? explorer.load(configPath) : explorer.search());

if (!cosmiconfigResult || cosmiconfigResult.isEmpty) {
return undefined;
}
if (!cosmiconfigResult || cosmiconfigResult.isEmpty) {
return undefined;
}

logger.debug(`Config file loaded from "${cosmiconfigResult.filepath}"`);
logger.debug(`Config file loaded from "${cosmiconfigResult.filepath}"`);

return cosmiconfigResult.config;
return cosmiconfigResult.config;
} catch (e) {
logger.error(`Error loading config file: ${e}`);
return undefined;
}
}

0 comments on commit 5acbaa4

Please sign in to comment.