Skip to content

feat: config can be a function #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions lib/getConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ const findOverrides = (root) => {
const filename = path.resolve(dir, file);

if (fs.existsSync(filename) && fs.statSync(filename).isFile()) {
return require(filename);
return {
dir,
overrides: require(filename)
};
}
}

Expand All @@ -31,7 +34,10 @@ const findOverrides = (root) => {
const changelog = require(pkgFilename).config.commitizen.changelog;

if (changelog) {
return changelog;
return {
dir,
overrides: changelog
};
}
// eslint-disable-next-line no-empty
} catch (error) {}
Expand All @@ -41,11 +47,45 @@ const findOverrides = (root) => {
return findOverrides(parent);
}

return {};
return {
dir,
overrides: null
};
};

const getConfigContext = (currentConfigDir) => {
const memo = {parentConfig: null};

return {
defaultConfig: defaults,
get parentConfig () {
if (memo.parentConfig) {
return memo.parentConfig;
}

const parent = path.resolve(currentConfigDir, '..');

// eslint-disable-next-line no-use-before-define
memo.parentConfig = getConfig(parent);

return memo.parentConfig;
}
};
};

const getOverrides = (root) => {
// eslint-disable-next-line prefer-const
let {overrides, dir: overridesDir} = findOverrides(root);

if (typeof overrides === 'function') {
overrides = overrides(getConfigContext(overridesDir));
}

return overrides;
};

const getConfig = (root) => {
const overrides = findOverrides(root);
const overrides = getOverrides(root);

if (typeof overrides !== 'object') {
signale.fatal(new TypeError('Expected changelog config to be an object.'));
Expand Down