Skip to content

Commit

Permalink
feat: commit conventions awareness (#159)
Browse files Browse the repository at this point in the history
* feat: commit convention awareness [wip]

fix all-contributors/app#132

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

* feat(git): added lowercase transformation

With some refactoring
  • Loading branch information
Berkmann18 committed Apr 15, 2019
1 parent 212e264 commit 2376ada
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 13 deletions.
39 changes: 39 additions & 0 deletions 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
17 changes: 15 additions & 2 deletions 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 = [
{
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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,
},
Expand Down
32 changes: 21 additions & 11 deletions src/util/git.js
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -37,27 +39,35 @@ 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) {
const files = options.files.concat(options.config)
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])
})
}
Expand Down

0 comments on commit 2376ada

Please sign in to comment.