/
getDefaultCommitContent.js
44 lines (37 loc) 路 1.23 KB
/
getDefaultCommitContent.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
// @flow
import fs from 'fs'
import { type CommitOptions } from '@commands/commit'
import COMMIT_MODES from '@constants/commit'
const COMMIT_FILE_PATH_INDEX = 3
const COMMIT_TITLE_LINE_INDEX = 0
const COMMIT_MESSAGE_LINE_INDEX = 2
const getDefaultCommitContent = (
options: CommitOptions
): { title: ?string, message: ?string, scope: ?string } => {
/*
Since the hook is called with `gitmoji --hook $1`
According to https://git-scm.com/docs/githooks#_prepare_commit_msg,
the commit file path will be stored in the 4th argument of the command
*/
const commitFilePath: string = process.argv[COMMIT_FILE_PATH_INDEX]
if (options.mode === COMMIT_MODES.CLIENT || !fs.existsSync(commitFilePath)) {
return {
message: options['message'] || null,
scope: options['scope'] || null,
title: options['title'] || null
}
}
const commitFileContent: Array<string> = fs
.readFileSync(commitFilePath)
.toString()
.split('\n')
return {
message:
commitFileContent.length > COMMIT_MESSAGE_LINE_INDEX
? commitFileContent[COMMIT_MESSAGE_LINE_INDEX]
: null,
scope: options['scope'],
title: commitFileContent[COMMIT_TITLE_LINE_INDEX]
}
}
export default getDefaultCommitContent