Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: commit conventions awareness #159

Merged
merged 7 commits into from Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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