1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const ggit = require('ggit');
|
6 | const preGit = require('pre-git');
|
7 | const la = require('lazy-ass');
|
8 | const check = require('check-more-types');
|
9 | const log = require('debug')('pre-git');
|
10 |
|
11 | function checkMessageAgainstPattern(msg, pattern) {
|
12 |
|
13 | var regex = new RegExp(pattern);
|
14 |
|
15 | if (!regex.test(msg)) {
|
16 | log('invalid commit message, must match the following pattern: ' + pattern, msg);
|
17 | process.exit(-1);
|
18 | }
|
19 |
|
20 | return true;
|
21 |
|
22 | }
|
23 |
|
24 | function checkMessage(msg) {
|
25 |
|
26 | const msgPattern = preGit.customMsgPattern();
|
27 |
|
28 | if (msgPattern) {
|
29 | checkMessageAgainstPattern(msg, msgPattern);
|
30 | }
|
31 |
|
32 | const wizard = preGit.wizard();
|
33 | if (!wizard) {
|
34 | log('no commit message wizard defined');
|
35 | process.exit(0);
|
36 | }
|
37 |
|
38 | log('found commit message wizard with name', wizard.name);
|
39 |
|
40 | la(check.fn(wizard.validate),
|
41 | 'missing wizard validate method,', Object.keys(wizard));
|
42 | la(check.fn(preGit.printError),
|
43 | 'missing preGit.printError,', Object.keys(preGit));
|
44 |
|
45 | log('checking commit message:', msg);
|
46 | const isValid = wizard.validate(msg);
|
47 | if (!isValid) {
|
48 | log('invalid commit message', msg);
|
49 | process.exit(-1);
|
50 | }
|
51 | log('valid git commit message');
|
52 | }
|
53 |
|
54 | ggit.commitMessage()
|
55 | .then((msg) => {
|
56 | return process.env.TEST_GIT_MESSAGE || msg;
|
57 | })
|
58 | .then(checkMessage)
|
59 | .catch((err) => {
|
60 |
|
61 | console.error(err);
|
62 | process.exit(-1);
|
63 | })
|
64 | .done();
|