Skip to content

Commit

Permalink
Revert empty config (#24)
Browse files Browse the repository at this point in the history
* Revert "fix(#8): improve handling when file is matched but empty (#11)"

This reverts commit 4c7215e.

* chore: changesets

* fix: resolve conflict
  • Loading branch information
natemoo-re authored Apr 26, 2022
1 parent d96ce4c commit 72c8577
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 71 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-rats-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@proload/core": patch
---

Rollback empty config file change
Empty file removed fixtures/empty/test.config.mjs
Empty file.
80 changes: 24 additions & 56 deletions packages/core/lib/esm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,23 @@ const toReadFile = promisify(readFile);
const require = createRequire(import.meta.url);

let merge = deepmerge;
const defaultExtensions = ["js", "cjs", "mjs"];
const defaultFileNames = ["[name].config"];
const defaultExtensions = ['js', 'cjs', 'mjs'];
const defaultFileNames = ['[name].config'];

const validNames = (namespace) => {
const extensionPlugins = load.plugins.filter((p) =>
Array.isArray(p.extensions)
);
const fileNamePlugins = load.plugins.filter((p) =>
Array.isArray(p.fileNames)
);
const validExtensions = [...defaultExtensions].concat(
...extensionPlugins.map((p) => p.extensions)
);
const validFileNames = [...defaultFileNames].concat(
...fileNamePlugins.map((p) => p.fileNames)
);
const extensionPlugins = load.plugins.filter(p => Array.isArray(p.extensions));
const fileNamePlugins = load.plugins.filter(p => Array.isArray(p.fileNames));
const validExtensions = [...defaultExtensions].concat(...extensionPlugins.map(p => p.extensions));
const validFileNames = [...defaultFileNames].concat(...fileNamePlugins.map(p => p.fileNames));

const result = validFileNames
.map((fileName) => fileName.replace("[name]", namespace))
.map(fileName => fileName.replace('[name]', namespace))
.reduce((acc, fileName) => {
return [...acc].concat(
...validExtensions.map(
(ext) => `${fileName}${ext ? "." + ext.replace(/^\./, "") : ""}`
)
);
return [...acc].concat(...validExtensions.map(ext => `${fileName}${ext ? '.' + ext.replace(/^\./, '') : ''}`))
}, []);

return result;
};
}

/**
* @param {any} val
Expand All @@ -61,14 +49,13 @@ const requireOrImportWithMiddleware = (filePath) => {
(plugin) => typeof plugin.transform !== "undefined"
);
return requireOrImport(filePath, { middleware: registerPlugins }).then(
async (mdl) =>
Promise.all(
transformPlugins.map((plugin) => {
return Promise.resolve(plugin.transform(mdl)).then((result) => {
if (result) mdl = result;
});
})
).then(() => mdl)
async (mdl) => Promise.all(
transformPlugins.map((plugin) => {
return Promise.resolve(plugin.transform(mdl)).then((result) => {
if (result) mdl = result;
});
})
).then(() => mdl)
);
};

Expand Down Expand Up @@ -105,15 +92,15 @@ async function resolveExtension(namespace, { filePath, extension }) {
if (resolvedPath && existsSync(resolvedPath)) {
break;
} else {
resolvedPath = null;
resolvedPath = null
}
} catch (e) {}
}
}
if (!resolvedPath) {
resolvedPath = require.resolve(extension, { cwd: dirname(filePath) });
}
if (!resolvedPath) return;
if (!resolvedPath) return
const value = await requireOrImportWithMiddleware(resolvedPath);

return { filePath: resolvedPath, value };
Expand All @@ -126,6 +113,7 @@ async function resolveExtensions(
) {
let value = typeof raw === "function" ? await raw(context) : raw;
if (Array.isArray(value)) return value;

assert(
isObject(value),
`${namespace} configuration expects an "object" but encountered ${value}`
Expand Down Expand Up @@ -161,7 +149,6 @@ async function resolveExtensions(
* @param {import('../index').LoadOptions} opts
*/
async function resolveConfig(namespace, opts = {}) {
// if (opts)
const accepted = validNames(namespace);
const { context, accept } = opts;
const input = opts.cwd || process.cwd();
Expand Down Expand Up @@ -237,37 +224,19 @@ async function resolveConfig(namespace, opts = {}) {
*/
async function load(namespace, opts = {}) {
const { context } = opts;

let mustExist = true;
if (typeof opts.mustExist !== "undefined") {
mustExist = opts.mustExist;
if (typeof opts.mustExist !== 'undefined') {
mustExist = opts.mustExist
}
const filePath = await resolveConfig(namespace, opts);
if (mustExist) {
assert(
!!filePath,
`Unable to resolve a ${namespace} configuration`,
"ERR_PROLOAD_NOT_FOUND"
);
assert(!!filePath, `Unable to resolve a ${namespace} configuration`, 'ERR_PROLOAD_NOT_FOUND');
} else if (!filePath) {
return;
}

let rawValue = await requireOrImportWithMiddleware(filePath);
if (filePath.endsWith("package.json")) rawValue = rawValue[namespace];
const hasExport = ('default' in rawValue);
if (!hasExport) {
if (mustExist) {
assert(
true,
`Resolved a ${namespace} configuration, but no configuration was exported`,
"ERR_PROLOAD_NOT_FOUND"
);
} else {
return;
}
}

if (filePath.endsWith('package.json')) rawValue = rawValue[namespace];
const resolvedValue = await resolveExtensions(namespace, {
filePath,
value: rawValue,
Expand All @@ -285,10 +254,9 @@ const defaultPlugins = [
{
name: "@proload/extract-default",
transform(mdl) {
if (typeof mdl === "undefined") return mdl;
if (mdl.default && Object.keys(mdl).length === 1) {
return mdl.default;
}
};

return mdl;
},
Expand Down
15 changes: 0 additions & 15 deletions packages/core/test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@ test('missing but mustExist (default)', async () => {
is(err, 1);
});

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('missing but not mustExist', async () => {
let mdl = await load('test', { cwd: resolve(`fixtures/missing`), mustExist: false });
type(mdl, 'undefined')
Expand Down

0 comments on commit 72c8577

Please sign in to comment.