feat: commit conventions awareness (#159)

* feat: commit convention awareness [wip]

fix https://github.com/all-contributors/all-contributors-bot/issues/132

* refactor(commit-conventions): added a tranformer for `angular` and refactored `none`

* feat(git): added lowercase transformation

With some refactoring
This commit is contained in:
Maximilian Berkmann 2019-04-15 15:05:00 +01:00 committed by GitHub
parent 212e264cd4
commit 2376ada660
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 13 deletions

View file

@ -0,0 +1,39 @@
const conventions = {
angular: {
name: 'Angular',
msg: 'docs:',
lowercase: true,
transform(msg) {
return msg.replace(
/(^.*?) ([A-Z][a-z]+) \w*/,
(_, ...words) => `${words[0]} ${words[1].toLowerCase()} `,
)
},
},
atom: {
name: 'Atom',
msg: ':memo:',
},
ember: {
name: 'Ember',
msg: '[DOC canary]',
},
eslint: {
name: 'ESLint',
msg: 'Docs:',
},
jshint: {
name: 'JSHint',
msg: '[[DOCS]]',
},
none: {
name: 'None',
msg: '',
},
}
Object.keys(conventions).forEach(style => {
conventions[style].value = style
})
module.exports = conventions

View file

@ -1,6 +1,7 @@
const _ = require('lodash/fp') const _ = require('lodash/fp')
const inquirer = require('inquirer') const inquirer = require('inquirer')
const git = require('../util').git const git = require('../util').git
const conventions = require('./commit-conventions')
const questions = [ const questions = [
{ {
@ -32,7 +33,8 @@ const questions = [
{ {
type: 'input', type: 'input',
name: 'repoHost', name: 'repoHost',
message: 'Where is the repository hosted? Hit Enter if it\'s on GitHub or GitLab', message:
"Where is the repository hosted? Hit Enter if it's on GitHub or GitLab",
default: function(answers) { default: function(answers) {
if (answers.repoType === 'github') { if (answers.repoType === 'github') {
return 'https://github.com' return 'https://github.com'
@ -77,9 +79,19 @@ const questions = [
'Do you want this badge to auto-commit when contributors are added?', 'Do you want this badge to auto-commit when contributors are added?',
default: true, default: true,
}, },
{
type: 'list',
name: 'commitConvention',
message: 'What commit convention would you want it to use?',
choices: Object.values(conventions),
default: 'none',
},
] ]
const uniqueFiles = _.flow(_.compact, _.uniq) const uniqueFiles = _.flow(
_.compact,
_.uniq,
)
module.exports = function prompt() { module.exports = function prompt() {
return git return git
@ -101,6 +113,7 @@ module.exports = function prompt() {
files: uniqueFiles([answers.contributorFile, answers.badgeFile]), files: uniqueFiles([answers.contributorFile, answers.badgeFile]),
imageSize: answers.imageSize, imageSize: answers.imageSize,
commit: answers.commit, commit: answers.commit,
commitConvention: answers.commitConvention,
contributors: [], contributors: [],
contributorsPerLine: 7, contributorsPerLine: 7,
}, },

View file

@ -2,9 +2,11 @@ const path = require('path')
const spawn = require('child_process').spawn const spawn = require('child_process').spawn
const _ = require('lodash/fp') const _ = require('lodash/fp')
const pify = require('pify') const pify = require('pify')
const conventions = require('../init/commit-conventions')
const {readConfig} = require('./config-file')
const commitTemplate = const commitTemplate =
'<%= (newContributor ? "Add" : "Update") %> @<%= username %> as a contributor' '<%= prefix %> <%= (newContributor ? "Add" : "Update") %> @<%= username %> as a contributor'
const getRemoteOriginData = pify(cb => { const getRemoteOriginData = pify(cb => {
let output = '' let output = ''
@ -37,16 +39,18 @@ function getRepoInfo() {
const spawnGitCommand = pify((args, cb) => { const spawnGitCommand = pify((args, cb) => {
const git = spawn('git', args) const git = spawn('git', args)
const bufs = []; const bufs = []
git.stderr.on('data', (buf) => bufs.push(buf)); git.stderr.on('data', buf => bufs.push(buf))
git.on('close', (code) => { git.on('close', code => {
if (code) { if (code) {
const msg = Buffer.concat(bufs).toString() || `git ${args.join(' ')} - exit code: ${code}`; const msg =
cb(new Error(msg)); Buffer.concat(bufs).toString() ||
`git ${args.join(' ')} - exit code: ${code}`
cb(new Error(msg))
} else { } else {
cb(null); cb(null)
} }
}); })
}) })
function commit(options, data) { function commit(options, data) {
@ -54,10 +58,16 @@ function commit(options, data) {
const absolutePathFiles = files.map(file => { const absolutePathFiles = files.map(file => {
return path.resolve(process.cwd(), file) return path.resolve(process.cwd(), file)
}) })
const config = readConfig(options.config)
const commitConvention = conventions[config.commitConvention]
return spawnGitCommand(['add'].concat(absolutePathFiles)).then(() => { return spawnGitCommand(['add'].concat(absolutePathFiles)).then(() => {
const commitMessage = _.template(options.commitTemplate || commitTemplate)( let commitMessage = _.template(options.commitTemplate || commitTemplate)({
data, ...data,
) prefix: commitConvention.msg,
})
if (commitConvention.lowercase)
commitMessage = commitConvention.transform(commitMessage)
return spawnGitCommand(['commit', '-m', commitMessage]) return spawnGitCommand(['commit', '-m', commitMessage])
}) })
} }