Skip to content

Commit

Permalink
fix: support async loaders with lilconfig.search (antonk52#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael42 committed Mar 23, 2022
1 parent 029f357 commit 0f68c45
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions src/spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 0f68c45

Please sign in to comment.