(filePath: string, isErrorMode: boolean)
| 19 | |
| 20 | /** Validate commit message at the provided file path. */ |
| 21 | export async function validateFile(filePath: string, isErrorMode: boolean) { |
| 22 | const git = await GitClient.get(); |
| 23 | const commitMessage = readFileSync(resolve(git.baseDir, filePath), 'utf8'); |
| 24 | const {valid, errors} = await validateCommitMessage(commitMessage); |
| 25 | if (valid) { |
| 26 | Log.info(`${green('✔')} Valid commit message`); |
| 27 | deleteCommitMessageDraft(filePath); |
| 28 | process.exitCode = 0; |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | /** Function used to print to the console log. */ |
| 33 | let printFn = isErrorMode ? Log.error : Log.log; |
| 34 | |
| 35 | printFn(isErrorMode ? '✘ Invalid commit message.' : yellow('! Invalid commit message.')); |
| 36 | printValidationErrors(errors, printFn); |
| 37 | if (isErrorMode) { |
| 38 | printFn('Aborting commit attempt due to invalid commit message.'); |
| 39 | printFn('Commit message aborted as failure rather than warning due to local configuration.'); |
| 40 | } else { |
| 41 | printFn(yellow('Before this commit can be merged into the upstream repository, it must be')); |
| 42 | printFn(yellow('amended to follow commit message guidelines.')); |
| 43 | } |
| 44 | |
| 45 | // On all invalid commit messages, the commit message should be saved as a draft to be |
| 46 | // restored on the next commit attempt. |
| 47 | saveCommitMessageDraft(filePath, commitMessage); |
| 48 | // Set the correct exit code based on if invalid commit message is an error. |
| 49 | process.exitCode = isErrorMode ? 1 : 0; |
| 50 | } |
no test coverage detected