/
index.js
68 lines (59 loc) 路 1.95 KB
/
index.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
// @flow
import { execa } from 'execa'
import fs from 'fs'
import { type Answers } from '../prompts'
const withHook = (answers: Answers) => {
try {
const scope = answers.scope ? `(${answers.scope}): ` : ''
const title = `${answers.gitmoji} ${scope}${answers.title}`
const commitMessage = `${title}\n\n${answers.message}`
fs.writeFileSync(process.argv[3], commitMessage)
} catch (error) {
console.error(error)
process.exit(1)
} finally {
process.exit(0)
}
}
export const registerHookInterruptionHandler = () => {
// Allow to interrupt the hook without cancelling the commit
process.on('SIGINT', () => {
console.warn('gitmoji-cli was interrupted')
process.exit(0)
})
}
export const cancelIfRebasing = (): Promise<void> =>
execa('git', ['rev-parse', '--absolute-git-dir']).then(
({ stdout: gitDirectory }) => {
// see https://stackoverflow.com/questions/3921409/how-to-know-if-there-is-a-git-rebase-in-progress
// to understand how a rebase is detected
if (
fs.existsSync(gitDirectory + '/rebase-merge') ||
fs.existsSync(gitDirectory + '/rebase-apply')
) {
process.exit(0)
}
}
)
export const COMMIT_MESSAGE_SOURCE = 4
export const cancelIfAmending = (): Promise<void> =>
new Promise<void>((resolve) => {
/*
from https://git-scm.com/docs/githooks#_prepare_commit_msg
the commit message source is passed as second argument and corresponding 4 for gitmoji-cli
`gitmoji --hook $1 $2`
*/
const commitMessageSource: ?string = process.argv[COMMIT_MESSAGE_SOURCE]
if (
commitMessageSource &&
(commitMessageSource.startsWith('commit') ||
commitMessageSource.startsWith('merge'))
) {
process.exit(0)
}
resolve()
})
// I avoid Promise.all to avoid race condition in future cancel callbacks
export const cancelIfNeeded = (): Promise<void> =>
cancelIfAmending().then(cancelIfRebasing)
export default withHook