| 
									
										
										
										
											2017-01-19 16:24:47 -08:00
										 |  |  | #!/usr/bin/env node
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 13:03:40 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-19 16:24:47 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * GIT commit message format enforcement | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Note: this script was originally written by Vojta for AngularJS :-) | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | const configPath = path.resolve(__dirname, './commit-message.json'); | 
					
						
							|  |  |  | const config = JSON.parse(fs.readFileSync(configPath, 'utf8')); | 
					
						
							| 
									
										
										
										
											2018-01-25 15:55:00 -08:00
										 |  |  | const PATTERN = /^(\w+)(?:\(([^)]+)\))?\: (.+)$/; | 
					
						
							|  |  |  | const FIXUP_SQUASH = /^(fixup|squash)\! /i; | 
					
						
							| 
									
										
										
										
											2018-02-20 17:11:10 -08:00
										 |  |  | const REVERT = /^revert:? (\"(.*)\"|(.*))?$/i; | 
					
						
							| 
									
										
										
										
											2017-01-19 16:24:47 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(commitSubject) { | 
					
						
							| 
									
										
										
										
											2018-01-25 15:55:00 -08:00
										 |  |  |   commitSubject = commitSubject.replace(FIXUP_SQUASH, ''); | 
					
						
							|  |  |  |   commitSubject = commitSubject.replace(REVERT, function(m, g1, g2, g3) { return g2 || g3; }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-19 16:24:47 -08:00
										 |  |  |   if (commitSubject.length > config['maxLength']) { | 
					
						
							|  |  |  |     error(`The commit message is longer than ${config['maxLength']} characters`, commitSubject); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const match = PATTERN.exec(commitSubject); | 
					
						
							| 
									
										
										
										
											2018-01-25 15:55:00 -08:00
										 |  |  |   if (!match) { | 
					
						
							| 
									
										
										
										
											2017-01-19 16:24:47 -08:00
										 |  |  |     error( | 
					
						
							| 
									
										
										
										
											2018-01-25 15:55:00 -08:00
										 |  |  |         `The commit message does not match the format of '<type>(<scope>): <subject>' OR 'Revert: "type(<scope>): <subject>"'`, | 
					
						
							| 
									
										
										
										
											2017-01-19 16:24:47 -08:00
										 |  |  |         commitSubject); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 15:55:00 -08:00
										 |  |  |   const type = match[1]; | 
					
						
							| 
									
										
										
										
											2017-01-19 16:24:47 -08:00
										 |  |  |   if (config['types'].indexOf(type) === -1) { | 
					
						
							|  |  |  |     error( | 
					
						
							|  |  |  |         `${type} is not an allowed type.\n => TYPES: ${config['types'].join(', ')}`, commitSubject); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 15:55:00 -08:00
										 |  |  |   const scope = match[2]; | 
					
						
							| 
									
										
										
										
											2017-01-19 16:24:47 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (scope && !config['scopes'].includes(scope)) { | 
					
						
							|  |  |  |     error( | 
					
						
							|  |  |  |         `"${scope}" is not an allowed scope.\n => SCOPES: ${config['scopes'].join(', ')}`, | 
					
						
							|  |  |  |         commitSubject); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function error(errorMessage, commitMessage) { | 
					
						
							|  |  |  |   console.error(`INVALID COMMIT MSG: "${commitMessage}"\n => ERROR: ${errorMessage}`); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-09 13:46:19 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports.config = config; |