Skip to content

Commit

Permalink
fix(getConfig): add option for a default module configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
darlanalves committed May 4, 2024
1 parent bd0cc43 commit 60bb055
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
26 changes: 17 additions & 9 deletions src/__tests__/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ import { describe, expect, it, vi } from 'vitest';
describe('configuration', () => {
const cwd = process.cwd();

it('should return an empty object if no configuration file exists', async () => {
vi.spyOn(process, 'cwd').mockImplementation(() => cwd);
const config = await getConfig('plugin');
expect(config).toEqual({});
});
describe('load module configurations', () => {
it('should return an empty object if no configuration file exists', async () => {
vi.spyOn(process, 'cwd').mockImplementation(() => cwd);
const config = await getConfig('plugin');
expect(config).toEqual({});
});

it('should merge configurations and defaults', async () => {
vi.spyOn(process, 'cwd').mockImplementation(() => cwd);
const config = await getConfig('plugin', { foo: true });
expect(config).toEqual({ foo: true });
});

it('should load module configurations', async () => {
vi.spyOn(process, 'cwd').mockImplementation(() => cwd + '/src/__tests__');
const config = await getConfig('plugin');
it('should load module configurations', async () => {
vi.spyOn(process, 'cwd').mockImplementation(() => cwd + '/src/__tests__');
const config = await getConfig('plugin');

expect(config).toEqual({ foo: true });
expect(config).toEqual({ foo: true });
});
});
});
5 changes: 3 additions & 2 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ export class CloudConfiguration {
}
}

export async function getConfig(moduleName: string): Promise<ModuleConfiguration> {
export async function getConfig<T extends Record<string, any>>(moduleName: string, defaults: T = null): Promise<ModuleConfiguration | T> {
const filePath = join(process.cwd(), 'configuration', `${moduleName}.json`);
const config = readJson<ModuleConfiguration>(filePath);
return config || {};

return Object.assign({}, config, defaults);
}

0 comments on commit 60bb055

Please sign in to comment.