mirror of
https://github.com/all-contributors/cli.git
synced 2025-01-09 13:36:29 +00:00
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:
parent
212e264cd4
commit
2376ada660
3 changed files with 75 additions and 13 deletions
39
src/init/commit-conventions.js
Normal file
39
src/init/commit-conventions.js
Normal 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
|
|
@ -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,
|
||||
},
|
||||
|
|
|
@ -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])
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue