forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-commit-message.js
60 lines (49 loc) · 1.15 KB
/
validate-commit-message.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
/* @flow */
const fs = require('fs');
const dedent = require('dedent');
const chalk = require('chalk');
const MAX_LENGTH = 72;
const MESSAGE_PATTERN = /^(?:fixup!\s*)?(\w*)(\(([\w$.*/-]*)\))?: (.*)$/;
const TYPES = [
'feat',
'fix',
'docs',
'style',
'refactor',
'perf',
'test',
'chore',
'revert',
'BREAKING',
];
const commit = process.argv[2];
try {
const message = fs.readFileSync(commit, 'utf8').split('\n')[0];
let error;
if (message.length > MAX_LENGTH) {
error = `It should be less than ${chalk.blue(
String(MAX_LENGTH)
)} characters`;
} else {
const match = MESSAGE_PATTERN.exec(message);
if (!match || !TYPES.includes(match[1])) {
error = dedent`
It should match the format '${chalk.blue('<type>: <subject>')}'
Where ${chalk.blue('<type>')} is one of:
${TYPES.map(t => `- ${t}`).join('\n')}
`;
}
}
if (error) {
// eslint-disable-next-line no-console
console.log(dedent`
${chalk.red('Invalid commit message:')} ${chalk.bold(message)}
${error}
`);
process.exit(1);
} else {
process.exit(0);
}
} catch (error) {
process.exit(1);
}