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
|
|
|
|
|
|
|
import {getRepoBaseDir} from '../utils/config';
|
2020-05-20 17:22:29 -04:00
|
|
|
import {info} from '../utils/console';
|
2020-03-26 13:45:09 -04:00
|
|
|
|
2020-07-30 17:01:03 -04:00
|
|
|
import {deleteCommitMessageDraft, saveCommitMessageDraft} from './commit-message-draft';
|
2020-03-26 13:45:09 -04:00
|
|
|
import {validateCommitMessage} from './validate';
|
|
|
|
|
|
|
|
/** Validate commit message at the provided file path. */
|
|
|
|
export function validateFile(filePath: string) {
|
2020-04-08 16:56:05 -04:00
|
|
|
const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8');
|
2020-03-26 13:45:09 -04:00
|
|
|
if (validateCommitMessage(commitMessage)) {
|
2020-05-20 17:22:29 -04:00
|
|
|
info('√ Valid commit message');
|
2020-07-30 17:01:03 -04:00
|
|
|
deleteCommitMessageDraft(filePath);
|
2020-03-20 15:24:12 -04:00
|
|
|
return;
|
2020-03-26 13:45:09 -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-03-20 15:24:12 -04:00
|
|
|
// If the validation did not return true, exit as a failure.
|
|
|
|
process.exit(1);
|
2020-03-26 13:45:09 -04:00
|
|
|
}
|