Skip to content

Commit

Permalink
fix: gracefully support empty config files (again) (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Apr 26, 2022
1 parent ec07ab7 commit a3046e3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-phones-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@proload/core": patch
---

Gracefully support empty config files (again)
1 change: 1 addition & 0 deletions fixtures/empty-export/test.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const other = true;
Empty file added fixtures/empty/test.config.mjs
Empty file.
13 changes: 13 additions & 0 deletions packages/core/lib/esm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@ async function load(namespace, opts = {}) {

let rawValue = await requireOrImportWithMiddleware(filePath);
if (filePath.endsWith('package.json')) rawValue = rawValue[namespace];
// Important: "empty" config files will be returned as `Module {}`
// We should handle them here
if (rawValue && !(rawValue instanceof Object)) {
if (mustExist) {
assert(
true,
`Resolved a ${namespace} configuration, but no configuration was exported`,
"ERR_PROLOAD_NOT_FOUND"
);
} else {
return;
}
}
const resolvedValue = await resolveExtensions(namespace, {
filePath,
value: rawValue,
Expand Down
30 changes: 30 additions & 0 deletions packages/core/test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@ test('missing but not mustExist', async () => {
type(mdl, 'undefined')
});

test('empty but not mustExist', async () => {
let mdl = await load('test', { cwd: resolve(`fixtures/empty`), mustExist: false });
type(mdl, 'undefined')
});

test('empty but mustExist (default)', async () => {
let err = 0;
try {
let mdl = await load('test', { cwd: resolve(`fixtures/empty`) });
} catch (e) {
err += 1;
}
is(err, 1);
});

test('empty export but not mustExist', async () => {
let mdl = await load('test', { cwd: resolve(`fixtures/empty-export`), mustExist: false });
type(mdl, 'undefined')
});

test('empty export but mustExist (default)', async () => {
let err = 0;
try {
let mdl = await load('test', { cwd: resolve(`fixtures/empty-export`) });
} catch (e) {
err += 1;
}
is(err, 1);
});

const throwFixtures = ['ts', 'ts-config', 'json', 'json-config'];

for (const fixture of throwFixtures) {
Expand Down

0 comments on commit a3046e3

Please sign in to comment.