diff --git a/lib/getConfig.js b/lib/getConfig.js index 7d68a45e..a9d490ce 100644 --- a/lib/getConfig.js +++ b/lib/getConfig.js @@ -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) + }; } } @@ -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) {} @@ -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.'));