Skip to content

Commit 98d7dab

Browse files
committed
feat(exists): expose the ability to check if config exists
1 parent 829488e commit 98d7dab

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

src/existence-checker.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import loadConfig from './loader.js';
2+
3+
export default async function ({name}) {
4+
try {
5+
await loadConfig({name});
6+
7+
return true;
8+
} catch (e) {
9+
if ('ENOCONFIG' === e.code) return false;
10+
11+
throw e;
12+
}
13+
}

src/existence-checker.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {describe, it, expect, vi} from 'vitest';
2+
import {when} from 'jest-when';
3+
import any from '@travi/any';
4+
5+
import loadConfig from './loader.js';
6+
import exists from './existence-checker.js';
7+
8+
vi.mock('./loader.js');
9+
10+
describe('existence-checker', () => {
11+
const name = any.word();
12+
13+
it('should return `true` if a config file exists', async () => {
14+
when(loadConfig).calledWith({name}).mockResolvedValue(any.simpleObject());
15+
16+
expect(await exists({name})).toBe(true);
17+
});
18+
19+
it('should return `false` if no config exists', async () => {
20+
const error = new Error();
21+
error.code = 'ENOCONFIG';
22+
when(loadConfig).calledWith({name}).mockRejectedValue(error);
23+
24+
expect(await exists({name})).toBe(false);
25+
});
26+
27+
it('should throw an error if an unexpected error occurs', async () => {
28+
const error = new Error();
29+
error.code = any.word();
30+
when(loadConfig).calledWith({name}).mockRejectedValue(error);
31+
32+
await expect(exists({name})).rejects.toThrow(error);
33+
});
34+
});

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export {default as write} from './writer.js';
22
export {default as load} from './loader.js';
3+
export {default as exists} from './existence-checker.js';
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Feature: exists
2+
3+
Scenario: json config exists
4+
Given a "json" config file exists
5+
When checking for config existence
6+
Then the config is reported to be found
7+
8+
Scenario: yaml config exists
9+
Given a "yaml" config file exists
10+
When checking for config existence
11+
Then the config is reported to be found
12+
13+
Scenario: no config exists
14+
Given no config exists
15+
When checking for config existence
16+
Then the config is reported to not be found

test/integration/features/step_definitions/config-file-steps.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {promises as fs} from 'fs';
33
import {dump, load as loadYaml} from 'js-yaml';
44
import {fileTypes} from '@form8ion/core';
55
// eslint-disable-next-line import/no-extraneous-dependencies,import/no-unresolved
6-
import {load, write} from '@form8ion/config-file';
6+
import {load, write, exists} from '@form8ion/config-file';
77

88
import {After, Before, Given, Then, When} from '@cucumber/cucumber';
99
import stubbedFs from 'mock-fs';
@@ -87,6 +87,10 @@ When('the provided config is merged into the existing file', async function () {
8787
return 'pending';
8888
});
8989

90+
When('checking for config existence', async function () {
91+
this.configExists = await exists({name: this.configName});
92+
});
93+
9094
Then('the config is defined in the file', async function () {
9195
const {desiredConfigFileFormat} = this;
9296
const fileContents = await fs.readFile(
@@ -113,3 +117,11 @@ Then('a missing-config error is thrown', async function () {
113117
assert.equal(message, 'No configuration found');
114118
assert.equal(code, 'ENOCONFIG');
115119
});
120+
121+
Then('the config is reported to not be found', async function () {
122+
assert.equal(this.configExists, false);
123+
});
124+
125+
Then('the config is reported to be found', async function () {
126+
assert.equal(this.configExists, true);
127+
});

0 commit comments

Comments
 (0)