-
Notifications
You must be signed in to change notification settings - Fork 672
Add 'rush-pnpm up' support for catalogs #5585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
benkeen
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
benkeen:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@microsoft/rush", | ||
| "comment": "Add catalog support to `rush-pnpm update`.", | ||
| "type": "none" | ||
| } | ||
| ], | ||
| "packageName": "@microsoft/rush" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
libraries/rush-lib/src/cli/test/RushPnpmCommandLineParser.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import * as path from 'node:path'; | ||
| import { FileSystem, JsonFile } from '@rushstack/node-core-library'; | ||
| import { TestUtilities } from '@rushstack/heft-config-file'; | ||
| import { RushConfiguration } from '../../api/RushConfiguration'; | ||
|
|
||
| describe('RushPnpmCommandLineParser', () => { | ||
| describe('catalog syncing', () => { | ||
| let testRepoPath: string; | ||
| let pnpmConfigPath: string; | ||
| let pnpmWorkspacePath: string; | ||
|
|
||
| beforeEach(() => { | ||
| testRepoPath = path.join(__dirname, 'temp', 'catalog-sync-test-repo'); | ||
| FileSystem.ensureFolder(testRepoPath); | ||
|
|
||
| const rushJsonPath: string = path.join(testRepoPath, 'rush.json'); | ||
| const rushJson = { | ||
| $schema: 'https://developer.microsoft.com/json-schemas/rush/v5/rush.schema.json', | ||
| rushVersion: '5.166.0', | ||
| pnpmVersion: '10.28.1', | ||
| nodeSupportedVersionRange: '>=18.0.0', | ||
| projects: [] | ||
| }; | ||
| JsonFile.save(rushJson, rushJsonPath, { ensureFolderExists: true }); | ||
|
|
||
| const configDir: string = path.join(testRepoPath, 'common', 'config', 'rush'); | ||
| FileSystem.ensureFolder(configDir); | ||
|
|
||
| pnpmConfigPath = path.join(configDir, 'pnpm-config.json'); | ||
| const pnpmConfig = { | ||
| globalCatalogs: { | ||
| default: { | ||
| react: '^18.0.0', | ||
| 'react-dom': '^18.0.0' | ||
| } | ||
| } | ||
| }; | ||
| JsonFile.save(pnpmConfig, pnpmConfigPath); | ||
|
|
||
| const tempDir: string = path.join(testRepoPath, 'common', 'temp'); | ||
| FileSystem.ensureFolder(tempDir); | ||
|
|
||
| pnpmWorkspacePath = path.join(tempDir, 'pnpm-workspace.yaml'); | ||
| const workspaceYaml = `packages: | ||
| - '../../apps/*' | ||
| catalogs: | ||
| default: | ||
| react: ^18.0.0 | ||
| react-dom: ^18.0.0 | ||
| `; | ||
| FileSystem.writeFile(pnpmWorkspacePath, workspaceYaml); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (FileSystem.exists(testRepoPath)) { | ||
| FileSystem.deleteFolder(testRepoPath); | ||
| } | ||
| }); | ||
|
|
||
| it('syncs updated catalogs from pnpm-workspace.yaml to pnpm-config.json', () => { | ||
| const updatedWorkspaceYaml = `packages: | ||
| - '../../apps/*' | ||
| catalogs: | ||
| default: | ||
| react: ^18.2.0 | ||
| react-dom: ^18.2.0 | ||
| typescript: ~5.3.0 | ||
| frontend: | ||
| vue: ^3.4.0 | ||
| `; | ||
| FileSystem.writeFile(pnpmWorkspacePath, updatedWorkspaceYaml); | ||
|
|
||
| const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( | ||
| path.join(testRepoPath, 'rush.json') | ||
| ); | ||
|
|
||
| const subspace = rushConfiguration.getSubspace('default'); | ||
| const pnpmOptions = subspace.getPnpmOptions(); | ||
|
|
||
| expect(TestUtilities.stripAnnotations(pnpmOptions?.globalCatalogs)).toEqual({ | ||
| default: { | ||
| react: '^18.0.0', | ||
| 'react-dom': '^18.0.0' | ||
| } | ||
| }); | ||
|
|
||
| const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile'); | ||
| const newCatalogs = PnpmWorkspaceFile.loadCatalogsFromFile(pnpmWorkspacePath); | ||
|
|
||
| pnpmOptions?.updateGlobalCatalogs(newCatalogs); | ||
|
|
||
| const updatedRushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( | ||
| path.join(testRepoPath, 'rush.json') | ||
| ); | ||
| const updatedSubspace = updatedRushConfiguration.getSubspace('default'); | ||
| const updatedPnpmOptions = updatedSubspace.getPnpmOptions(); | ||
|
|
||
| expect(TestUtilities.stripAnnotations(updatedPnpmOptions?.globalCatalogs)).toEqual({ | ||
| default: { | ||
| react: '^18.2.0', | ||
| 'react-dom': '^18.2.0', | ||
| typescript: '~5.3.0' | ||
| }, | ||
| frontend: { | ||
| vue: '^3.4.0' | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('does not update pnpm-config.json when catalogs are unchanged', () => { | ||
| const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile'); | ||
| const newCatalogs = PnpmWorkspaceFile.loadCatalogsFromFile(pnpmWorkspacePath); | ||
|
|
||
| const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( | ||
| path.join(testRepoPath, 'rush.json') | ||
| ); | ||
| const subspace = rushConfiguration.getSubspace('default'); | ||
| const pnpmOptions = subspace.getPnpmOptions(); | ||
|
|
||
| pnpmOptions?.updateGlobalCatalogs(newCatalogs); | ||
|
|
||
| const savedConfig = JsonFile.load(pnpmConfigPath); | ||
| expect(savedConfig.globalCatalogs).toEqual({ | ||
| default: { | ||
| react: '^18.0.0', | ||
| 'react-dom': '^18.0.0' | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it('removes catalogs when pnpm-workspace.yaml has no catalogs', () => { | ||
| const workspaceWithoutCatalogs = `packages: | ||
| - '../../apps/*' | ||
| `; | ||
| FileSystem.writeFile(pnpmWorkspacePath, workspaceWithoutCatalogs); | ||
|
|
||
| const { PnpmWorkspaceFile } = require('../../logic/pnpm/PnpmWorkspaceFile'); | ||
| const newCatalogs = PnpmWorkspaceFile.loadCatalogsFromFile(pnpmWorkspacePath); | ||
|
|
||
| expect(newCatalogs).toBeUndefined(); | ||
|
|
||
| const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( | ||
| path.join(testRepoPath, 'rush.json') | ||
| ); | ||
| const subspace = rushConfiguration.getSubspace('default'); | ||
| const pnpmOptions = subspace.getPnpmOptions(); | ||
|
|
||
| pnpmOptions?.updateGlobalCatalogs(newCatalogs); | ||
|
|
||
| const updatedRushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile( | ||
| path.join(testRepoPath, 'rush.json') | ||
| ); | ||
| const updatedSubspace = updatedRushConfiguration.getSubspace('default'); | ||
| const updatedPnpmOptions = updatedSubspace.getPnpmOptions(); | ||
|
|
||
| expect(updatedPnpmOptions?.globalCatalogs).toBeUndefined(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
|
|
||
| import * as path from 'node:path'; | ||
|
|
||
| import { Sort, Import, Path } from '@rushstack/node-core-library'; | ||
| import { FileSystem, Sort, Import, Path } from '@rushstack/node-core-library'; | ||
|
|
||
| import { BaseWorkspaceFile } from '../base/BaseWorkspaceFile'; | ||
| import { PNPM_SHRINKWRAP_YAML_FORMAT } from './PnpmYamlCommon'; | ||
|
|
@@ -29,7 +29,7 @@ const globEscape: (unescaped: string) => string = require('glob-escape'); // No | |
| interface IPnpmWorkspaceYaml { | ||
| /** The list of local package directories */ | ||
| packages: string[]; | ||
| /** Catalog definitions for centralized version management */ | ||
| /** Named catalog definitions for centralized version management */ | ||
| catalogs?: Record<string, Record<string, string>>; | ||
| } | ||
|
|
||
|
|
@@ -56,6 +56,30 @@ export class PnpmWorkspaceFile extends BaseWorkspaceFile { | |
| this._catalogs = undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Loads and returns the catalogs section from an existing pnpm-workspace.yaml file. | ||
| * This method handles both the singular 'catalog' field (for the default catalog) and | ||
| * the plural 'catalogs' field (for named catalogs), merging them into a single object. | ||
| * | ||
| * @param workspaceYamlFilename - The path to the pnpm-workspace.yaml file | ||
| * @returns The catalogs object, or undefined if the file doesn't exist or has no catalogs | ||
| */ | ||
| public static loadCatalogsFromFile( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you do this async? |
||
| workspaceYamlFilename: string | ||
| ): Record<string, Record<string, string>> | undefined { | ||
| if (!FileSystem.exists(workspaceYamlFilename)) { | ||
| return undefined; | ||
| } | ||
| const content: string = FileSystem.readFile(workspaceYamlFilename); | ||
| const parsed: IPnpmWorkspaceYaml | undefined = yamlModule.load(content) as IPnpmWorkspaceYaml | undefined; | ||
|
|
||
| if (!parsed || !parsed.catalogs) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return parsed.catalogs; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the catalog definitions for the workspace. | ||
| * @param catalogs - A map of catalog name to package versions | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do this async?