All files / git-hooks/src/conventional-commits-validation-hook index.ts

100% Statements 16/16
100% Branches 1/1
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                1x     1x   11x   11x 11x 11x 11x 10x   10x 1x 1x     10x   1x 1x         13x  
/**
 * Copyright (c) 2021-present, Matti Bar-Zeev.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
 
/* eslint-disable no-useless-escape */
import fs from 'fs';
import GitHook from '../GitHook';
 
const conventionalCommitsValidationHook: GitHook = {
    execute: () => {
        try {
            const conventionalCommitMessageRegExp: RegExp =
                /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\([\s\w\-\.]+\))?(!)?: ([\w ])+([\s\S]*)/g;
            let exitCode = 0;
            const commitMsgFile = process.argv[2];
            const message: string = fs.readFileSync(commitMsgFile, 'utf8');
            const isValid: boolean = conventionalCommitMessageRegExp.test(message);
 
            if (!isValid) {
                console.log('Cannot commit: the commit message does not comply with conventional commits standards.');
                exitCode = 1;
            }
 
            process.exit(exitCode);
        } catch (error) {
            console.error(`Cannot commit: unexpected error occurred: ${error.message}`);
            process.exit(1);
        }
    },
};
 
export default conventionalCommitsValidationHook;