2020-03-26 13:45:09 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-03-26 13:45:09 -04:00
|
|
|
*
|
|
|
|
* 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 {readFileSync} from 'fs';
|
2020-04-08 16:56:05 -04:00
|
|
|
import {resolve} from 'path';
|
2020-03-26 13:45:09 -04:00
|
|
|
|
2020-09-14 16:21:31 -04:00
|
|
|
import {getRepoBaseDir} from '../../utils/config';
|
|
|
|
import {error, green, info, log, red, yellow} from '../../utils/console';
|
2020-03-26 13:45:09 -04:00
|
|
|
|
2020-09-14 16:21:31 -04:00
|
|
|
import {deleteCommitMessageDraft, saveCommitMessageDraft} from '../commit-message-draft';
|
|
|
|
import {printValidationErrors, validateCommitMessage} from '../validate';
|
2020-03-26 13:45:09 -04:00
|
|
|
|
|
|
|
/** Validate commit message at the provided file path. */
|
2020-09-08 17:38:04 -04:00
|
|
|
export function validateFile(filePath: string, isErrorMode: boolean) {
|
2020-04-08 16:56:05 -04:00
|
|
|
const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8');
|
2020-09-03 17:54:31 -04:00
|
|
|
const {valid, errors} = validateCommitMessage(commitMessage);
|
|
|
|
if (valid) {
|
|
|
|
info(`${green('√')} Valid commit message`);
|
2020-07-30 17:01:03 -04:00
|
|
|
deleteCommitMessageDraft(filePath);
|
2020-09-08 17:38:04 -04:00
|
|
|
process.exitCode = 0;
|
2020-03-20 15:24:12 -04:00
|
|
|
return;
|
2020-03-26 13:45:09 -04:00
|
|
|
}
|
2020-09-03 17:54:31 -04:00
|
|
|
|
2020-09-08 17:38:04 -04:00
|
|
|
/** Function used to print to the console log. */
|
|
|
|
let printFn = isErrorMode ? error : log;
|
|
|
|
|
|
|
|
printFn(`${isErrorMode ? red('✘') : yellow('!')} Invalid commit message`);
|
|
|
|
printValidationErrors(errors, printFn);
|
|
|
|
if (isErrorMode) {
|
|
|
|
printFn(red('Aborting commit attempt due to invalid commit message.'));
|
|
|
|
printFn(
|
|
|
|
red('Commit message aborted as failure rather than warning due to local configuration.'));
|
|
|
|
} else {
|
|
|
|
printFn(yellow('Before this commit can be merged into the upstream repository, it must be'));
|
|
|
|
printFn(yellow('amended to follow commit message guidelines.'));
|
|
|
|
}
|
2020-09-03 17:54:31 -04:00
|
|
|
|
2020-07-30 17:01:03 -04:00
|
|
|
// On all invalid commit messages, the commit message should be saved as a draft to be
|
|
|
|
// restored on the next commit attempt.
|
|
|
|
saveCommitMessageDraft(filePath, commitMessage);
|
2020-09-08 17:38:04 -04:00
|
|
|
// Set the correct exit code based on if invalid commit message is an error.
|
|
|
|
process.exitCode = isErrorMode ? 1 : 0;
|
2020-03-26 13:45:09 -04:00
|
|
|
}
|