From 7c95c10e1bb7898c75511de2c282816e9efc7079 Mon Sep 17 00:00:00 2001 From: Jeppe Hasseriis Date: Tue, 14 Jan 2020 12:35:45 +0100 Subject: [PATCH] Resolve config internally --- lib/config.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/lib/config.js b/lib/config.js index 2e14c6e..4e7fefd 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,4 +1,3 @@ -const { cosmiconfigSync } = require("cosmiconfig"); const { isString, isNumber, isPlainObject } = require("is-what"); const path = require("path"); const pkg = require("../package.json"); @@ -56,8 +55,54 @@ function validateConfiguration(configuration) { } function resolveConfig() { - const explorer = cosmiconfigSync(pkg.name); - return explorer.search(); + // const explorer = cosmiconfigSync(pkg.name); + // return explorer.search(); + const key = pkg.name; + const result = { + config: undefined, + filepath: undefined, + isEmpty: true + }; + + const hostPkgPath = path.join(cwd, "package.json"); + const hostPkg = require(hostPkgPath); + if (hostPkg.hasOwnProperty(key)) { + result.isEmpty = false; + result.config = hostPkg[key]; + result.file = hostPkgPath; + } + + if (result.isEmpty) { + const files = [ + `.${key}rc`, + `.${key}rc.json`, + `.${key}rc.js`, + `${key}.config.js` + ]; + let i = 0; + let fileName; + let filePath; + let config; + + while (result.isEmpty && i < files.length) { + fileName = files[i++]; + filePath = path.join(cwd, fileName); + config = require(filePath); + result.isEmpty = Boolean(file); + } + + if (!result.isEmpty) { + result.file = filePath; + const isFunction = typeof config === "function"; + result.config = isFunction ? config() : config; + } + } + + if (!result.isEmpty) { + delete result.isEmpty; + } + + return result; } function processConfiguration(customConfig) { @@ -65,7 +110,7 @@ function processConfiguration(customConfig) { const configuration = Object.assign( {}, defaults, - searchedConfig && searchedConfig.config, + searchedConfig.config, customConfig );