diff --git a/dev-infra/commit-message/cli.ts b/dev-infra/commit-message/cli.ts index aada6a3178..6e459d19d2 100644 --- a/dev-infra/commit-message/cli.ts +++ b/dev-infra/commit-message/cli.ts @@ -14,9 +14,29 @@ export function buildCommitMessageParser(localYargs: yargs.Argv) { return localYargs.help() .strict() .command( - 'pre-commit-validate', 'Validate the most recent commit message', {}, - () => { - validateFile('.git/COMMIT_EDITMSG'); + '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); }) .command( 'validate-range', 'Validate a range of commit messages', { diff --git a/dev-infra/commit-message/validate-file.ts b/dev-infra/commit-message/validate-file.ts index b5c02555a9..9769caa33b 100644 --- a/dev-infra/commit-message/validate-file.ts +++ b/dev-infra/commit-message/validate-file.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ import {readFileSync} from 'fs'; -import {join} from 'path'; +import {resolve} from 'path'; import {getRepoBaseDir} from '../utils/config'; @@ -14,7 +14,7 @@ import {validateCommitMessage} from './validate'; /** Validate commit message at the provided file path. */ export function validateFile(filePath: string) { - const commitMessage = readFileSync(join(getRepoBaseDir(), filePath), 'utf8'); + const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8'); if (validateCommitMessage(commitMessage)) { console.info('√ Valid commit message'); return; diff --git a/package.json b/package.json index 0f2589f789..647c82bbb3 100644 --- a/package.json +++ b/package.json @@ -199,7 +199,7 @@ "cldr-data-coverage": "full", "husky": { "hooks": { - "commit-msg": "yarn -s ng-dev commit-message pre-commit-validate" + "commit-msg": "yarn -s ng-dev commit-message pre-commit-validate --file-env-variable HUSKY_GIT_PARAMS" } } }