From a53d820a4afb0fc27e5bc578722cbf5a74bc33b0 Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Mon, 23 May 2022 20:52:47 +0000 Subject: [PATCH] refactor(ng-dev/pr): Migrate discover-new-conflicts and rebase commands to CommandModules Use CommandModules for discover-new-conflicts and rebase commands to better align with how all of the other subcommands are defined. --- ng-dev/pr/cli.ts | 21 ++++----------------- ng-dev/pr/discover-new-conflicts/cli.ts | 21 ++++++++++++--------- ng-dev/pr/rebase/cli.ts | 19 ++++++++++++------- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/ng-dev/pr/cli.ts b/ng-dev/pr/cli.ts index cd3b1bd5c..7a24a6eaa 100644 --- a/ng-dev/pr/cli.ts +++ b/ng-dev/pr/cli.ts @@ -10,12 +10,9 @@ import * as yargs from 'yargs'; import {CheckTargetBranchesModule} from './check-target-branches/cli'; import {CheckoutCommandModule} from './checkout/cli'; -import { - buildDiscoverNewConflictsCommand, - handleDiscoverNewConflictsCommand, -} from './discover-new-conflicts/cli'; +import {DiscoverNewConflictsCommandModule} from './discover-new-conflicts/cli'; import {MergeCommandModule} from './merge/cli'; -import {buildRebaseCommand, handleRebaseCommand} from './rebase/cli'; +import {RebaseCommandModule} from './rebase/cli'; /** Build the parser for pull request commands. */ export function buildPrParser(localYargs: yargs.Argv) { @@ -23,18 +20,8 @@ export function buildPrParser(localYargs: yargs.Argv) { .help() .strict() .demandCommand() - .command( - 'discover-new-conflicts ', - 'Check if a pending PR causes new conflicts for other pending PRs', - buildDiscoverNewConflictsCommand, - handleDiscoverNewConflictsCommand, - ) - .command( - 'rebase ', - 'Rebase a pending PR and push the rebased commits back to Github', - buildRebaseCommand, - handleRebaseCommand, - ) + .command(DiscoverNewConflictsCommandModule) + .command(RebaseCommandModule) .command(MergeCommandModule) .command(CheckoutCommandModule) .command(CheckTargetBranchesModule); diff --git a/ng-dev/pr/discover-new-conflicts/cli.ts b/ng-dev/pr/discover-new-conflicts/cli.ts index 5df632253..70b07cd63 100644 --- a/ng-dev/pr/discover-new-conflicts/cli.ts +++ b/ng-dev/pr/discover-new-conflicts/cli.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Arguments, Argv} from 'yargs'; +import {Arguments, Argv, CommandModule} from 'yargs'; import {error} from '../../utils/console'; import {addGithubTokenOption} from '../../utils/git/github-yargs'; @@ -14,15 +14,13 @@ import {addGithubTokenOption} from '../../utils/git/github-yargs'; import {discoverNewConflictsForPr} from './index'; /** The options available to the discover-new-conflicts command via CLI. */ -export interface DiscoverNewConflictsCommandOptions { +export interface DiscoverNewConflictsOptions { date: number; pr: number; } /** Builds the discover-new-conflicts pull request command. */ -export function buildDiscoverNewConflictsCommand( - yargs: Argv, -): Argv { +function builder(yargs: Argv): Argv { return addGithubTokenOption(yargs) .option('date', { description: 'Only consider PRs updated since provided date', @@ -34,10 +32,7 @@ export function buildDiscoverNewConflictsCommand( } /** Handles the discover-new-conflicts pull request command. */ -export async function handleDiscoverNewConflictsCommand({ - pr, - date, -}: Arguments) { +async function handler({pr, date}: Arguments) { // If a provided date is not able to be parsed, yargs provides it as NaN. if (isNaN(date)) { error('Unable to parse the value provided via --date flag'); @@ -55,3 +50,11 @@ function getThirtyDaysAgoDate() { date.setDate(date.getDate() - 30); return date.getTime(); } + +/** yargs command module for discovering new conflicts for a PR */ +export const DiscoverNewConflictsCommandModule: CommandModule<{}, DiscoverNewConflictsOptions> = { + handler, + builder, + command: 'checkout ', + describe: 'Checkout a PR from the upstream repo', +}; diff --git a/ng-dev/pr/rebase/cli.ts b/ng-dev/pr/rebase/cli.ts index 4b08b45a6..ac9488b23 100644 --- a/ng-dev/pr/rebase/cli.ts +++ b/ng-dev/pr/rebase/cli.ts @@ -6,27 +6,32 @@ * found in the LICENSE file at https://angular.io/license */ -import {Arguments, Argv} from 'yargs'; +import {Arguments, Argv, CommandModule} from 'yargs'; import {addGithubTokenOption} from '../../utils/git/github-yargs'; import {rebasePr} from './index'; /** The options available to the rebase command via CLI. */ -export interface RebaseCommandOptions { +export interface RebaseOptions { githubToken: string; pr: number; } /** Builds the rebase pull request command. */ -export function buildRebaseCommand(yargs: Argv): Argv { +function builder(yargs: Argv): Argv { return addGithubTokenOption(yargs).positional('pr', {type: 'number', demandOption: true}); } /** Handles the rebase pull request command. */ -export async function handleRebaseCommand({ - pr, - githubToken, -}: Arguments) { +async function handler({pr, githubToken}: Arguments) { process.exitCode = await rebasePr(pr, githubToken); } + +/** yargs command module for rebaseing a PR */ +export const RebaseCommandModule: CommandModule<{}, RebaseOptions> = { + handler, + builder, + command: 'checkout ', + describe: 'Checkout a PR from the upstream repo', +};