From 1758b94593554f4ebdce734820ecf35e378d8104 Mon Sep 17 00:00:00 2001 From: Va Da Date: Wed, 23 May 2018 23:09:40 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20make=20changelog=20confi?= =?UTF-8?q?g=20customizable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/getConfig.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 4 ++-- 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/getConfig.js diff --git a/src/getConfig.js b/src/getConfig.js new file mode 100644 index 00000000..212a8134 --- /dev/null +++ b/src/getConfig.js @@ -0,0 +1,48 @@ +/* eslint-disable global-require, import/no-dynamic-require */ +const path = require('path'); +const defaults = require('./defaults'); + +const configFiles = [ + 'changelog.config.js', + 'changelog.config.json' +]; + +const findOverrides = () => { + const dir = process.cwd(); + + for (const file of configFiles) { + try { + return require(path.join(dir, file)); + // eslint-disable-next-line no-empty + } catch (error) {} + } + + try { + const {changelog} = require(path.join(dir, 'package.json')); + + if (changelog) { + return changelog; + } + // eslint-disable-next-line no-empty + } catch (error) {} + + return {}; +}; + +const getConfig = () => { + const overrides = findOverrides(); + + if (typeof overrides !== 'object') { + console.log(new TypeError('Expected changelog config to be an object.')); + + // eslint-disable-next-line no-process-exit + process.exit(1); + } + + return { + ...defaults, + ...overrides + }; +}; + +module.exports = getConfig; diff --git a/src/index.js b/src/index.js index fb18ce48..5c860a62 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ const createPrompter = require('./createPrompter'); -const config = require('./defaults'); +const getConfig = require('./getConfig'); -module.exports = createPrompter(config); +module.exports = createPrompter(getConfig());