From 711433e9a65227372a03f35ddd83263dc6ceaf98 Mon Sep 17 00:00:00 2001 From: Berkmann18 Date: Fri, 1 Feb 2019 17:33:48 +0000 Subject: [PATCH 1/3] feat: commit convention awareness [wip] fix https://github.com/all-contributors/all-contributors-bot/issues/132 --- src/init/commit-conventions.js | 33 +++++++++++++++++++++++++++++++++ src/init/prompt.js | 17 +++++++++++++++-- src/util/git.js | 33 ++++++++++++++++++++++----------- 3 files changed, 70 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..3dd93764 --- /dev/null +++ b/src/init/commit-conventions.js @@ -0,0 +1,33 @@ +const conventions = { + angular: { + name: 'Angular', + msg: 'docs:', + lowercase: true, + }, + 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: 'Docs:', + }, +} + +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..e2ae4812 100644 --- a/src/util/git.js +++ b/src/util/git.js @@ -2,9 +2,14 @@ const path = require('path') const spawn = require('child_process').spawn const _ = require('lodash/fp') const pify = require('pify') +const convention = require('../init/commit-conventions') +const {readConfig} = require('./config-file') + +// const config = readConfig('.all-contributorsrc') +// console.log(c) const commitTemplate = - '<%= (newContributor ? "Add" : "Update") %> @<%= username %> as a contributor' + '<%= prefix %> <%= (newContributor ? "Add" : "Update") %> @<%= username %> as a contributor' const getRemoteOriginData = pify(cb => { let output = '' @@ -37,27 +42,33 @@ 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) { + // console.log('opts=', options, 'data=', data) const files = options.files.concat(options.config) const absolutePathFiles = files.map(file => { return path.resolve(process.cwd(), file) }) + const config = readConfig(options.config) + const prefix = convention[config.commitConvention].msg return spawnGitCommand(['add'].concat(absolutePathFiles)).then(() => { - const commitMessage = _.template(options.commitTemplate || commitTemplate)( - data, - ) + const commitMessage = _.template(options.commitTemplate || commitTemplate)({ + ...data, + prefix, + }) return spawnGitCommand(['commit', '-m', commitMessage]) }) } From 6271ef25c520c772e2f7291690c5e00304e13426 Mon Sep 17 00:00:00 2001 From: Berkmann18 Date: Mon, 4 Feb 2019 16:14:13 +0000 Subject: [PATCH 2/3] refactor(commit-conventions): added a tranformer for `angular` and refactored `none` --- src/init/commit-conventions.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/init/commit-conventions.js b/src/init/commit-conventions.js index 3dd93764..5600533a 100644 --- a/src/init/commit-conventions.js +++ b/src/init/commit-conventions.js @@ -3,6 +3,12 @@ const conventions = { 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', @@ -22,7 +28,7 @@ const conventions = { }, none: { name: 'None', - msg: 'Docs:', + msg: '', }, } From cc500f4648b779c324532d051f2a29040ab99c5e Mon Sep 17 00:00:00 2001 From: Berkmann18 Date: Mon, 4 Feb 2019 16:16:34 +0000 Subject: [PATCH 3/3] feat(git): added lowercase transformation With some refactoring --- src/util/git.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/util/git.js b/src/util/git.js index e2ae4812..e082925f 100644 --- a/src/util/git.js +++ b/src/util/git.js @@ -2,12 +2,9 @@ const path = require('path') const spawn = require('child_process').spawn const _ = require('lodash/fp') const pify = require('pify') -const convention = require('../init/commit-conventions') +const conventions = require('../init/commit-conventions') const {readConfig} = require('./config-file') -// const config = readConfig('.all-contributorsrc') -// console.log(c) - const commitTemplate = '<%= prefix %> <%= (newContributor ? "Add" : "Update") %> @<%= username %> as a contributor' @@ -57,18 +54,20 @@ const spawnGitCommand = pify((args, cb) => { }) function commit(options, data) { - // console.log('opts=', options, 'data=', data) const files = options.files.concat(options.config) const absolutePathFiles = files.map(file => { return path.resolve(process.cwd(), file) }) const config = readConfig(options.config) - const prefix = convention[config.commitConvention].msg + const commitConvention = conventions[config.commitConvention] + return spawnGitCommand(['add'].concat(absolutePathFiles)).then(() => { - const commitMessage = _.template(options.commitTemplate || commitTemplate)({ + let commitMessage = _.template(options.commitTemplate || commitTemplate)({ ...data, - prefix, + prefix: commitConvention.msg, }) + if (commitConvention.lowercase) + commitMessage = commitConvention.transform(commitMessage) return spawnGitCommand(['commit', '-m', commitMessage]) }) }