Skip to content

Commit

Permalink
Resolve config internally
Browse files Browse the repository at this point in the history
  • Loading branch information
cenobitedk committed Jan 14, 2020
1 parent a76f097 commit 7c95c10
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { cosmiconfigSync } = require("cosmiconfig");
const { isString, isNumber, isPlainObject } = require("is-what");
const path = require("path");
const pkg = require("../package.json");
Expand Down Expand Up @@ -56,16 +55,62 @@ 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) {
const searchedConfig = resolveConfig();
const configuration = Object.assign(
{},
defaults,
searchedConfig && searchedConfig.config,
searchedConfig.config,
customConfig
);

Expand Down

0 comments on commit 7c95c10

Please sign in to comment.