Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 7 additions & 5 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -31,11 +32,12 @@ 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})
.if(cliOptions.breaking).add({showBreakingChangeQuestion: true})
.build();

state = createState(configFromFlags);

if (cliOptions.dryRun) {
// eslint-disable-next-line no-console
Expand Down
12 changes: 11 additions & 1 deletion lib/createState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand All @@ -21,7 +31,7 @@ const createState = (config = {}) => {
type: ''
},
config: {
...getConfig(root),
...userConfig,
...config
},
root
Expand Down
17 changes: 16 additions & 1 deletion lib/parseArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,7 +94,10 @@ const parseArgs = () => {
process.exit();
}

const [breakingChangeFlag, breakingChangeValue] = flagAndAnswerValues(breaking);

const cliOptions = {
breaking: breakingChangeFlag,
disableEmoji,
dryRun,
format,
Expand All @@ -94,7 +109,7 @@ const parseArgs = () => {

const cliAnswers = {
body,
breaking,
breaking: breakingChangeValue,
issues,
lerna,
scope,
Expand Down
40 changes: 40 additions & 0 deletions lib/util/ObjectBuilder.js
Original file line number Diff line number Diff line change
@@ -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;