From 21706e80326fb29996153debf917cf16d9d7bf36 Mon Sep 17 00:00:00 2001 From: Benyamin Galeano Date: Tue, 30 Aug 2022 19:27:11 -0600 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20ObjectBuilder=20clas?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit this class allows to create a complex object from others (can be conditional) --- lib/util/ObjectBuilder.js | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lib/util/ObjectBuilder.js diff --git a/lib/util/ObjectBuilder.js b/lib/util/ObjectBuilder.js new file mode 100644 index 00000000..244525ca --- /dev/null +++ b/lib/util/ObjectBuilder.js @@ -0,0 +1,40 @@ +class ObjectBuilder { + constructor (initial) { + this.currentObject = initial || {}; + } + + /** + * @param {boolean} condition + * @returns {{ add: ConditionalObjectBuilder["add"] }} + */ + if (condition) { + return { + add: (mappedValues) => { + if (condition) { + return this.add(mappedValues); + } + + return this; + } + }; + } + + /** + * @param {{ [key: string]: value }} mappedValues + * @returns {ConditionalObjectBuilder} + */ + add (mappedValues) { + Object.keys(mappedValues) + .forEach((key) => { + this.currentObject[key] = mappedValues[key]; + }); + + return this; + } + + build () { + return this.currentObject; + } +} + +module.exports = ObjectBuilder; From 9bae37df4fdcca4bf603e6099a2331ca971cfcd9 Mon Sep 17 00:00:00 2001 From: Benyamin Galeano Date: Tue, 30 Aug 2022 19:34:29 -0600 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20use=20ObjectBuil?= =?UTF-8?q?der=20instead=20of=20if-else=20statement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/cli.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index c3d7b5d6..55e7dbca 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -9,6 +9,7 @@ const runInteractiveQuestions = require('./runInteractiveQuestions'); const runNonInteractiveMode = require('./runNonInteractiveMode'); const formatCommitMessage = require('./formatCommitMessage'); const getGitDir = require('./util/getGitDir'); +const ObjectBuilder = require('./util/ObjectBuilder'); // eslint-disable-next-line no-process-env const executeCommand = (command, env = process.env) => { @@ -31,11 +32,11 @@ const main = async () => { let state = null; - if (cliOptions.disableEmoji) { - state = createState({disableEmoji: cliOptions.disableEmoji}); - } else { - state = createState(); - } + const configFromFlags = new ObjectBuilder() + .if(cliOptions.disableEmoji).add({disableEmoji: true}) + .build(); + + state = createState(configFromFlags); if (cliOptions.dryRun) { // eslint-disable-next-line no-console From 18e9850caf69d72f9167d219758d406014458a10 Mon Sep 17 00:00:00 2001 From: Benyamin Galeano Date: Tue, 30 Aug 2022 20:48:00 -0600 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20if=20breaking=20opti?= =?UTF-8?q?on=20is=20empty,=20the=20question=20is=20enabled?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/cli.js | 1 + lib/createState.js | 12 +++++++++++- lib/parseArgs.js | 17 ++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 55e7dbca..a4d545d4 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -34,6 +34,7 @@ const main = async () => { const configFromFlags = new ObjectBuilder() .if(cliOptions.disableEmoji).add({disableEmoji: true}) + .if(cliOptions.breaking).add({showBreakingChangeQuestion: true}) .build(); state = createState(configFromFlags); diff --git a/lib/createState.js b/lib/createState.js index bc21af93..8c2f0c46 100644 --- a/lib/createState.js +++ b/lib/createState.js @@ -4,12 +4,22 @@ const getConfig = require('./getConfig'); const createState = (config = {}) => { let root; + const showBreakingChangeQuestion = config.showBreakingChangeQuestion; + delete config.showBreakingChangeQuestion; + try { root = getGitRootDir(); } catch (error) { throw new Error('Could not find Git root folder.'); } + const userConfig = getConfig(root); + const missingBreakingChangeQuestion = !userConfig.questions.includes('breaking'); + + if (showBreakingChangeQuestion && missingBreakingChangeQuestion) { + userConfig.questions.push('breaking'); + } + const state = { answers: { body: '', @@ -21,7 +31,7 @@ const createState = (config = {}) => { type: '' }, config: { - ...getConfig(root), + ...userConfig, ...config }, root diff --git a/lib/parseArgs.js b/lib/parseArgs.js index 503171c1..0aadc253 100644 --- a/lib/parseArgs.js +++ b/lib/parseArgs.js @@ -26,6 +26,18 @@ const helpScreen = ` --lerna Lerna mono-repo packages this commit affects `; +/** If the value is empty the flag is true and the answer is undefined, + * in any other case the flag is false and the answer is the value. + * + * @param {string} value + * @returns {[boolean, string]} + * */ +const flagAndAnswerValues = (value) => { + const isFlagActive = value === ''; + + return [isFlagActive, isFlagActive ? undefined : value]; +}; + const parseArgs = () => { const { // eslint-disable-next-line no-unused-vars @@ -82,7 +94,10 @@ const parseArgs = () => { process.exit(); } + const [breakingChangeFlag, breakingChangeValue] = flagAndAnswerValues(breaking); + const cliOptions = { + breaking: breakingChangeFlag, disableEmoji, dryRun, format, @@ -94,7 +109,7 @@ const parseArgs = () => { const cliAnswers = { body, - breaking, + breaking: breakingChangeValue, issues, lerna, scope,