From 2376ada660965e576fb81e788071ee32b7309947 Mon Sep 17 00:00:00 2001 From: Maximilian Berkmann Date: Mon, 15 Apr 2019 15:05:00 +0100 Subject: [PATCH] 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 --- src/init/commit-conventions.js | 39 ++++++++++++++++++++++++++++++++++ src/init/prompt.js | 17 +++++++++++++-- src/util/git.js | 32 ++++++++++++++++++---------- 3 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 src/init/commit-conventions.js diff --git a/src/init/commit-conventions.js b/src/init/commit-conventions.js new file mode 100644 index 00000000..5600533a --- /dev/null +++ b/src/init/commit-conventions.js @@ -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 diff --git a/src/init/prompt.js b/src/init/prompt.js index c29410d1..87446a54 100644 --- a/src/init/prompt.js +++ b/src/init/prompt.js @@ -1,6 +1,7 @@ const _ = require('lodash/fp') const inquirer = require('inquirer') const git = require('../util').git +const conventions = require('./commit-conventions') const questions = [ { @@ -32,7 +33,8 @@ const questions = [ { type: 'input', 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) { if (answers.repoType === 'github') { return 'https://github.com' @@ -77,9 +79,19 @@ const questions = [ 'Do you want this badge to auto-commit when contributors are added?', 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() { return git @@ -101,6 +113,7 @@ module.exports = function prompt() { files: uniqueFiles([answers.contributorFile, answers.badgeFile]), imageSize: answers.imageSize, commit: answers.commit, + commitConvention: answers.commitConvention, contributors: [], contributorsPerLine: 7, }, diff --git a/src/util/git.js b/src/util/git.js index fdde39d9..e082925f 100644 --- a/src/util/git.js +++ b/src/util/git.js @@ -2,9 +2,11 @@ const path = require('path') const spawn = require('child_process').spawn const _ = require('lodash/fp') const pify = require('pify') +const conventions = require('../init/commit-conventions') +const {readConfig} = require('./config-file') const commitTemplate = - '<%= (newContributor ? "Add" : "Update") %> @<%= username %> as a contributor' + '<%= prefix %> <%= (newContributor ? "Add" : "Update") %> @<%= username %> as a contributor' const getRemoteOriginData = pify(cb => { let output = '' @@ -37,16 +39,18 @@ function getRepoInfo() { const spawnGitCommand = pify((args, cb) => { const git = spawn('git', args) - const bufs = []; - git.stderr.on('data', (buf) => bufs.push(buf)); - git.on('close', (code) => { + const bufs = [] + git.stderr.on('data', buf => bufs.push(buf)) + git.on('close', code => { if (code) { - const msg = Buffer.concat(bufs).toString() || `git ${args.join(' ')} - exit code: ${code}`; - cb(new Error(msg)); + const msg = + Buffer.concat(bufs).toString() || + `git ${args.join(' ')} - exit code: ${code}` + cb(new Error(msg)) } else { - cb(null); + cb(null) } - }); + }) }) function commit(options, data) { @@ -54,10 +58,16 @@ function commit(options, data) { const absolutePathFiles = files.map(file => { return path.resolve(process.cwd(), file) }) + const config = readConfig(options.config) + const commitConvention = conventions[config.commitConvention] + return spawnGitCommand(['add'].concat(absolutePathFiles)).then(() => { - const commitMessage = _.template(options.commitTemplate || commitTemplate)( - data, - ) + let commitMessage = _.template(options.commitTemplate || commitTemplate)({ + ...data, + prefix: commitConvention.msg, + }) + if (commitConvention.lowercase) + commitMessage = commitConvention.transform(commitMessage) return spawnGitCommand(['commit', '-m', commitMessage]) }) }