Skip to content

Commit beb0449

Browse files
iclantonclaude
andauthored
[rush-lib] Add async APIs for disk-touching methods in PackageJsonEditor, CommonVersionsConfiguration, and VersionPolicy (microsoft#5724)
* Refactor PackageJsonEditor. * fixup! Refactor PackageJsonEditor. * fixup! Refactor PackageJsonEditor. * Add changelog entry for PackageJsonEditor async API refactor. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Update a downstream project to use the async APIs. --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9938fd5 commit beb0449

19 files changed

+441
-232
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Add async variants of disk-touching APIs in `PackageJsonEditor` (`loadAsync`, `saveIfModifiedAsync`), `CommonVersionsConfiguration` (`loadFromFileAsync`, `saveAsync`), and `VersionPolicy` (`setDependenciesBeforePublishAsync`, `setDependenciesBeforeCommitAsync`); deprecate corresponding sync methods.",
5+
"type": "minor",
6+
"packageName": "@microsoft/rush"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "iclanton@users.noreply.github.com"
11+
}

common/reviews/api/rush-lib.api.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,13 @@ export class CommonVersionsConfiguration {
137137
getAllPreferredVersions(): Map<string, string>;
138138
getPreferredVersionsHash(): string;
139139
readonly implicitlyPreferredVersions: boolean | undefined;
140+
// @deprecated (undocumented)
140141
static loadFromFile(jsonFilePath: string, rushConfiguration?: RushConfiguration): CommonVersionsConfiguration;
142+
static loadFromFileAsync(jsonFilePath: string, rushConfiguration?: RushConfiguration): Promise<CommonVersionsConfiguration>;
141143
readonly preferredVersions: Map<string, string>;
144+
// @deprecated (undocumented)
142145
save(): boolean;
146+
saveAsync(): Promise<boolean>;
143147
}
144148

145149
export { CredentialCache }
@@ -1097,15 +1101,19 @@ export class PackageJsonEditor {
10971101
readonly filePath: string;
10981102
// (undocumented)
10991103
static fromObject(object: IPackageJson, filename: string): PackageJsonEditor;
1100-
// (undocumented)
1104+
// @deprecated (undocumented)
11011105
static load(filePath: string): PackageJsonEditor;
11021106
// (undocumented)
1107+
static loadAsync(filePath: string): Promise<PackageJsonEditor>;
1108+
// (undocumented)
11031109
get name(): string;
11041110
// (undocumented)
11051111
removeDependency(packageName: string, dependencyType: DependencyType): void;
11061112
get resolutionsList(): ReadonlyArray<PackageJsonDependency>;
1107-
// (undocumented)
1113+
// @deprecated (undocumented)
11081114
saveIfModified(): boolean;
1115+
// (undocumented)
1116+
saveIfModifiedAsync(): Promise<boolean>;
11091117
saveToObject(): IPackageJson;
11101118
// (undocumented)
11111119
tryGetDependency(packageName: string): PackageJsonDependency | undefined;
@@ -1648,8 +1656,12 @@ export abstract class VersionPolicy {
16481656
// @internal
16491657
static load(versionPolicyJson: IVersionPolicyJson): VersionPolicy | undefined;
16501658
get policyName(): string;
1659+
// @deprecated (undocumented)
16511660
setDependenciesBeforeCommit(packageName: string, configuration: RushConfiguration): void;
1661+
setDependenciesBeforeCommitAsync(packageName: string, configuration: RushConfiguration): Promise<void>;
1662+
// @deprecated (undocumented)
16521663
setDependenciesBeforePublish(packageName: string, configuration: RushConfiguration): void;
1664+
setDependenciesBeforePublishAsync(packageName: string, configuration: RushConfiguration): Promise<void>;
16531665
abstract validate(versionString: string, packageName: string): void;
16541666
}
16551667

libraries/rush-lib/src/api/CommonVersionsConfiguration.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,42 @@ export class CommonVersionsConfiguration {
186186
}
187187

188188
/**
189-
* Loads the common-versions.json data from the specified file path.
190-
* If the file has not been created yet, then an empty object is returned.
189+
* @deprecated Use {@link CommonVersionsConfiguration.loadFromFileAsync} method instead.
191190
*/
192191
public static loadFromFile(
193192
jsonFilePath: string,
194193
rushConfiguration?: RushConfiguration
195194
): CommonVersionsConfiguration {
196195
let commonVersionsJson: ICommonVersionsJson | undefined = undefined;
197-
198-
if (FileSystem.exists(jsonFilePath)) {
196+
try {
199197
commonVersionsJson = JsonFile.loadAndValidate(jsonFilePath, CommonVersionsConfiguration._jsonSchema);
198+
} catch (error) {
199+
if (!FileSystem.isNotExistError(error)) {
200+
throw error;
201+
}
202+
}
203+
204+
return new CommonVersionsConfiguration(commonVersionsJson, jsonFilePath, rushConfiguration);
205+
}
206+
207+
/**
208+
* Loads the common-versions.json data from the specified file path.
209+
* If the file has not been created yet, then an empty object is returned.
210+
*/
211+
public static async loadFromFileAsync(
212+
jsonFilePath: string,
213+
rushConfiguration?: RushConfiguration
214+
): Promise<CommonVersionsConfiguration> {
215+
let commonVersionsJson: ICommonVersionsJson | undefined = undefined;
216+
try {
217+
commonVersionsJson = await JsonFile.loadAndValidateAsync(
218+
jsonFilePath,
219+
CommonVersionsConfiguration._jsonSchema
220+
);
221+
} catch (error) {
222+
if (!FileSystem.isNotExistError(error)) {
223+
throw error;
224+
}
200225
}
201226

202227
return new CommonVersionsConfiguration(commonVersionsJson, jsonFilePath, rushConfiguration);
@@ -242,7 +267,7 @@ export class CommonVersionsConfiguration {
242267
}
243268

244269
/**
245-
* Writes the "common-versions.json" file to disk, using the filename that was passed to loadFromFile().
270+
* @deprecated Use {@link CommonVersionsConfiguration.saveAsync} method instead.
246271
*/
247272
public save(): boolean {
248273
if (this._modified) {
@@ -257,6 +282,22 @@ export class CommonVersionsConfiguration {
257282
return false;
258283
}
259284

285+
/**
286+
* Writes the "common-versions.json" file to disk, using the filename that was passed to loadFromFile().
287+
*/
288+
public async saveAsync(): Promise<boolean> {
289+
if (this._modified) {
290+
await JsonFile.saveAsync(this._serialize(), this.filePath, {
291+
updateExistingFile: true,
292+
ignoreUndefinedValues: true
293+
});
294+
this._modified = false;
295+
return true;
296+
}
297+
298+
return false;
299+
}
300+
260301
/**
261302
* Returns preferredVersions.
262303
*/

0 commit comments

Comments
 (0)