| 
									
										
										
										
											2020-03-26 10:45:09 -07: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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | import * as yargs from 'yargs'; | 
					
						
							|  |  |  | import {validateFile} from './validate-file'; | 
					
						
							| 
									
										
										
										
											2020-03-20 12:24:12 -07:00
										 |  |  | import {validateCommitRange} from './validate-range'; | 
					
						
							| 
									
										
										
										
											2020-03-26 10:45:09 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** Build the parser for the commit-message commands. */ | 
					
						
							|  |  |  | export function buildCommitMessageParser(localYargs: yargs.Argv) { | 
					
						
							| 
									
										
										
										
											2020-03-20 12:24:12 -07:00
										 |  |  |   return localYargs.help() | 
					
						
							|  |  |  |       .strict() | 
					
						
							|  |  |  |       .command( | 
					
						
							| 
									
										
										
										
											2020-04-08 23:56:05 +03:00
										 |  |  |           'pre-commit-validate', 'Validate the most recent commit message', { | 
					
						
							|  |  |  |             'file': { | 
					
						
							|  |  |  |               type: 'string', | 
					
						
							|  |  |  |               conflicts: ['file-env-variable'], | 
					
						
							|  |  |  |               description: 'The path of the commit message file.', | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             'file-env-variable': { | 
					
						
							|  |  |  |               type: 'string', | 
					
						
							|  |  |  |               conflicts: ['file'], | 
					
						
							|  |  |  |               description: | 
					
						
							|  |  |  |                   'The key of the environment variable for the path of the commit message file.', | 
					
						
							|  |  |  |               coerce: arg => { | 
					
						
							|  |  |  |                 const file = process.env[arg]; | 
					
						
							|  |  |  |                 if (!file) { | 
					
						
							|  |  |  |                   throw new Error(`Provided environment variable "${arg}" was not found.`); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return file; | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           args => { | 
					
						
							|  |  |  |             const file = args.file || args.fileEnvVariable || '.git/COMMIT_EDITMSG'; | 
					
						
							|  |  |  |             validateFile(file); | 
					
						
							| 
									
										
										
										
											2020-03-20 12:24:12 -07:00
										 |  |  |           }) | 
					
						
							|  |  |  |       .command( | 
					
						
							|  |  |  |           'validate-range', 'Validate a range of commit messages', { | 
					
						
							|  |  |  |             'range': { | 
					
						
							|  |  |  |               description: 'The range of commits to check, e.g. --range abc123..xyz456', | 
					
						
							|  |  |  |               demandOption: '  A range must be provided, e.g. --range abc123..xyz456', | 
					
						
							|  |  |  |               type: 'string', | 
					
						
							|  |  |  |               requiresArg: true, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           argv => { | 
					
						
							|  |  |  |             // If on CI, and not pull request number is provided, assume the branch
 | 
					
						
							|  |  |  |             // being run on is an upstream branch.
 | 
					
						
							|  |  |  |             if (process.env['CI'] && process.env['CI_PULL_REQUEST'] === 'false') { | 
					
						
							|  |  |  |               console.info( | 
					
						
							|  |  |  |                   `Since valid commit messages are enforced by PR linting on CI, we do not\n` + | 
					
						
							|  |  |  |                   `need to validate commit messages on CI runs on upstream branches.\n\n` + | 
					
						
							|  |  |  |                   `Skipping check of provided commit range`); | 
					
						
							|  |  |  |               return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             validateCommitRange(argv.range); | 
					
						
							|  |  |  |           }); | 
					
						
							| 
									
										
										
										
											2020-03-26 10:45:09 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (require.main == module) { | 
					
						
							|  |  |  |   buildCommitMessageParser(yargs).parse(); | 
					
						
							|  |  |  | } |