From 0f68c45a9c4c763131117dc86b537349434e2299 Mon Sep 17 00:00:00 2001 From: Michael Zugelder Date: Wed, 23 Mar 2022 22:44:06 +0100 Subject: [PATCH] fix: support async loaders with lilconfig.search (#29) --- src/index.ts | 4 ++-- src/spec/index.spec.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 68e23b0..eeab334 100644 --- a/src/index.ts +++ b/src/index.ts @@ -211,7 +211,7 @@ export function lilconfig( // handle package.json if (fileName === 'package.json') { - const pkg = loader(filepath, content); + const pkg = await loader(filepath, content); const maybeConfig = getPackageProp(packageProp, pkg); if (maybeConfig != null) { result.config = maybeConfig; @@ -231,7 +231,7 @@ export function lilconfig( result.config = undefined; } else { validateLoader(loader, loaderKey); - result.config = loader(filepath, content); + result.config = await loader(filepath, content); } result.filepath = filepath; break; diff --git a/src/spec/index.spec.ts b/src/spec/index.spec.ts index 46064f6..770f588 100644 --- a/src/spec/index.spec.ts +++ b/src/spec/index.spec.ts @@ -76,6 +76,42 @@ describe('options', () => { expect(ccResult).toEqual(expected); }); }); + + describe('async loaders', () => { + const config = {data: 42}; + const options = { + loaders: {'.js': async () => config}, + }; + + it('async load', async () => { + const filepath = path.join(__dirname, 'load', 'test-app.js'); + + const result = await lilconfig('test-app', options).load( + filepath, + ); + const ccResult = await cosmiconfig('test-app', options).load( + filepath, + ); + + expect(result).toEqual({config, filepath}); + expect(ccResult).toEqual({config, filepath}); + }); + + it('async search', async () => { + const searchPath = path.join(__dirname, 'search'); + const filepath = path.join(searchPath, 'test-app.config.js'); + + const result = await lilconfig('test-app', options).search( + searchPath, + ); + const ccResult = await cosmiconfig('test-app', options).search( + searchPath, + ); + + expect(result).toEqual({config, filepath}); + expect(ccResult).toEqual({config, filepath}); + }); + }); }); describe('transform', () => {