Skip to content

Commit 0f3f805

Browse files
author
Ben Keen
committed
Apply code review feedback
1 parent e8d5481 commit 0f3f805

7 files changed

Lines changed: 45 additions & 26 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ export class PnpmOptionsConfiguration extends PackageManagerOptionsConfiguration
11771177
readonly strictPeerDependencies: boolean;
11781178
readonly unsupportedPackageJsonSettings: unknown | undefined;
11791179
updateGlobalCatalogs(catalogs: Record<string, Record<string, string>> | undefined): void;
1180-
updateGlobalOnlyBuiltDependencies(onlyBuiltDependencies: string[] | undefined): void;
1180+
updateGlobalOnlyBuiltDependenciesAsync(onlyBuiltDependencies: string[] | undefined): Promise<void>;
11811181
updateGlobalPatchedDependencies(patchedDependencies: Record<string, string> | undefined): void;
11821182
readonly useWorkspaces: boolean;
11831183
}

libraries/rush-lib/src/cli/RushPnpmCommandLineParser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ export class RushPnpmCommandLineParser {
584584

585585
if (!Objects.areDeepEqual(currentGlobalOnlyBuiltDependencies, newGlobalOnlyBuiltDependencies)) {
586586
// Update onlyBuiltDependencies to pnpm configuration file
587-
pnpmOptions?.updateGlobalOnlyBuiltDependencies(newGlobalOnlyBuiltDependencies);
587+
await pnpmOptions?.updateGlobalOnlyBuiltDependenciesAsync(newGlobalOnlyBuiltDependencies);
588588

589589
// Rerun installation to update
590590
await this._doRushUpdateAsync();
@@ -605,7 +605,7 @@ export class RushPnpmCommandLineParser {
605605

606606
const workspaceYamlFilename: string = path.join(subspaceTempFolder, 'pnpm-workspace.yaml');
607607
const newCatalogs: Record<string, Record<string, string>> | undefined =
608-
PnpmWorkspaceFile.loadCatalogsFromFile(workspaceYamlFilename);
608+
await PnpmWorkspaceFile.loadCatalogsFromFileAsync(workspaceYamlFilename);
609609
const currentCatalogs: Record<string, Record<string, string>> | undefined =
610610
pnpmOptions.globalCatalogs;
611611

libraries/rush-lib/src/cli/test/RushPnpmCommandLineParser.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ catalogs:
6060
}
6161
});
6262

63-
it('syncs updated catalogs from pnpm-workspace.yaml to pnpm-config.json', () => {
63+
it('syncs updated catalogs from pnpm-workspace.yaml to pnpm-config.json', async () => {
6464
const updatedWorkspaceYaml = `packages:
6565
- '../../apps/*'
6666
catalogs:
@@ -88,7 +88,7 @@ catalogs:
8888
});
8989

9090
const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile');
91-
const newCatalogs = PnpmWorkspaceFile.loadCatalogsFromFile(pnpmWorkspacePath);
91+
const newCatalogs = await PnpmWorkspaceFile.loadCatalogsFromFileAsync(pnpmWorkspacePath);
9292

9393
pnpmOptions?.updateGlobalCatalogs(newCatalogs);
9494

@@ -110,9 +110,9 @@ catalogs:
110110
});
111111
});
112112

113-
it('does not update pnpm-config.json when catalogs are unchanged', () => {
113+
it('does not update pnpm-config.json when catalogs are unchanged', async () => {
114114
const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile');
115-
const newCatalogs = PnpmWorkspaceFile.loadCatalogsFromFile(pnpmWorkspacePath);
115+
const newCatalogs = await PnpmWorkspaceFile.loadCatalogsFromFileAsync(pnpmWorkspacePath);
116116

117117
const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile(
118118
path.join(testRepoPath, 'rush.json')
@@ -131,14 +131,14 @@ catalogs:
131131
});
132132
});
133133

134-
it('removes catalogs when pnpm-workspace.yaml has no catalogs', () => {
134+
it('removes catalogs when pnpm-workspace.yaml has no catalogs', async () => {
135135
const workspaceWithoutCatalogs = `packages:
136136
- '../../apps/*'
137137
`;
138138
FileSystem.writeFile(pnpmWorkspacePath, workspaceWithoutCatalogs);
139139

140140
const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile');
141-
const newCatalogs = PnpmWorkspaceFile.loadCatalogsFromFile(pnpmWorkspacePath);
141+
const newCatalogs = await PnpmWorkspaceFile.loadCatalogsFromFileAsync(pnpmWorkspacePath);
142142

143143
expect(newCatalogs).toBeUndefined();
144144

libraries/rush-lib/src/logic/pnpm/PnpmOptionsConfiguration.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,16 @@ export class PnpmOptionsConfiguration extends PackageManagerOptionsConfiguration
552552
/**
553553
* Updates globalOnlyBuiltDependencies field of the PNPM options in the common/config/rush/pnpm-config.json file.
554554
*/
555-
public updateGlobalOnlyBuiltDependencies(onlyBuiltDependencies: string[] | undefined): void {
555+
public async updateGlobalOnlyBuiltDependenciesAsync(
556+
onlyBuiltDependencies: string[] | undefined
557+
): Promise<void> {
556558
if (onlyBuiltDependencies === undefined) {
557559
delete this._json.globalOnlyBuiltDependencies;
558560
} else {
559561
this._json.globalOnlyBuiltDependencies = onlyBuiltDependencies;
560562
}
561563
if (this.jsonFilename) {
562-
JsonFile.save(this._json, this.jsonFilename, { updateExistingFile: true });
564+
await JsonFile.saveAsync(this._json, this.jsonFilename, { updateExistingFile: true });
563565
}
564566
}
565567

libraries/rush-lib/src/logic/pnpm/PnpmWorkspaceFile.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ export class PnpmWorkspaceFile extends BaseWorkspaceFile {
6464
* @param workspaceYamlFilename - The path to the pnpm-workspace.yaml file
6565
* @returns The catalogs object, or undefined if the file doesn't exist or has no catalogs
6666
*/
67-
public static loadCatalogsFromFile(
67+
public static async loadCatalogsFromFileAsync(
6868
workspaceYamlFilename: string
69-
): Record<string, Record<string, string>> | undefined {
70-
if (!FileSystem.exists(workspaceYamlFilename)) {
69+
): Promise<Record<string, Record<string, string>> | undefined> {
70+
if (!(await FileSystem.existsAsync(workspaceYamlFilename))) {
7171
return undefined;
7272
}
73-
const content: string = FileSystem.readFile(workspaceYamlFilename);
73+
const content: string = await FileSystem.readFileAsync(workspaceYamlFilename);
7474
const parsed: IPnpmWorkspaceYaml | undefined = yamlModule.load(content) as IPnpmWorkspaceYaml | undefined;
7575

7676
if (!parsed || !parsed.catalogs) {

libraries/rush-lib/src/logic/pnpm/test/PnpmOptionsConfiguration.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ describe(PnpmOptionsConfiguration.name, () => {
294294
}
295295
});
296296

297-
it('handles undefined in updateGlobalOnlyBuiltDependencies', () => {
297+
it('handles undefined in updateGlobalOnlyBuiltDependenciesAsync', async () => {
298298
const testConfigPath: string = path.join(__dirname, 'temp', 'pnpm-config-undefined-test.json');
299299
const tempDir: string = path.dirname(testConfigPath);
300300
FileSystem.ensureFolder(tempDir);
@@ -316,9 +316,9 @@ describe(PnpmOptionsConfiguration.name, () => {
316316
'esbuild'
317317
]);
318318

319-
expect(() => {
320-
pnpmConfiguration.updateGlobalOnlyBuiltDependencies(undefined);
321-
}).not.toThrow();
319+
await expect(
320+
pnpmConfiguration.updateGlobalOnlyBuiltDependenciesAsync(undefined)
321+
).resolves.not.toThrow();
322322

323323
const reloadedConfig: PnpmOptionsConfiguration = PnpmOptionsConfiguration.loadFromJsonFileOrThrow(
324324
testConfigPath,

libraries/rush-lib/src/logic/pnpm/test/PnpmWorkspaceFile.test.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ describe(PnpmWorkspaceFile.name, () => {
1212

1313
let mockWriteFile: jest.SpyInstance;
1414
let mockReadFile: jest.SpyInstance;
15+
let mockReadFileAsync: jest.SpyInstance;
1516
let mockExists: jest.SpyInstance;
17+
let mockExistsAsync: jest.SpyInstance;
1618
let writtenContent: string | undefined;
1719

1820
beforeEach(() => {
@@ -34,16 +36,31 @@ describe(PnpmWorkspaceFile.name, () => {
3436
return writtenContent;
3537
});
3638

39+
// Mock async version for loadCatalogsFromFileAsync
40+
mockReadFileAsync = jest.spyOn(FileSystem, 'readFileAsync').mockImplementation(async () => {
41+
if (writtenContent === undefined) {
42+
throw new Error('File not found');
43+
}
44+
return writtenContent;
45+
});
46+
3747
// Mock FileSystem.exists to return true if content was written
3848
mockExists = jest.spyOn(FileSystem, 'exists').mockImplementation(() => {
3949
return writtenContent !== undefined;
4050
});
51+
52+
// Mock async version for loadCatalogsFromFileAsync
53+
mockExistsAsync = jest.spyOn(FileSystem, 'existsAsync').mockImplementation(async () => {
54+
return writtenContent !== undefined;
55+
});
4156
});
4257

4358
afterEach(() => {
4459
mockWriteFile.mockRestore();
4560
mockReadFile.mockRestore();
61+
mockReadFileAsync.mockRestore();
4662
mockExists.mockRestore();
63+
mockExistsAsync.mockRestore();
4764
});
4865

4966
describe('basic functionality', () => {
@@ -181,23 +198,23 @@ describe(PnpmWorkspaceFile.name, () => {
181198
});
182199
});
183200

184-
describe('loadCatalogsFromFile', () => {
185-
it('returns undefined for non-existent file', () => {
201+
describe('loadCatalogsFromFileAsync', () => {
202+
it('returns undefined for non-existent file', async () => {
186203
const nonExistentFile: string = path.join(tempDir, 'non-existent.yaml');
187-
const catalogs = PnpmWorkspaceFile.loadCatalogsFromFile(nonExistentFile);
204+
const catalogs = await PnpmWorkspaceFile.loadCatalogsFromFileAsync(nonExistentFile);
188205
expect(catalogs).toBeUndefined();
189206
});
190207

191-
it('returns undefined when file has no catalogs', () => {
208+
it('returns undefined when file has no catalogs', async () => {
192209
const workspaceFile: PnpmWorkspaceFile = new PnpmWorkspaceFile(workspaceFilePath);
193210
workspaceFile.addPackage(path.join(projectsDir, 'app1'));
194211
workspaceFile.save(workspaceFilePath, { onlyIfChanged: true });
195212

196-
const catalogs = PnpmWorkspaceFile.loadCatalogsFromFile(workspaceFilePath);
213+
const catalogs = await PnpmWorkspaceFile.loadCatalogsFromFileAsync(workspaceFilePath);
197214
expect(catalogs).toBeUndefined();
198215
});
199216

200-
it('loads catalogs from existing file', () => {
217+
it('loads catalogs from existing file', async () => {
201218
const workspaceFile: PnpmWorkspaceFile = new PnpmWorkspaceFile(workspaceFilePath);
202219
workspaceFile.addPackage(path.join(projectsDir, 'app1'));
203220
workspaceFile.setCatalogs({
@@ -211,7 +228,7 @@ describe(PnpmWorkspaceFile.name, () => {
211228
});
212229
workspaceFile.save(workspaceFilePath, { onlyIfChanged: true });
213230

214-
const catalogs = PnpmWorkspaceFile.loadCatalogsFromFile(workspaceFilePath);
231+
const catalogs = await PnpmWorkspaceFile.loadCatalogsFromFileAsync(workspaceFilePath);
215232
expect(catalogs).toEqual({
216233
default: {
217234
react: '^18.0.0',

0 commit comments

Comments
 (0)