refactor(dev-infra): refactor commit-message files (#38845)
Refactor the commit-message files to be consistent with how other ng-dev tooling is structured. PR Close #38845
This commit is contained in:

committed by
Andrew Kushnir

parent
af3b401e15
commit
3d94919800
62
dev-infra/commit-message/validate-file/cli.ts
Normal file
62
dev-infra/commit-message/validate-file/cli.ts
Normal file
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google LLC All Rights Reserved.
|
||||
*
|
||||
* 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 {Arguments, Argv, CommandModule} from 'yargs';
|
||||
|
||||
import {getUserConfig} from '../../utils/config';
|
||||
|
||||
import {validateFile} from './validate-file';
|
||||
|
||||
|
||||
export interface ValidateFileOptions {
|
||||
file?: string;
|
||||
fileEnvVariable?: string;
|
||||
error: boolean;
|
||||
}
|
||||
|
||||
/** Builds the command. */
|
||||
function builder(yargs: Argv) {
|
||||
return yargs
|
||||
.option('file', {
|
||||
type: 'string',
|
||||
conflicts: ['file-env-variable'],
|
||||
description: 'The path of the commit message file.',
|
||||
})
|
||||
.option('file-env-variable' as 'fileEnvVariable', {
|
||||
type: 'string',
|
||||
conflicts: ['file'],
|
||||
description: 'The key of the environment variable for the path of the commit message file.',
|
||||
coerce: (arg: string) => {
|
||||
const file = process.env[arg];
|
||||
if (!file) {
|
||||
throw new Error(`Provided environment variable "${arg}" was not found.`);
|
||||
}
|
||||
return file;
|
||||
},
|
||||
})
|
||||
.option('error', {
|
||||
type: 'boolean',
|
||||
description:
|
||||
'Whether invalid commit messages should be treated as failures rather than a warning',
|
||||
default: !!getUserConfig().commitMessage?.errorOnInvalidMessage || !!process.env['CI']
|
||||
});
|
||||
}
|
||||
|
||||
/** Handles the command. */
|
||||
async function handler({error, file, fileEnvVariable}: Arguments<ValidateFileOptions>) {
|
||||
const filePath = file || fileEnvVariable || '.git/COMMIT_EDITMSG';
|
||||
validateFile(filePath, error);
|
||||
}
|
||||
|
||||
/** yargs command module describing the command. */
|
||||
export const ValidateFileModule: CommandModule<{}, ValidateFileOptions> = {
|
||||
handler,
|
||||
builder,
|
||||
command: 'pre-commit-validate',
|
||||
describe: 'Validate the most recent commit message',
|
||||
};
|
47
dev-infra/commit-message/validate-file/validate-file.ts
Normal file
47
dev-infra/commit-message/validate-file/validate-file.ts
Normal file
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google LLC All Rights Reserved.
|
||||
*
|
||||
* 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';
|
||||
import {resolve} from 'path';
|
||||
|
||||
import {getRepoBaseDir} from '../../utils/config';
|
||||
import {error, green, info, log, red, yellow} from '../../utils/console';
|
||||
|
||||
import {deleteCommitMessageDraft, saveCommitMessageDraft} from '../commit-message-draft';
|
||||
import {printValidationErrors, validateCommitMessage} from '../validate';
|
||||
|
||||
/** Validate commit message at the provided file path. */
|
||||
export function validateFile(filePath: string, isErrorMode: boolean) {
|
||||
const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8');
|
||||
const {valid, errors} = validateCommitMessage(commitMessage);
|
||||
if (valid) {
|
||||
info(`${green('√')} Valid commit message`);
|
||||
deleteCommitMessageDraft(filePath);
|
||||
process.exitCode = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/** Function used to print to the console log. */
|
||||
let printFn = isErrorMode ? error : log;
|
||||
|
||||
printFn(`${isErrorMode ? red('✘') : yellow('!')} Invalid commit message`);
|
||||
printValidationErrors(errors, printFn);
|
||||
if (isErrorMode) {
|
||||
printFn(red('Aborting commit attempt due to invalid commit message.'));
|
||||
printFn(
|
||||
red('Commit message aborted as failure rather than warning due to local configuration.'));
|
||||
} else {
|
||||
printFn(yellow('Before this commit can be merged into the upstream repository, it must be'));
|
||||
printFn(yellow('amended to follow commit message guidelines.'));
|
||||
}
|
||||
|
||||
// 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);
|
||||
// Set the correct exit code based on if invalid commit message is an error.
|
||||
process.exitCode = isErrorMode ? 1 : 0;
|
||||
}
|
Reference in New Issue
Block a user