This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
forked from streamich/git-cz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 👌 do dependency injection of config
- Loading branch information
Showing
5 changed files
with
154 additions
and
147 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const fs = require('fs'); | ||
const inquirer = require('inquirer'); | ||
const wrap = require('word-wrap'); | ||
const appRoot = require('app-root-path'); | ||
const { | ||
createPackagesQuestion, | ||
createQuestions | ||
} = require('./questions'); | ||
const LimitedInput = require('./LimitedInput'); | ||
const { | ||
getAllPackages, | ||
getChangedPackages | ||
} = require('./lernaUtils'); | ||
|
||
const MAX_LINE_WIDTH = 72; | ||
|
||
inquirer.registerPrompt('limitedInput', LimitedInput); | ||
|
||
const IS_LERNA_PROJECT = fs.existsSync(appRoot.resolve('lerna.json')); | ||
|
||
const makeAffectsLine = function (answers) { | ||
const selectedPackages = answers.packages; | ||
|
||
if (selectedPackages && selectedPackages.length) { | ||
return `\naffects: ${selectedPackages.join(', ')}`; | ||
} | ||
|
||
return ''; | ||
}; | ||
|
||
module.exports = (config) => { | ||
const prompter = { | ||
prompter (cz, commit) { | ||
let promptQuestions = createQuestions(config); | ||
|
||
if (IS_LERNA_PROJECT) { | ||
const allPackages = getAllPackages().map((pkg) => pkg.name); | ||
const changedPackages = getChangedPackages(); | ||
|
||
promptQuestions = promptQuestions.concat(createPackagesQuestion(allPackages, changedPackages)); | ||
} | ||
|
||
return inquirer.prompt(promptQuestions) | ||
.then((answers) => { | ||
const wrapOptions = { | ||
indent: '', | ||
trim: true, | ||
width: MAX_LINE_WIDTH | ||
}; | ||
|
||
const emoji = config.types[answers.type].emoji; | ||
const emojiPrefix = emoji ? emoji + ' ' : ''; | ||
const head = answers.type + ': ' + emojiPrefix + answers.subject; | ||
const affectsLine = makeAffectsLine(answers); | ||
|
||
// Wrap these lines at MAX_LINE_WIDTH character | ||
const body = wrap(answers.body + affectsLine, wrapOptions); | ||
const breaking = wrap(answers.breaking, wrapOptions); | ||
const footer = wrap(answers.footer, wrapOptions); | ||
|
||
let msg; | ||
|
||
msg = head; | ||
|
||
if (body) { | ||
msg += '\n\n' + body; | ||
} | ||
|
||
if (breaking) { | ||
msg += '\n\nBREAKING CHANGE: ' + breaking; | ||
} | ||
|
||
if (footer) { | ||
msg += '\n\nIssues: ' + footer; | ||
} | ||
|
||
return commit(msg); | ||
}); | ||
} | ||
}; | ||
|
||
return prompter; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,4 @@ | ||
const fs = require('fs'); | ||
const inquirer = require('inquirer'); | ||
const wrap = require('word-wrap'); | ||
const appRoot = require('app-root-path'); | ||
const defaults = require('./defaults'); | ||
const { | ||
makePackagesQuestion, | ||
questions | ||
} = require('./prompt/questions'); | ||
const LimitedInput = require('./prompt/LimitedInput'); | ||
const { | ||
getAllPackages, | ||
getChangedPackages | ||
} = require('./lernaUtils'); | ||
const createPrompter = require('./createPrompter'); | ||
const config = require('./defaults'); | ||
|
||
const MAX_LINE_WIDTH = 72; | ||
|
||
inquirer.registerPrompt('limitedInput', LimitedInput); | ||
|
||
const IS_LERNA_PROJECT = fs.existsSync(appRoot.resolve('lerna.json')); | ||
|
||
const makeAffectsLine = function (answers) { | ||
const selectedPackages = answers.packages; | ||
|
||
if (selectedPackages && selectedPackages.length) { | ||
return `\naffects: ${selectedPackages.join(', ')}`; | ||
} | ||
|
||
return ''; | ||
}; | ||
|
||
module.exports = { | ||
prompter (cz, commit) { | ||
let promptQuestions = questions; | ||
|
||
if (IS_LERNA_PROJECT) { | ||
const allPackages = getAllPackages().map((pkg) => pkg.name); | ||
const changedPackages = getChangedPackages(); | ||
|
||
promptQuestions = promptQuestions.concat(makePackagesQuestion(allPackages, changedPackages)); | ||
} | ||
|
||
return inquirer.prompt(promptQuestions) | ||
.then((answers) => { | ||
const wrapOptions = { | ||
indent: '', | ||
trim: true, | ||
width: MAX_LINE_WIDTH | ||
}; | ||
|
||
const emoji = defaults.types[answers.type].emoji; | ||
const emojiPrefix = emoji ? emoji + ' ' : ''; | ||
const head = answers.type + ': ' + emojiPrefix + answers.subject; | ||
const affectsLine = makeAffectsLine(answers); | ||
|
||
// Wrap these lines at MAX_LINE_WIDTH character | ||
const body = wrap(answers.body + affectsLine, wrapOptions); | ||
const breaking = wrap(answers.breaking, wrapOptions); | ||
const footer = wrap(answers.footer, wrapOptions); | ||
|
||
let msg; | ||
|
||
msg = head; | ||
|
||
if (body) { | ||
msg += '\n\n' + body; | ||
} | ||
|
||
if (breaking) { | ||
msg += '\n\nBREAKING CHANGE: ' + breaking; | ||
} | ||
|
||
if (footer) { | ||
msg += '\n\nIssues: ' + footer; | ||
} | ||
|
||
return commit(msg); | ||
}); | ||
} | ||
}; | ||
module.exports = createPrompter(config); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const pad = require('pad-right'); | ||
|
||
const typeToListItem = ({description, emoji, value}) => ({ | ||
name: (emoji || '❔') + ' ' + pad(value + ':', 12, ' ') + description, | ||
value | ||
}); | ||
|
||
const createQuestions = (config) => { | ||
const MIN_SUBJECT_LENGTH_ERROR_MESSAGE = `The subject must have at least ${config.minMessageLength} characters`; | ||
const questions = [ | ||
{ | ||
choices: config.list.map((type) => typeToListItem(config.types[type])), | ||
message: 'Select the type of change that you\'re committing:', | ||
name: 'type', | ||
type: 'list' | ||
}, | ||
{ | ||
filter: (input) => { | ||
let subject; | ||
|
||
subject = input.trim(); | ||
while (subject.endsWith('.')) { | ||
subject = subject.substr(0, subject.length - 1).trim(); | ||
} | ||
|
||
return subject; | ||
}, | ||
leadingLabel: (answers) => `${answers.type}:`, | ||
|
||
// Minus 3 chars are for emoji + space. | ||
maxLength: config.maxMessageLength - 3, | ||
message: 'Write a short, imperative mood description of the change:', | ||
name: 'subject', | ||
type: 'limitedInput', | ||
validate: (input) => input.length >= config.minMessageLength || MIN_SUBJECT_LENGTH_ERROR_MESSAGE | ||
}, | ||
{ | ||
message: 'Provide a longer description of the change:\n', | ||
name: 'body', | ||
type: 'input' | ||
}, | ||
{ | ||
message: 'List any breaking changes:\n BREAKING CHANGE:', | ||
name: 'breaking', | ||
type: 'input' | ||
}, | ||
{ | ||
message: 'Reference any task that this commit closes:\n Issues:', | ||
name: 'footer', | ||
type: 'input' | ||
} | ||
]; | ||
|
||
return questions; | ||
}; | ||
|
||
const createPackagesQuestion = (allPackages, changedPackages) => ({ | ||
choices: allPackages, | ||
default: changedPackages, | ||
message: `The packages that this commit has affected (${changedPackages.length} detected)\n`, | ||
name: 'packages', | ||
type: 'checkbox' | ||
}); | ||
|
||
module.exports = { | ||
createPackagesQuestion, | ||
createQuestions | ||
}; |