@@ -18,12 +18,23 @@ const globEscape: (unescaped: string) => string = require('glob-escape'); // No
1818 * {
1919 * "packages": [
2020 * "../../apps/project1"
21- * ]
21+ * ],
22+ * "catalogs": {
23+ * "default": {
24+ * "lodash": "^4.17.21"
25+ * },
26+ * "react18": {
27+ * "react": "^18.2.0",
28+ * "react-dom": "^18.2.0"
29+ * }
30+ * }
2231 * }
2332 */
2433interface IPnpmWorkspaceYaml {
2534 /** The list of local package directories */
2635 packages : string [ ] ;
36+ /** Named catalogs - maps catalog names to package version mappings */
37+ catalogs ?: Record < string , Record < string , string > > ;
2738}
2839
2940export class PnpmWorkspaceFile extends BaseWorkspaceFile {
@@ -33,6 +44,7 @@ export class PnpmWorkspaceFile extends BaseWorkspaceFile {
3344 public readonly workspaceFilename : string ;
3445
3546 private _workspacePackages : Set < string > ;
47+ private _catalogs : Record < string , Record < string , string > > | undefined ;
3648
3749 /**
3850 * The PNPM workspace file is used to specify the location of workspaces relative to the root
@@ -45,6 +57,7 @@ export class PnpmWorkspaceFile extends BaseWorkspaceFile {
4557 // Ignore any existing file since this file is generated and we need to handle deleting packages
4658 // If we need to support manual customization, that should be an additional parameter for "base file"
4759 this . _workspacePackages = new Set < string > ( ) ;
60+ this . _catalogs = undefined ;
4861 }
4962
5063 /** @override */
@@ -59,6 +72,19 @@ export class PnpmWorkspaceFile extends BaseWorkspaceFile {
5972 this . _workspacePackages . add ( globEscape ( globPath ) ) ;
6073 }
6174
75+ /**
76+ * Set the named catalogs for the workspace.
77+ * Catalogs allow defining reusable dependency version ranges that can be referenced
78+ * in package.json files using the "catalog:" or "catalog:\<name\>" protocol.
79+ * Use the "default" catalog name for packages that should be referenced with "catalog:"
80+ * (no name), or use custom catalog names for "catalog:\<name\>" references.
81+ *
82+ * @param catalogs - A record mapping catalog names to package version mappings, or undefined to clear
83+ */
84+ public setCatalogs ( catalogs : Record < string , Record < string , string > > | undefined ) : void {
85+ this . _catalogs = catalogs ;
86+ }
87+
6288 /** @override */
6389 protected serialize ( ) : string {
6490 // Ensure stable sort order when serializing
@@ -67,6 +93,22 @@ export class PnpmWorkspaceFile extends BaseWorkspaceFile {
6793 const workspaceYaml : IPnpmWorkspaceYaml = {
6894 packages : Array . from ( this . _workspacePackages )
6995 } ;
96+
97+ // Add named catalogs if defined and non-empty
98+ if ( this . _catalogs && Object . keys ( this . _catalogs ) . length > 0 ) {
99+ // Sort the catalog names and entries for stable output
100+ const sortedCatalogs : Record < string , Record < string , string > > = { } ;
101+ for ( const catalogName of Object . keys ( this . _catalogs ) . sort ( ) ) {
102+ const catalog : Record < string , string > = this . _catalogs [ catalogName ] ;
103+ const sortedCatalog : Record < string , string > = { } ;
104+ for ( const key of Object . keys ( catalog ) . sort ( ) ) {
105+ sortedCatalog [ key ] = catalog [ key ] ;
106+ }
107+ sortedCatalogs [ catalogName ] = sortedCatalog ;
108+ }
109+ workspaceYaml . catalogs = sortedCatalogs ;
110+ }
111+
70112 return yamlModule . dump ( workspaceYaml , PNPM_SHRINKWRAP_YAML_FORMAT ) ;
71113 }
72114}
0 commit comments