From 171d967993025285a146acd5877ecf3e2c55eca3 Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Wed, 20 May 2020 14:22:29 -0700 Subject: [PATCH] feat(dev-infra): migrate commit-message tool to use new logging system (#37232) Migrate the commit-message tool in ng-dev to use new logging system rather than directly calling console.* to create a better experience for users. PR Close #37232 --- dev-infra/commit-message/BUILD.bazel | 1 + dev-infra/commit-message/cli.ts | 11 +++++++---- dev-infra/commit-message/validate-file.ts | 3 ++- dev-infra/commit-message/validate-range.ts | 7 +++++-- dev-infra/commit-message/validate.ts | 23 ++++++++++++---------- 5 files changed, 28 insertions(+), 17 deletions(-) diff --git a/dev-infra/commit-message/BUILD.bazel b/dev-infra/commit-message/BUILD.bazel index 152bc195b7..2c6d3be371 100644 --- a/dev-infra/commit-message/BUILD.bazel +++ b/dev-infra/commit-message/BUILD.bazel @@ -32,6 +32,7 @@ ts_library( "@npm//@types/events", "@npm//@types/jasmine", "@npm//@types/node", + "@npm//inquirer", ], ) diff --git a/dev-infra/commit-message/cli.ts b/dev-infra/commit-message/cli.ts index 6e459d19d2..bd67bcfdf0 100644 --- a/dev-infra/commit-message/cli.ts +++ b/dev-infra/commit-message/cli.ts @@ -6,6 +6,9 @@ * found in the LICENSE file at https://angular.io/license */ import * as yargs from 'yargs'; + +import {info} from '../utils/console'; + import {validateFile} from './validate-file'; import {validateCommitRange} from './validate-range'; @@ -51,10 +54,10 @@ export function buildCommitMessageParser(localYargs: yargs.Argv) { // If on CI, and not pull request number is provided, assume the branch // being run on is an upstream branch. if (process.env['CI'] && process.env['CI_PULL_REQUEST'] === 'false') { - console.info( - `Since valid commit messages are enforced by PR linting on CI, we do not\n` + - `need to validate commit messages on CI runs on upstream branches.\n\n` + - `Skipping check of provided commit range`); + info(`Since valid commit messages are enforced by PR linting on CI, we do not`); + info(`need to validate commit messages on CI runs on upstream branches.`); + info(); + info(`Skipping check of provided commit range`); return; } validateCommitRange(argv.range); diff --git a/dev-infra/commit-message/validate-file.ts b/dev-infra/commit-message/validate-file.ts index 9769caa33b..75f4fb4241 100644 --- a/dev-infra/commit-message/validate-file.ts +++ b/dev-infra/commit-message/validate-file.ts @@ -9,6 +9,7 @@ import {readFileSync} from 'fs'; import {resolve} from 'path'; import {getRepoBaseDir} from '../utils/config'; +import {info} from '../utils/console'; import {validateCommitMessage} from './validate'; @@ -16,7 +17,7 @@ import {validateCommitMessage} from './validate'; export function validateFile(filePath: string) { const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8'); if (validateCommitMessage(commitMessage)) { - console.info('√ Valid commit message'); + info('√ Valid commit message'); return; } // If the validation did not return true, exit as a failure. diff --git a/dev-infra/commit-message/validate-range.ts b/dev-infra/commit-message/validate-range.ts index 1633aac443..713cac0f51 100644 --- a/dev-infra/commit-message/validate-range.ts +++ b/dev-infra/commit-message/validate-range.ts @@ -6,6 +6,9 @@ * found in the LICENSE file at https://angular.io/license */ import {exec} from 'shelljs'; + +import {info} from '../utils/console'; + import {parseCommitMessage, validateCommitMessage, ValidateCommitMessageOptions} from './validate'; // Whether the provided commit is a fixup commit. @@ -31,7 +34,7 @@ export function validateCommitRange(range: string) { // Separate the commits from a single string into individual commits const commits = result.split(randomValueSeparator).map(l => l.trim()).filter(line => !!line); - console.info(`Examining ${commits.length} commit(s) in the provided range: ${range}`); + info(`Examining ${commits.length} commit(s) in the provided range: ${range}`); // Check each commit in the commit range. Commits are allowed to be fixup commits for other // commits in the provided commit range. @@ -46,7 +49,7 @@ export function validateCommitRange(range: string) { }); if (allCommitsInRangeValid) { - console.info('√ All commit messages in range valid.'); + info('√ All commit messages in range valid.'); } else { // Exit with a non-zero exit code if invalid commit messages have // been discovered. diff --git a/dev-infra/commit-message/validate.ts b/dev-infra/commit-message/validate.ts index c30570e467..4cc05cf2e4 100644 --- a/dev-infra/commit-message/validate.ts +++ b/dev-infra/commit-message/validate.ts @@ -5,6 +5,8 @@ * 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 {error} from '../utils/console'; + import {getCommitMessageConfig} from './config'; /** Options for commit message validation. */ @@ -62,8 +64,8 @@ export function parseCommitMessage(commitMsg: string) { /** Validate a commit message against using the local repo's config. */ export function validateCommitMessage( commitMsg: string, options: ValidateCommitMessageOptions = {}) { - function error(errorMessage: string) { - console.error( + function printError(errorMessage: string) { + error( `INVALID COMMIT MSG: \n` + `${'─'.repeat(40)}\n` + `${commitMsg}\n` + @@ -91,7 +93,7 @@ export function validateCommitMessage( // the git history anyway, unless the options provided to not allow squash commits. if (commit.isSquash) { if (options.disallowSquash) { - error('The commit must be manually squashed into the target commit'); + printError('The commit must be manually squashed into the target commit'); return false; } return true; @@ -104,7 +106,7 @@ export function validateCommitMessage( // check. if (commit.isFixup) { if (options.nonFixupCommitHeaders && !options.nonFixupCommitHeaders.includes(commit.header)) { - error( + printError( 'Unable to find match for fixup commit among prior commits: ' + (options.nonFixupCommitHeaders.map(x => `\n ${x}`).join('') || '-')); return false; @@ -117,22 +119,23 @@ export function validateCommitMessage( // Checking commit header // //////////////////////////// if (commit.header.length > config.maxLineLength) { - error(`The commit message header is longer than ${config.maxLineLength} characters`); + printError(`The commit message header is longer than ${config.maxLineLength} characters`); return false; } if (!commit.type) { - error(`The commit message header does not match the expected format.`); + printError(`The commit message header does not match the expected format.`); return false; } if (!config.types.includes(commit.type)) { - error(`'${commit.type}' is not an allowed type.\n => TYPES: ${config.types.join(', ')}`); + printError(`'${commit.type}' is not an allowed type.\n => TYPES: ${config.types.join(', ')}`); return false; } if (commit.scope && !config.scopes.includes(commit.scope)) { - error(`'${commit.scope}' is not an allowed scope.\n => SCOPES: ${config.scopes.join(', ')}`); + printError( + `'${commit.scope}' is not an allowed scope.\n => SCOPES: ${config.scopes.join(', ')}`); return false; } @@ -146,14 +149,14 @@ export function validateCommitMessage( ////////////////////////// if (commit.bodyWithoutLinking.trim().length < config.minBodyLength) { - error(`The commit message body does not meet the minimum length of ${ + printError(`The commit message body does not meet the minimum length of ${ config.minBodyLength} characters`); return false; } const bodyByLine = commit.body.split('\n'); if (bodyByLine.some(line => line.length > config.maxLineLength)) { - error( + printError( `The commit messsage body contains lines greater than ${config.maxLineLength} characters`); return false; }