@@ -17,6 +17,7 @@ import {
1717 ChangeType
1818} from '../../data/ChangeManagement' ;
1919import RushConfigurationProject from '../../data/RushConfigurationProject' ;
20+ import RushConfiguration from '../../data/RushConfiguration' ;
2021import Utilities from '../../utilities/Utilities' ;
2122import { execSync } from 'child_process' ;
2223import PrereleaseToken from './PrereleaseToken' ;
@@ -27,6 +28,19 @@ export interface IChangeInfoHash {
2728}
2829
2930export default class PublishUtilities {
31+ private static _rushConfiguration : RushConfiguration ;
32+
33+ public static get rushConfiguration ( ) : RushConfiguration {
34+ if ( ! PublishUtilities . _rushConfiguration ) {
35+ PublishUtilities . _rushConfiguration = RushConfiguration . loadFromDefaultLocation ( ) ;
36+ }
37+ return PublishUtilities . _rushConfiguration ;
38+ }
39+
40+ public static set rushConfiguration ( newConfiguration : RushConfiguration ) {
41+ PublishUtilities . _rushConfiguration = newConfiguration ;
42+ }
43+
3044 /**
3145 * Finds change requests in the given folder.
3246 * @param changesPath Path to the changes folder.
@@ -85,9 +99,10 @@ export default class PublishUtilities {
8599 if ( skipVersionBump ) {
86100 change . newVersion = pkg . version ;
87101 } else {
102+ // For hotfix changes, do not re-write new version
88103 change . newVersion = ( change . changeType ! >= ChangeType . patch ) ?
89104 semver . inc ( pkg . version , PublishUtilities . _getReleaseType ( change . changeType ! ) ) :
90- pkg . version ;
105+ ( change . changeType === ChangeType . hotfix ? change . newVersion : pkg . version ) ;
91106 }
92107
93108 if ( deps ) {
@@ -205,7 +220,6 @@ export default class PublishUtilities {
205220 ) : string {
206221 const currentDependencyVersion : string = dependencies [ dependencyName ] ;
207222 let newDependencyVersion : string ;
208-
209223 if ( PublishUtilities . isRangeDependency ( currentDependencyVersion ) ) {
210224 newDependencyVersion = PublishUtilities . _getNewRangeDependency ( newProjectVersion ) ;
211225 } else if ( currentDependencyVersion . lastIndexOf ( '~' , 0 ) === 0 ) {
@@ -226,6 +240,8 @@ export default class PublishUtilities {
226240 return 'minor' ;
227241 case ChangeType . patch :
228242 return 'patch' ;
243+ case ChangeType . hotfix :
244+ return 'prerelease' ;
229245 default :
230246 throw new Error ( `Wrong change type ${ changeType } ` ) ;
231247 }
@@ -353,7 +369,7 @@ export default class PublishUtilities {
353369 // For prelease, the newVersion needs to be appended with prerelease name.
354370 // And dependency should specify the specific prerelease version.
355371 dependencies [ depName ] = PublishUtilities . _getChangeInfoNewVersion ( depChange , prereleaseToken ) ;
356- } else if ( depChange && depChange . changeType ! >= ChangeType . patch ) {
372+ } else if ( depChange && depChange . changeType ! >= ChangeType . hotfix ) {
357373 PublishUtilities . _updateDependencyVersion (
358374 packageName ,
359375 dependencies ,
@@ -431,6 +447,13 @@ export default class PublishUtilities {
431447
432448 const oldChangeType : ChangeType = currentChange . changeType ! ;
433449
450+ if ( oldChangeType === ChangeType . hotfix && change . changeType ! > oldChangeType ) {
451+ throw new Error ( `Cannot apply ${ this . _getReleaseType ( change . changeType ! ) } change after hotfix on same package` ) ;
452+ }
453+ if ( change . changeType ! === ChangeType . hotfix && oldChangeType > change . changeType ! ) {
454+ throw new Error ( `Cannot apply hotfix alongside ${ this . _getReleaseType ( oldChangeType ! ) } change on same package` ) ;
455+ }
456+
434457 currentChange . changeType = Math . max ( currentChange . changeType ! , change . changeType ! ) ;
435458 currentChange . changes ! . push ( change ) ;
436459
@@ -444,10 +467,27 @@ export default class PublishUtilities {
444467 hasChanged = false ;
445468 currentChange . changeType = ChangeType . none ;
446469 } else {
447- currentChange . newVersion = change . changeType ! >= ChangeType . patch ?
448- semver . inc ( pkg . version , PublishUtilities . _getReleaseType ( currentChange . changeType ! ) ) :
449- pkg . version ;
450- currentChange . newRangeDependency = PublishUtilities . _getNewRangeDependency ( currentChange . newVersion ) ;
470+ if ( change . changeType === ChangeType . hotfix ) {
471+ const prereleaseComponents : string [ ] = semver . prerelease ( pkg . version ) ;
472+ if ( ! this . rushConfiguration . hotfixChangeEnabled ) {
473+ throw new Error ( `Cannot add hotfix change; hotfixChangeEnabled is false in configuration.` ) ;
474+ }
475+
476+ currentChange . newVersion = pkg . version ;
477+ if ( ! prereleaseComponents ) {
478+ currentChange . newVersion += '-hotfix' ;
479+ }
480+ currentChange . newVersion = semver . inc ( currentChange . newVersion , 'prerelease' ) ;
481+ } else {
482+ currentChange . newVersion = change . changeType ! >= ChangeType . patch ?
483+ semver . inc ( pkg . version , PublishUtilities . _getReleaseType ( currentChange . changeType ! ) ) :
484+ pkg . version ;
485+ }
486+
487+ // If hotfix change, force new range dependency to be the exact new version
488+ currentChange . newRangeDependency = change . changeType === ChangeType . hotfix ?
489+ currentChange . newVersion :
490+ PublishUtilities . _getNewRangeDependency ( currentChange . newVersion ) ;
451491 }
452492 return hasChanged ;
453493 }
@@ -465,7 +505,7 @@ export default class PublishUtilities {
465505
466506 // Iterate through all downstream dependencies for the package.
467507 if ( downstreamNames ) {
468- if ( ( change . changeType ! >= ChangeType . patch ) ||
508+ if ( ( change . changeType ! >= ChangeType . hotfix ) ||
469509 ( prereleaseToken && prereleaseToken . hasValue ) ) {
470510 for ( const depName of downstreamNames ) {
471511 const pkg : IPackageJson = allPackages . get ( depName ) ! . packageJson ;
@@ -497,11 +537,17 @@ export default class PublishUtilities {
497537 // If the version range exists and has not yet been updated to this version, update it.
498538 if ( requiredVersion !== change . newRangeDependency || alwaysUpdate ) {
499539
500- // Either it already satisfies the new version, or doesn't.
501- // If not, the downstream dep needs to be republished.
502- const changeType : ChangeType = semver . satisfies ( change . newVersion ! , requiredVersion ) ?
503- ChangeType . dependency :
504- ChangeType . patch ;
540+ let changeType : ChangeType ;
541+ // Propagate hotfix changes to dependencies
542+ if ( change . changeType === ChangeType . hotfix ) {
543+ changeType = ChangeType . hotfix ;
544+ } else {
545+ // Either it already satisfies the new version, or doesn't.
546+ // If not, the downstream dep needs to be republished.
547+ changeType = semver . satisfies ( change . newVersion ! , requiredVersion ) ?
548+ ChangeType . dependency :
549+ ChangeType . patch ;
550+ }
505551
506552 const hasChanged : boolean = PublishUtilities . _addChange ( {
507553 packageName : parentPackageName ,
@@ -545,8 +591,8 @@ export default class PublishUtilities {
545591 packageName : packageName ,
546592 changeType : ChangeType . dependency ,
547593 comment :
548- `Updating dependency "${ dependencyName } " from \`${ currentDependencyVersion } \`` +
549- ` to \`${ dependencies [ dependencyName ] } \``
594+ `Updating dependency "${ dependencyName } " from \`${ currentDependencyVersion } \`` +
595+ ` to \`${ dependencies [ dependencyName ] } \``
550596 } ,
551597 allChanges ,
552598 allPackages
0 commit comments