forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-commit-messages.js
61 lines (49 loc) · 1.86 KB
/
test-commit-messages.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
require('../lib/bootstrap-local');
const validateCommitMessage = require('./validate-commit-message');
const exec = require('child_process').exec;
const chalk = require('chalk');
const Logger = require('@ngtools/logger').Logger;
require('rxjs/add/operator/filter');
// Configure logger
const logger = new Logger('test-commit-messages');
logger.subscribe((entry) => {
let color = chalk.white;
let output = process.stdout;
switch (entry.level) {
case 'info': color = chalk.white; break;
case 'warn': color = chalk.yellow; break;
case 'error': color = chalk.red; output = process.stderr; break;
case 'fatal': color = (x) => chalk.bold(chalk.red(x)); output = process.stderr; break;
}
output.write(color(entry.message) + '\n');
});
logger
.filter((entry) => entry.level == 'fatal')
.subscribe(() => {
process.stderr.write('A fatal error happened. See details above.');
process.exit(1);
});
// Note: This is based on the gulp task found in the angular/angular repository
exec(
'git fetch origin master && git log --reverse --format=%s origin/master.. --no-merges',
(error, stdout, stderr) => {
if (error) {
logger.fatal(stderr);
return;
}
const output = stdout.trim();
if (output.length == 0) {
logger.warn('There are zero new commits between this HEAD and master');
return;
}
const commitsByLine = output.split(/\n/);
logger.info(`Examining ${commitsByLine.length} commit(s) between HEAD and master`);
const someCommitsInvalid = !commitsByLine.every(validateCommitMessage);
if (someCommitsInvalid) {
logger.error('Please fix the failing commit messages before continuing...');
logger.fatal(
'Commit message guidelines: https://github.com/angular/angular-cli/blob/master/CONTRIBUTING.md#commit');
} else {
logger.info('All commit messages are valid.');
}
});