@@ -52,6 +52,66 @@ export enum VersionPolicyDefinitionName {
5252 'individualVersion'
5353}
5454
55+ /**
56+ * Updates the dependencies in the package json editor to values used for publishing, if needed.
57+ *
58+ * @returns the updated package json editor if the version format for publish is 'exact', otherwise undefined.
59+ */
60+ function updateDependenciesBeforePublish (
61+ packageName : string ,
62+ configuration : RushConfiguration ,
63+ versionFormatForPublish : VersionFormatForPublish
64+ ) : PackageJsonEditor | undefined {
65+ if ( versionFormatForPublish === 'exact' ) {
66+ const project : RushConfigurationProject = configuration . getProjectByName ( packageName ) ! ;
67+
68+ const packageJsonEditor : PackageJsonEditor = project . packageJsonEditor ;
69+
70+ for ( const dependency of packageJsonEditor . dependencyList ) {
71+ const rushDependencyProject : RushConfigurationProject | undefined = configuration . getProjectByName (
72+ dependency . name
73+ ) ;
74+
75+ if ( rushDependencyProject ) {
76+ const dependencyVersion : string = rushDependencyProject . packageJson . version ;
77+
78+ dependency . setVersion ( dependencyVersion ) ;
79+ }
80+ }
81+
82+ return packageJsonEditor ;
83+ }
84+ }
85+
86+ /**
87+ * Updates the dependencies in the package json editor to values used for checked-in source, if needed.
88+ *
89+ * @returns the updated package json editor if the version format for commit is 'wildcard', otherwise undefined.
90+ */
91+ function updateDependenciesBeforeCommit (
92+ packageName : string ,
93+ configuration : RushConfiguration ,
94+ versionFormatForCommit : VersionFormatForCommit
95+ ) : PackageJsonEditor | undefined {
96+ if ( versionFormatForCommit === 'wildcard' ) {
97+ const project : RushConfigurationProject = configuration . getProjectByName ( packageName ) ! ;
98+
99+ const packageJsonEditor : PackageJsonEditor = project . packageJsonEditor ;
100+
101+ for ( const dependency of packageJsonEditor . dependencyList ) {
102+ const rushDependencyProject : RushConfigurationProject | undefined = configuration . getProjectByName (
103+ dependency . name
104+ ) ;
105+
106+ if ( rushDependencyProject ) {
107+ dependency . setVersion ( '*' ) ;
108+ }
109+ }
110+
111+ return packageJsonEditor ;
112+ }
113+ }
114+
55115/**
56116 * This is the base class for version policy which controls how versions get bumped.
57117 * @public
@@ -161,53 +221,63 @@ export abstract class VersionPolicy {
161221 public abstract validate ( versionString : string , packageName : string ) : void ;
162222
163223 /**
164- * Tells the version policy to modify any dependencies in the target package
165- * to values used for publishing.
224+ * @deprecated Use {@link VersionPolicy.setDependenciesBeforePublishAsync} method instead.
166225 */
167226 public setDependenciesBeforePublish ( packageName : string , configuration : RushConfiguration ) : void {
168- if ( this . _versionFormatForPublish === 'exact' ) {
169- const project : RushConfigurationProject = configuration . getProjectByName ( packageName ) ! ;
227+ const packageJsonEditor : PackageJsonEditor | undefined = updateDependenciesBeforePublish (
228+ packageName ,
229+ configuration ,
230+ this . _versionFormatForPublish
231+ ) ;
170232
171- const packageJsonEditor : PackageJsonEditor = project . packageJsonEditor ;
233+ packageJsonEditor ?. saveIfModified ( ) ;
234+ }
172235
173- for ( const dependency of packageJsonEditor . dependencyList ) {
174- const rushDependencyProject : RushConfigurationProject | undefined = configuration . getProjectByName (
175- dependency . name
176- ) ;
236+ /**
237+ * Tells the version policy to modify any dependencies in the target package
238+ * to values used for publishing.
239+ */
240+ public async setDependenciesBeforePublishAsync (
241+ packageName : string ,
242+ configuration : RushConfiguration
243+ ) : Promise < void > {
244+ const packageJsonEditor : PackageJsonEditor | undefined = updateDependenciesBeforePublish (
245+ packageName ,
246+ configuration ,
247+ this . _versionFormatForPublish
248+ ) ;
177249
178- if ( rushDependencyProject ) {
179- const dependencyVersion : string = rushDependencyProject . packageJson . version ;
250+ await packageJsonEditor ?. saveIfModifiedAsync ( ) ;
251+ }
180252
181- dependency . setVersion ( dependencyVersion ) ;
182- }
183- }
253+ /**
254+ * @deprecated Use {@link VersionPolicy.setDependenciesBeforeCommitAsync} method instead.
255+ */
256+ public setDependenciesBeforeCommit ( packageName : string , configuration : RushConfiguration ) : void {
257+ const packageJsonEditor : PackageJsonEditor | undefined = updateDependenciesBeforeCommit (
258+ packageName ,
259+ configuration ,
260+ this . _versionFormatForCommit
261+ ) ;
184262
185- packageJsonEditor . saveIfModified ( ) ;
186- }
263+ packageJsonEditor ?. saveIfModified ( ) ;
187264 }
188265
189266 /**
190267 * Tells the version policy to modify any dependencies in the target package
191268 * to values used for checked-in source.
192269 */
193- public setDependenciesBeforeCommit ( packageName : string , configuration : RushConfiguration ) : void {
194- if ( this . _versionFormatForCommit === 'wildcard' ) {
195- const project : RushConfigurationProject = configuration . getProjectByName ( packageName ) ! ;
196-
197- const packageJsonEditor : PackageJsonEditor = project . packageJsonEditor ;
198-
199- for ( const dependency of packageJsonEditor . dependencyList ) {
200- const rushDependencyProject : RushConfigurationProject | undefined = configuration . getProjectByName (
201- dependency . name
202- ) ;
203-
204- if ( rushDependencyProject ) {
205- dependency . setVersion ( '*' ) ;
206- }
207- }
270+ public async setDependenciesBeforeCommitAsync (
271+ packageName : string ,
272+ configuration : RushConfiguration
273+ ) : Promise < void > {
274+ const packageJsonEditor : PackageJsonEditor | undefined = updateDependenciesBeforeCommit (
275+ packageName ,
276+ configuration ,
277+ this . _versionFormatForCommit
278+ ) ;
208279
209- packageJsonEditor . saveIfModified ( ) ;
210- }
280+ await packageJsonEditor ?. saveIfModifiedAsync ( ) ;
211281 }
212282}
213283
0 commit comments