From 1259fdcb42223b0f6e383236dd014900c7cdb232 Mon Sep 17 00:00:00 2001 From: Ian Clanton-Thuon Date: Fri, 20 Sep 2024 14:28:27 -0400 Subject: [PATCH] Grab the currently installed variant in a few cases. --- libraries/rush-lib/src/api/Variants.ts | 13 +++++++++---- libraries/rush-lib/src/cli/actions/AddAction.ts | 10 +++++++--- .../src/cli/actions/BaseAddAndRemoveAction.ts | 5 +++-- .../rush-lib/src/cli/actions/BaseInstallAction.ts | 8 ++++++-- libraries/rush-lib/src/cli/actions/CheckAction.ts | 8 ++++++-- libraries/rush-lib/src/cli/actions/InstallAction.ts | 8 ++++++-- libraries/rush-lib/src/cli/actions/RemoveAction.ts | 10 +++++++--- libraries/rush-lib/src/cli/actions/UpdateAction.ts | 8 ++++++-- .../src/cli/actions/UpgradeInteractiveAction.ts | 10 ++++++---- 9 files changed, 56 insertions(+), 24 deletions(-) diff --git a/libraries/rush-lib/src/api/Variants.ts b/libraries/rush-lib/src/api/Variants.ts index 57bb125c6a2..c2bfe8c56c5 100644 --- a/libraries/rush-lib/src/api/Variants.ts +++ b/libraries/rush-lib/src/api/Variants.ts @@ -17,14 +17,19 @@ export const VARIANT_PARAMETER: ICommandLineStringDefinition = { environmentVariable: EnvironmentVariableNames.RUSH_VARIANT }; -export function getVariant( +export async function getVariantAsync( variantsParameter: CommandLineStringParameter, - rushConfiguration: RushConfiguration -): string | undefined { - const variant: string | undefined = variantsParameter.value; + rushConfiguration: RushConfiguration, + defaultToCurrentlyInstalledVariant: boolean +): Promise { + let variant: string | undefined = variantsParameter.value; if (variant && !rushConfiguration.variants.has(variant)) { throw new Error(`The variant "${variant}" is not defined in ${RushConstants.rushJsonFilename}`); } + if (!variant && defaultToCurrentlyInstalledVariant) { + variant = await rushConfiguration.getCurrentlyInstalledVariantAsync(); + } + return variant; } diff --git a/libraries/rush-lib/src/cli/actions/AddAction.ts b/libraries/rush-lib/src/cli/actions/AddAction.ts index 2358f5f2e05..f0c2fe25593 100644 --- a/libraries/rush-lib/src/cli/actions/AddAction.ts +++ b/libraries/rush-lib/src/cli/actions/AddAction.ts @@ -17,7 +17,7 @@ import { type IPackageJsonUpdaterRushAddOptions, SemVerStyle } from '../../logic/PackageJsonUpdaterTypes'; -import { getVariant, VARIANT_PARAMETER } from '../../api/Variants'; +import { getVariantAsync, VARIANT_PARAMETER } from '../../api/Variants'; export class AddAction extends BaseAddAndRemoveAction { protected readonly _allFlag: CommandLineFlagParameter; @@ -94,7 +94,7 @@ export class AddAction extends BaseAddAndRemoveAction { this._variantParameter = this.defineStringParameter(VARIANT_PARAMETER); } - public getUpdateOptions(): IPackageJsonUpdaterRushAddOptions { + public async getUpdateOptionsAsync(): Promise { const projects: RushConfigurationProject[] = super.getProjects(); if (this._caretFlag.value && this._exactFlag.value) { @@ -157,7 +157,11 @@ export class AddAction extends BaseAddAndRemoveAction { packagesToAdd.push({ packageName, version, rangeStyle }); } - const variant: string | undefined = getVariant(this._variantParameter, this.rushConfiguration); + const variant: string | undefined = await getVariantAsync( + this._variantParameter, + this.rushConfiguration, + true + ); return { projects: projects, diff --git a/libraries/rush-lib/src/cli/actions/BaseAddAndRemoveAction.ts b/libraries/rush-lib/src/cli/actions/BaseAddAndRemoveAction.ts index 1398164a096..71b84b3cd39 100644 --- a/libraries/rush-lib/src/cli/actions/BaseAddAndRemoveAction.ts +++ b/libraries/rush-lib/src/cli/actions/BaseAddAndRemoveAction.ts @@ -54,7 +54,7 @@ export abstract class BaseAddAndRemoveAction extends BaseRushAction { }); } - protected abstract getUpdateOptions(): IPackageJsonUpdaterRushBaseUpdateOptions; + protected abstract getUpdateOptionsAsync(): Promise; protected getProjects(): RushConfigurationProject[] { if (this._allFlag.value) { @@ -83,6 +83,7 @@ export abstract class BaseAddAndRemoveAction extends BaseRushAction { this.rushGlobalFolder ); - await updater.doRushUpdateAsync(this.getUpdateOptions()); + const updateOptions: IPackageJsonUpdaterRushBaseUpdateOptions = await this.getUpdateOptionsAsync(); + await updater.doRushUpdateAsync(updateOptions); } } diff --git a/libraries/rush-lib/src/cli/actions/BaseInstallAction.ts b/libraries/rush-lib/src/cli/actions/BaseInstallAction.ts index 72666805f78..82865908a69 100644 --- a/libraries/rush-lib/src/cli/actions/BaseInstallAction.ts +++ b/libraries/rush-lib/src/cli/actions/BaseInstallAction.ts @@ -23,7 +23,7 @@ import { RushConstants } from '../../logic/RushConstants'; import { SUBSPACE_LONG_ARG_NAME, type SelectionParameterSet } from '../parsing/SelectionParameterSet'; import type { RushConfigurationProject } from '../../api/RushConfigurationProject'; import type { Subspace } from '../../api/Subspace'; -import { getVariant, VARIANT_PARAMETER } from '../../api/Variants'; +import { getVariantAsync, VARIANT_PARAMETER } from '../../api/Variants'; /** * Temporary data structure used by `BaseInstallAction.runAsync()` @@ -169,7 +169,11 @@ export abstract class BaseInstallAction extends BaseRushAction { } } - const variant: string | undefined = getVariant(this._variantParameter, this.rushConfiguration); + const variant: string | undefined = await getVariantAsync( + this._variantParameter, + this.rushConfiguration, + false + ); if (selectedSubspaces) { // Check each subspace for version inconsistencies for (const subspace of selectedSubspaces) { diff --git a/libraries/rush-lib/src/cli/actions/CheckAction.ts b/libraries/rush-lib/src/cli/actions/CheckAction.ts index 0484995baf3..2a4d769bfab 100644 --- a/libraries/rush-lib/src/cli/actions/CheckAction.ts +++ b/libraries/rush-lib/src/cli/actions/CheckAction.ts @@ -7,7 +7,7 @@ import { Colorize, type ITerminal } from '@rushstack/terminal'; import type { RushCommandLineParser } from '../RushCommandLineParser'; import { BaseRushAction } from './BaseRushAction'; import { VersionMismatchFinder } from '../../logic/versionMismatch/VersionMismatchFinder'; -import { getVariant, VARIANT_PARAMETER } from '../../api/Variants'; +import { getVariantAsync, VARIANT_PARAMETER } from '../../api/Variants'; export class CheckAction extends BaseRushAction { private readonly _terminal: ITerminal; @@ -60,7 +60,11 @@ export class CheckAction extends BaseRushAction { const currentlyInstalledVariant: string | undefined = await this.rushConfiguration.getCurrentlyInstalledVariantAsync(); - const variant: string | undefined = getVariant(this._variantParameter, this.rushConfiguration); + const variant: string | undefined = await getVariantAsync( + this._variantParameter, + this.rushConfiguration, + true + ); if (!variant && currentlyInstalledVariant) { this._terminal.writeWarningLine( Colorize.yellow( diff --git a/libraries/rush-lib/src/cli/actions/InstallAction.ts b/libraries/rush-lib/src/cli/actions/InstallAction.ts index a8c843d7449..945c12337ca 100644 --- a/libraries/rush-lib/src/cli/actions/InstallAction.ts +++ b/libraries/rush-lib/src/cli/actions/InstallAction.ts @@ -9,7 +9,7 @@ import type { RushCommandLineParser } from '../RushCommandLineParser'; import { SelectionParameterSet } from '../parsing/SelectionParameterSet'; import type { RushConfigurationProject } from '../../api/RushConfigurationProject'; import type { Subspace } from '../../api/Subspace'; -import { getVariant } from '../../api/Variants'; +import { getVariantAsync } from '../../api/Variants'; export class InstallAction extends BaseInstallAction { private readonly _checkOnlyParameter: CommandLineFlagParameter; @@ -62,7 +62,11 @@ export class InstallAction extends BaseInstallAction { (await this._selectionParameters?.getSelectedProjectsAsync(this._terminal)) ?? new Set(this.rushConfiguration.projects); - const variant: string | undefined = getVariant(this._variantParameter, this.rushConfiguration); + const variant: string | undefined = await getVariantAsync( + this._variantParameter, + this.rushConfiguration, + false + ); return { debug: this.parser.isDebug, diff --git a/libraries/rush-lib/src/cli/actions/RemoveAction.ts b/libraries/rush-lib/src/cli/actions/RemoveAction.ts index cd49dfecff0..7eb3358f8b0 100644 --- a/libraries/rush-lib/src/cli/actions/RemoveAction.ts +++ b/libraries/rush-lib/src/cli/actions/RemoveAction.ts @@ -15,7 +15,7 @@ import type { IPackageForRushRemove, IPackageJsonUpdaterRushRemoveOptions } from '../../logic/PackageJsonUpdaterTypes'; -import { getVariant, VARIANT_PARAMETER } from '../../api/Variants'; +import { getVariantAsync, VARIANT_PARAMETER } from '../../api/Variants'; export class RemoveAction extends BaseAddAndRemoveAction { protected readonly _allFlag: CommandLineFlagParameter; @@ -54,7 +54,7 @@ export class RemoveAction extends BaseAddAndRemoveAction { this._variantParameter = this.defineStringParameter(VARIANT_PARAMETER); } - public getUpdateOptions(): IPackageJsonUpdaterRushRemoveOptions { + public async getUpdateOptionsAsync(): Promise { const projects: RushConfigurationProject[] = super.getProjects(); const packagesToRemove: IPackageForRushRemove[] = []; @@ -83,7 +83,11 @@ export class RemoveAction extends BaseAddAndRemoveAction { packagesToRemove.push({ packageName }); } - const variant: string | undefined = getVariant(this._variantParameter, this.rushConfiguration); + const variant: string | undefined = await getVariantAsync( + this._variantParameter, + this.rushConfiguration, + true + ); return { projects: projects, diff --git a/libraries/rush-lib/src/cli/actions/UpdateAction.ts b/libraries/rush-lib/src/cli/actions/UpdateAction.ts index 3bea78d0aa1..837ce149c17 100644 --- a/libraries/rush-lib/src/cli/actions/UpdateAction.ts +++ b/libraries/rush-lib/src/cli/actions/UpdateAction.ts @@ -9,7 +9,7 @@ import type { RushCommandLineParser } from '../RushCommandLineParser'; import { SelectionParameterSet } from '../parsing/SelectionParameterSet'; import type { RushConfigurationProject } from '../../api/RushConfigurationProject'; import type { Subspace } from '../../api/Subspace'; -import { getVariant } from '../../api/Variants'; +import { getVariantAsync } from '../../api/Variants'; export class UpdateAction extends BaseInstallAction { private readonly _fullParameter: CommandLineFlagParameter; @@ -85,7 +85,11 @@ export class UpdateAction extends BaseInstallAction { (await this._selectionParameters?.getSelectedProjectsAsync(this._terminal)) ?? new Set(this.rushConfiguration.projects); - const variant: string | undefined = getVariant(this._variantParameter, this.rushConfiguration); + const variant: string | undefined = await getVariantAsync( + this._variantParameter, + this.rushConfiguration, + false + ); return { debug: this.parser.isDebug, diff --git a/libraries/rush-lib/src/cli/actions/UpgradeInteractiveAction.ts b/libraries/rush-lib/src/cli/actions/UpgradeInteractiveAction.ts index ffc1468f7c8..4c1666b28a6 100644 --- a/libraries/rush-lib/src/cli/actions/UpgradeInteractiveAction.ts +++ b/libraries/rush-lib/src/cli/actions/UpgradeInteractiveAction.ts @@ -7,7 +7,7 @@ import { BaseRushAction } from './BaseRushAction'; import type * as PackageJsonUpdaterType from '../../logic/PackageJsonUpdater'; import type * as InteractiveUpgraderType from '../../logic/InteractiveUpgrader'; -import { getVariant, VARIANT_PARAMETER } from '../../api/Variants'; +import { getVariantAsync, VARIANT_PARAMETER } from '../../api/Variants'; export class UpgradeInteractiveAction extends BaseRushAction { private _makeConsistentFlag: CommandLineFlagParameter; @@ -62,9 +62,11 @@ export class UpgradeInteractiveAction extends BaseRushAction { this.rushConfiguration ); - const variant: string | undefined = - getVariant(this._variantParameter, this.rushConfiguration) ?? - (await this.rushConfiguration.getCurrentlyInstalledVariantAsync()); + const variant: string | undefined = await getVariantAsync( + this._variantParameter, + this.rushConfiguration, + true + ); const shouldMakeConsistent: boolean = this.rushConfiguration.defaultSubspace.shouldEnsureConsistentVersions(variant) || this._makeConsistentFlag.value;