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 |
|
17 | var msgError = preGit.customMsgPatternError();
|
18 |
|
19 | if(msgError) {
|
20 | console.error(msgError);
|
21 | console.error('');
|
22 | }
|
23 |
|
24 | log('invalid commit message, must match the following pattern: ' + pattern, msg);
|
25 | process.exit(-1);
|
26 | }
|
27 |
|
28 | return true;
|
29 |
|
30 | }
|
31 |
|
32 | function checkMessage(msg) {
|
33 |
|
34 | const msgPattern = preGit.customMsgPattern();
|
35 |
|
36 | if (msgPattern) {
|
37 | checkMessageAgainstPattern(msg, msgPattern);
|
38 | }
|
39 |
|
40 | const wizard = preGit.wizard();
|
41 | if (!wizard) {
|
42 | log('no commit message wizard defined');
|
43 | process.exit(0);
|
44 | }
|
45 |
|
46 | log('found commit message wizard with name', wizard.name);
|
47 |
|
48 | la(check.fn(wizard.validate),
|
49 | 'missing wizard validate method,', Object.keys(wizard));
|
50 | la(check.fn(preGit.printError),
|
51 | 'missing preGit.printError,', Object.keys(preGit));
|
52 |
|
53 | log('checking commit message:', msg);
|
54 | const isValid = wizard.validate(msg);
|
55 | if (!isValid) {
|
56 | log('invalid commit message', msg);
|
57 | process.exit(-1);
|
58 | }
|
59 | log('valid git commit message');
|
60 | }
|
61 |
|
62 | ggit.commitMessage()
|
63 | .then((msg) => {
|
64 | return process.env.TEST_GIT_MESSAGE || msg;
|
65 | })
|
66 | .then(checkMessage)
|
67 | .catch((err) => {
|
68 |
|
69 | console.error(err);
|
70 | process.exit(-1);
|
71 | })
|
72 | .done();
|