-
Notifications
You must be signed in to change notification settings - Fork 146
/
git.js
78 lines (68 loc) · 1.94 KB
/
git.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 =
'<%= prefix %> <%= (newContributor ? "Add" : "Update") %> @<%= username %> as a contributor'
const getRemoteOriginData = pify(cb => {
let output = ''
const git = spawn('git', 'config --get remote.origin.url'.split(' '))
git.stdout.on('data', data => {
output += data
})
git.stderr.on('data', cb)
git.on('close', () => {
cb(null, output)
})
})
function parse(originUrl) {
const result = /:(\w+)\/([A-Za-z0-9-_]+)/.exec(originUrl)
if (!result) {
return null
}
return {
projectOwner: result[1],
projectName: result[2],
}
}
function getRepoInfo() {
return getRemoteOriginData().then(parse)
}
const spawnGitCommand = pify((args, cb) => {
const git = spawn('git', args)
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))
} else {
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(() => {
let commitMessage = _.template(options.commitTemplate || commitTemplate)({
...data,
prefix: commitConvention.msg,
})
if (commitConvention.lowercase)
commitMessage = commitConvention.transform(commitMessage)
return spawnGitCommand(['commit', '-m', commitMessage])
})
}
module.exports = {
commit,
getRepoInfo,
}