From 4ed9dcd738ee29289add6b42fbf0764205f6626b Mon Sep 17 00:00:00 2001 From: Benyamin Galeano Date: Wed, 31 Aug 2022 18:48:28 -0600 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20replace=20the=20?= =?UTF-8?q?{}=20for=20null=20value?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "typeof null" is "object", replacing this we can know if the configuration is not found --- lib/getConfig.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/getConfig.js b/lib/getConfig.js index 7d68a45e..d360e04d 100644 --- a/lib/getConfig.js +++ b/lib/getConfig.js @@ -41,7 +41,7 @@ const findOverrides = (root) => { return findOverrides(parent); } - return {}; + return null; }; const getConfig = (root) => { From d2b0286ef312c88a1722a3b94a42c2537fa0f3fe Mon Sep 17 00:00:00 2001 From: Benyamin Galeano Date: Wed, 31 Aug 2022 20:24:10 -0600 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20configuration=20can?= =?UTF-8?q?=20be=20a=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit from config function can access to default and parent configurations --- lib/getConfig.js | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/getConfig.js b/lib/getConfig.js index d360e04d..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 null; + 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.'));